170 likes | 323 Views
COP 3502: Computer Science I Spring 2004 – Note Set 20 – Binary Trees – Part 4. Instructor : Mark Llewellyn markl@cs.ucf.edu CC1 211, 823-2790 http://www.cs.ucf.edu/courses/cop3502/spr04. School of Electrical Engineering and Computer Science University of Central Florida.
E N D
COP 3502: Computer Science I Spring 2004 – Note Set 20 – Binary Trees – Part 4 Instructor : Mark Llewellyn markl@cs.ucf.edu CC1 211, 823-2790 http://www.cs.ucf.edu/courses/cop3502/spr04 School of Electrical Engineering and Computer Science University of Central Florida
Try all of the practice problems on the next couple of pages. Solutions appear after the problem descriptions. Write a function that will return the count of the number of leaf nodes in a binary tree. Write a function that will return the height of a binary tree. The height of an empty tree is defined to be 0. The height of a single node tree is defined to be 1. List the nodes of the trees shown on pages 4 and 5 using preorder, inorder, and postorder traversals. Is the binary tree on page 4 a valid binary search tree? If not, indicate why it isn’t. Is the binary tree on page 5 a valid binary search tree? If not, indicate why it isn’t. Practice Problems
Answer the following questions using the binary tree on page 4: (a) What is the degree of node 36? (b) What is the degree of node 88? (c) What is the height of node 64? (d) What is the sibling of node 23? (e) How many grandchildren does node 24 have and what are there values? (f) Which node is the parent of node 62? (g) Which node is the grandparent of node 62? (h) What is the height of the tree? List all the leaf nodes in the tree. Given the following data values, construct a binary search tree based on the order the values appear: 26, 13, 17, 29, 34, 10, 8, 28, 20. Practice Problems (cont.)
Practice Problems (cont.) 50 Tree #1 24 65 59 88 23 36 90 62 15 28 40 94 30 42 58 64 8 19 61 92 41 17 22
Practice Problems (cont.) 50 Tree #2 74 25 20 35 66 80 10 34 38 60 70 96 6 26 37 52 68 72 85 98 9 36 58 82 87
Solution to Practice Problem #1 // function returns the number of leaf nodes in a binary tree int number_of_leaves (struct treeNode *p) { if (p!= NULL) if (p->left == NULL && p->right == NULL) return 1; else return (number_of_leaves(p->left) + number_of_leaves(p->right); else return 0; }
Solution to Practice Problem #2 // function returns the height of a binary tree int height (struct treeNode *p) { int leftheight, rightheight; if (p == NULL) return 0; else { leftheight = height(p->left); rightheight = height(p->right); if (leftheight > rightheight) return(leftheight + 1); else return(rightheight + 1); } }
Tree #1 on Page 3 Preorder traversal produces: 50, 24, 23, 15, 8, 19, 17, 22, 36, 28, 30, 40, 42, 41, 65, 59, 62, 58, 61, 64, 88, 90, 94, 82 Inorder traversal produces: 8, 15, 17, 19, 22, 23, 24, 28, 30, 36, 40, 41, 42, 50, 59, 58, 61, 62, 64, 65, 88, 90, 92, 94 Postorder traversal produces: 8, 17, 22, 19, 15, 23, 30, 28, 41, 42, 40, 36, 24, 61, 58, 64, 62, 59, 92, 94, 90, 88, 65, 50 Tree #2 on Page 4 Preorder traversal produces: 50, 25, 20, 10, 6, 9, 35, 34, 26, 38, 40, 36, 74, 66, 60, 52, 58, 70, 68, 72, 80, 96, 85, 82, 87, 98 Inorder traversal produces: 6, 9, 10, 20, 25, 26, 34, 35, 36, 37, 38, 50, 52, 58, 60, 66, 68, 70, 72, 74, 80, 82, 85, 87, 96, 98 Postorder traversal produces: 9, 6, 10, 20, 26, 34, 36, 37, 38, 35, 25, 38, 52, 60, 68, 72, 70, 66, 82, 87, 85, 98, 96, 80, 74, 50 Solution to Practice Problem #3
Solution to Problem #4 No, the tree is not a BST because node with value 58 is in the right subtree of node with value 58. This can also be seen in the inorder traversal of the tree where the values 58 and 59 do not appear in order. Solution to Problem #5 Yes, the tree is a BST. This can also be verified with the inorder traversal of the tree where all of the values appear in order. Solution to Practice Problems #4 and #5
(a) What is the degree of node 36? 2 (b) What is the degree of node 88? 1 (c) What is the height of node 64? 5 (d) What is the sibling of node 23? 36 (e) How many grandchildren does node 24 have and what are there values? 3 grandchildren (15, 28, 40) 4 great-grandchildren (8, 19, 30, 42) 3 great-great-grandchildren (17, 22, 41) (f) Which node is the parent of node 62? 59 (g) Which node is the grandparent of node 62? 65 (h) What is the height of the tree? 6 (i) List all the leaf nodes in the tree. 8, 17, 22, 30, 41, 61, 64, 92 Solutions to Practice Problem #6
26 13 26 Step 4: add 29 26 Step 3: add 17 29 13 13 17 17 Solution to Practice Problem #7 26 Step 1: add 26 Step 2: add 13
26 26 Step 6: add 10 Step 5: add 34 29 29 13 13 17 34 17 34 10 26 26 Step 7: add 8 Step 8: add 28 29 13 29 13 17 34 10 17 28 34 10 8 8 Solution to Practice Problem #7 (cont.)
26 Step 9: add 20 29 13 17 28 34 10 8 20 Solution to Practice Problem #7 (cont.) Final tree
In the last set of notes we saw that deletion of nodes in a BST can be broken into three separate cases: Deletion of a leaf node. This was a trivial case as the node can simply be deleted without any additional changes to the BST. Deletion of an internal node with a single child (either a left or right subtree). This case wasn’t too difficult either, we simply had to “move” the tree rooted at the child node up one level to take the place of the deleted node. It made no difference if the deleted node had only a left or only a right child. Deletion of an internal node with two children (has both a left and right subtree). This was the difficult case where we need to find either the deleted node’s immediate predecessor or its immediate successor and move this node into the spot vacated by the deleted node. Deletion In A Binary Search Tree
Think about the scenarios that could exist when the logical predecessor to an internal node with two children is located. It is possible that the logical predecessor is a leaf node. Deletion via copying will handle this case. The logical predecessor node simply replaces the node to be deleted. This case was illustrated on pages 24 and 25 of Note Set #19. It is also possible that the logical predecessor node is an internal node. Question: Is it possible for the logical predecessor node to have two children? Answer: No. Explanation on next page. Deletion of a Node with Two Children From A BST
Deletion of Node with Two Children From a BST (cont.) 59 logical predecessor 15 67 13 32 63 72 10 14 25 43 This node cannot exist if 13 is the logical predecessor of 15. Consider the scenario shown in the tree above. Node 15 is to be deleted. The logical predecessor is node 13. The question becomes, would it be possible for the node in the position shown containing 14 to exist in this BST? The answer is clearly no, because if it did exist then node 13 would not be the rightmost node in the left subtree rooted at 15 and thus it would not be logical predecessor of 15. In this case, node 14 would be the logical predecessor and in this case, it would be a leaf node.
Deletion of Node with Two Children From a BST (cont.) 59 logical successor 25 67 13 32 63 72 10 15 30 43 This node cannot exist if 32 is to be the logical successor of 25. A symmetric situation arises if we are considering the logical successor case. In the tree above the node containing 25 is to be deleted. The logical successor is node 32. The question becomes, would it be possible for the node in the position shown containing 30 to exist in this BST? The answer is again no, because if it did exist then node 32 would not be the leftmost node in the right subtree rooted at 25 and thus it would not be logical successor of 25. In this case, node 30 would be the logical successor and in this case, it would happen to be a leaf node.