Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees and Recursive Algorithms. Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F?

Similar presentations


Presentation on theme: "Trees and Recursive Algorithms. Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F?"— Presentation transcript:

1 Trees and Recursive Algorithms

2 Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F? – Recursive. – Reduce the original problem to smaller problem(s), and execute the same function to each smaller problem. – For the “smallest” problem (stopping case), solve it directly.

3 Recursive Example Recursive code usually looks like this: if (the stopping case is reached) { Solve it } else { Reduce the problem using recursion } Here is a recursive function to compute the factorial of a positive number. int Factorial (int N) // Computes the factorial of N recursively, returns N! { if (N == 1) { return 1; // stopping case } else { return N * Factorial(N-1); // recursion } }

4 Tree definition Recursively defined data structure Tree (in general) – Empty – Data + a specific number of subtrees Binary tree – Empty – Data + left subtree + right subtree

5 C Binary Tree node struct btnode { int data; struct btnode *left; struct btnode *right; } ;

6 Visual example of a binary tree Each value corresponds to a node in the tree root is a struct btnode pointer that points at the node containing the 9

7 Tree traversal How to visit each node only once. Do not miss any node.

8 Tree traversal (preorder) PreOrderPrint(struct btnode *anode) { if(anode == NULL) return; printf(“%i ”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Output of PreOrderPrint(root) is: 9 6 2 7 15 12 25

9 Tree traversal (preorder incorrect) PreOrderPrint(struct btnode *anode) { printf(“%i”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Any problems with this function?

10 Tree traversal (inorder) InOrderPrint(struct btnode *anode) { if(anode == NULL) return; InOrderPrint(anode->left); printf(“%i ”, anode->data); InOrderPrint(anode->right); } Output of InOrderPrint(root) is: 2 6 7 9 12 15 25

11 Tree traversal (postorder) PostOrderPrint(struct btnode *anode) { if(anode == NULL) return; PostOrderPrint(anode->left); PostOrderPrint(anode->right); printf(“%i ”, anode->data); } Output of PostOrderPrint(root) is: 2 7 6 12 25 15 9

12 Tree termination NULL pointers NULL are not btnodes, but the value of their parent’s left and right pointers NULL data -1 are btnodes, whose left and right btnodes are uninitialized Assumption: -1 is never valid data in the tree

13 Creating nodes struct node * NewNode(int data) { struct node *mynode = (struct node *) malloc (sizeof(struct node)); mynode->data = data; mynode->left = NULL; mynode->right = NULL; return(node); }

14 Deallocating binary trees Three things to do – Free current node – Recursively free left subtree – Recursively free right subtree What is the order? void delete_tree(struct btnode *leaf) { if( leaf != NULL ) { delete_tree(leaf->left); delete_tree(leaf->right); free( leaf ); }


Download ppt "Trees and Recursive Algorithms. Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F?"

Similar presentations


Ads by Google