Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Recursion and Trees Dale Roberts, Lecturer

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Recursion and Trees Dale Roberts, Lecturer"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Recursion and Trees Dale Roberts, Lecturer IUPUIdroberts@cs.iupui.edu

2 Dale Roberts Trees Definitions Tree: A tree is a collection of nodes. If the collection is not empty, it must consist of a unique root node, r, and zero or more nonempty subtrees T 1, T 2,…, T k, with roots connected by a direct edge from r For a tree of N nodes, there must be N-1 edges. A node with no child is called a leaf node; nodes with the same parents are siblings. A E C D G F I H B M L K J N P Q Path: a path from node n 1 to n k is a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+1 (1  i  k); The no. of edges in the path is call the length of the path; A length 0 path is a path from a node to itself. There is a unique path from root to any node n i ; The length of this path is called the depth of n i ; thus, the root is a depth 0 node. Path: a path from node n 1 to n k is a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+1 (1  i  k); The no. of edges in the path is call the length of the path; A length 0 path is a path from a node to itself. There is a unique path from root to any node n i ; The length of this path is called the depth of n i ; thus, the root is a depth 0 node. The height of a node n i is the length of the longest path from n i to a leaf node, thus, the height of a leaf node is 0. The height of a node n i is the length of the longest path from n i to a leaf node, thus, the height of a leaf node is 0. The height of a tree is the height of its root node. The depth of a tree is the depth of the deepest leaf node, which is the same as the height of the tree. The height of a tree is the height of its root node. The depth of a tree is the depth of the deepest leaf node, which is the same as the height of the tree.

3 Dale Roberts Trees Height – deepest level, not including root. Leaf nodes – have no children Interior Notes – have at least one child Degree of a node – Number of children In a binary tree, interior nodes have degree 1 or 2.

4 Dale Roberts Binary trees – –Definition: A binary tree is a tree in which no nodes can have more than two children. (That is, the degree of interior nodes is restricted to 1 or 2.) – –The root node is the first node in a tree. – –Each link in the root node refers to a child – –A node with no children is called a leaf node – –Total number of nodes in a full binary tree of height k is 2 k -1 Binary Trees

5 Dale Roberts Array implementation of binary tree We can embed the nodes of a binary tree into a one-dimensional array by defining a relationship between the position of each parent node and the position of its children. 1. left_child of node i is 2*i 1. left_child of node i is 2*i 2. right_child of node i is 2*i+1 2. right_child of node i is 2*i+1 3. parent of node i is i/2 (integer division) 3. parent of node i is i/2 (integer division) How much space is required for the array to represent a tree of depth d? 2 d+1 – 1 (must assume full tree)

6 Dale Roberts Dynamic Implementation of Binary Tree A dynamic implementation of a binary tree uses space proportional to the actual number of nodes used. Not the size of the full tree. Struct BinaryNode { data_type element; BinaryNode  left; BinaryNode  right; }

7 Dale Roberts Binary Search Tree (BST) Binary search tree (BST) Values in left subtree less than parent Values in right subtree greater than parent Facilitates duplicate elimination Fast searches - for a balanced tree, maximum of log 2 (n) comparisons Example: Construct an Binary Search Tree (BST) for the data 555501701358513561797345490724 Array Implementation: 47 25 77 11 43 65 93 68 7 17 31 44 555 501 555 501701 555 501701 358 555 501701 358513 555 501701 358513561 555 501701 358513561797 555 501701 358513561797 345490345 555 501701 358513561797 490724 555501701358513561797345490 724

8 Dale Roberts Tree Traversals Tree traversals: In-order traversal – prints the node values in ascending order (LNR) 1. Traverse the left sub-tree with an in-order traversal 2. Process the value in the node (i.e., print the node value) 3. Traverse the right sub-tree with an in-order traversal Pre-order traversal (NLR) 1. Process the value in the node 2. Traverse the left sub-tree with a preorder traversal 3. Traverse the right sub-tree with a preorder traversal Post-order traversal (LRN) 1. Traverse the left sub-tree with a post-order traversal 2. Traverse the right sub-tree with a post-order traversal 3. Process the value in the node

