# trees16 - Binary Trees The traversal algorithms that...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Binary Trees The traversal algorithms that appeared in the previous set of notes make exploit the fact that a binary tree can be recursively defined as: A binary tree is: empty, or a node (the root) with two binary trees called the left subtree and the right subtree of the root. Lets examine the inorder traversal algorithm again: Consider the following binary tree and produce the inorder traversal using the algorithm above. (For practice do the preorder and postorder traversals as well.) Binary Trees - 1 Data Structures: Binary Trees void inorder( struct treeNode *p) { if (p != NULL) { inorder(p->left); printf(%d\n, p->data); inorder(p->right); } } A B C D E F G Answers: preorder traversal: A B C D F G E inorder traversal: B A F D G C E postorder traversal: B F G D E C A An inorder traversal of this binary tree produces: B A F D G C E. Notice that the search for a node to print moves as deeply as possible in the left subtree before ever considering the right subtree. This is called a depth-first traversal . The preorder and postorder traversals are also depth-first traversals. Now lets perform some other operations on binary trees. Suppose that we want to find the largest value in a binary tree. The following algorithm will accomplish this task. Trace of algorithm findMax Binary Trees - 2 int findMax (struct treeNode *p) { int root_value, left, right, max; max = -1 //assume all values in the tree are positive integers if (p!= NULL) { root_value = p -> data; left = findMax( p -> left); right = findMax( p -> right ); //find the largest of the tree values. if (left > right) max = left; else max = right; if (root_value > max) max = root_value; } return max; } struct treeNode { int data; struct treeNode *left; struct treeNode *right; }; Lets trace the execution of algorithm findMax on the tree shown below: 1. [intial call] p == root, root_value = 40 2. left = findMax(ptr to 54) root_value = 54 3. left == NULL, return(-1) 4. right == NULL, return(-1) 5. return 54 6. right = findMax(ptr to 13) root_value = 13 7. left = findMax(ptr to 23) root_value = 23 8. left == NULL, return(-1) 9. right ==NULL, return(-1) 10. return 23 11. right = findMax(ptr to 77) root_value = 77 12. left == NULL, return(-1) 13. right == NULL, return(-1) 14. return 77 On return: left subtree search at 40 returns 54, right subtree pending left subtree of 13 returns 23, right subtree of 13 returns 77 tree rooted at 13 returns 77 as the right subtree at 40 tree at 40 has left subtree 54, and right subtree 77 77 is returned as maximum value in the tree....
View Full Document

## This document was uploaded on 06/12/2011.

### Page1 / 12

trees16 - Binary Trees The traversal algorithms that...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online