9 Dale Roberts A simple definition of recursion Recursion simply means a function that calls itself. The conditions that cause a function to call itself again are called the recursive case. In order to keep the recursion from going on forever, you must make sure you hit a termination condition called the base case. The number of nested invocations is called the depth of recursion. Function may call itself directly or indirectly. (All of our examples are direct.)

10 Dale Roberts Tree Traversal is “Naturally” recursive Naturally recursive because: 1. Subgraphs are similar in character to the original graph. 2. Subgraphs are smaller. 3. Guaranteed to eventually hit a leaf (acyclic is assumed) Obeys fundamental rules of recursion 1.) Base cases 2.) Making progress through recursion 3.) Design rule: assuming all recursive call work (details hidden) 4.) Compound interest rule: do not duplicate recursive calls Always specify the base case; otherwise, indefinite recursive will occur and cause “stack-overflow” error.

11 Dale Roberts Sample recursive programs In-order: (LNR) void inorder(tree_pointer ptr) { if (ptr) if (ptr) { inorder(ptr->left_child); inorder(ptr->left_child); printf(“%d”, ptr->data); printf(“%d”, ptr->data); inorder(ptr->right_child); inorder(ptr->right_child); }} Pre-order: (NLR) void preorder(tree_pointer ptr) { if (ptr) if (ptr) { printf(“%d”, ptr->data); printf(“%d”, ptr->data); preorder(ptr->left_child); preorder(ptr->left_child); preorder(ptr->right_child); preorder(ptr->right_child); }} Post-order: (LRN) void postorder(tree_pointer ptr) { if (ptr) if (ptr) { postorder(ptr->left_child); postorder(ptr->left_child); postorder(ptr->right_child); postorder(ptr->right_child); printf(“%d”, ptr->data); printf(“%d”, ptr->data); }}

12 Dale Roberts Example: Expression Tree Example: An Expression Tree 1. Perform in-order traversal (LNR) A – B + C * E / F 2. Perform post-order traversal (LRN) A B – C E F / * + 3. Perform pre-order traversal (NLR) + - A B * C / E F + -* AB C / E F

13 Dale Roberts Tree Traversal Example Try your hand at traversing this tree: In-order: (LNR) Pre-order: (NLR) Post-order: (LRN) Apr, Aug, Dec, Feb, Jan, Jun, Jan, Mar, May, Nov, Oct, Sep Mar, Apr, Jul, Feb, Dec, Aug, Jun, Jan, May, Sep, Oct, Nov Aug, Dec, Jan, Jun, Feb, Jul, Apr, Nov, Oct, Sep, May, Mar

14 Dale Roberts Adjacency List We need a computer representation for a graph if we want to use the computer to solve graph problems. One representation is to list the nodes, and then build a list of connections from each node. Notice that in this graph, connections are not directional. If A is connected to B, then B is connected to A.

15 Dale Roberts Adjacency Matrix For a graph G with n vertices we create an n x n boolean (or equivalent) matrix. Label the row and columns of the matrix with the names of the vertices. Each element of the matrix represents a potential edge in the graph as defined by its associated vertex pair. We set an element of the matrix to TRUE if there is an edge connecting the two corresponding vertices. Notice that the matrix is symmetric along diagonal. Connections can be give weights to represent the cost or distance between nodes. What’s a problem with this approach?

16 Dale Roberts Dynamic Adjacency Matrix A dynamic list can be used to represent the actual number of connections in the graph. Not the n x n worst case. What problem does this approach have that the matrix approach does not?

17 Dale Roberts Summary We have discussed how to represent trees in a computer system. Binary Trees restricted to degree <= 2. Unrestricted tree that are acyclic graphs. Unrestricted graphs that use adjacency information to represent path. You will receive a programming assignment that traverses a graph to list shortest path between two points. (You will be given the algorithm.)

18 Dale Roberts Acknowledgements Horowitz and Sahni. Data Structures in C++ Bob Pilgram. Murray State University.


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Recursion and Trees Dale Roberts, Lecturer"

Similar presentations


Ads by Google