Recursion, Binary Trees, and Heaps February 18 19 20 Recursion Review BST: Begin Slides Early Dismissal Problems on board BST Problems 23 24 25 26 27 Binary Tree problems QUIZ: Drawing trees and Traversals Binary Tree problems Deleting nodes & expression trees Heaps: Slides & Problems Quiz Heaps / deleting nodes from BST BST Program March 2 March 3 BST Program BST & Heaps Test
Recursion Review Evaluate the methods below with the indicated method call: 1. What is the result of the call m1(5)? public int m1(int n) if (n == 1) return 50; return n + m1(n-1); 2. What is the result of the call m2(5)? public int m2(int n) if (n == 1) return 1; return n * m2(n-1); 3. What is the result of the call m3(5,6)? public int m3(int a, int b) if (a == 0) return 0; return b + m3(a-1,b); 4. What is the result of the call m4(9,5)? public int m4(int a, int b) if (a < b) return 5; return b + m4(a-1,b+1); 5. What is the result of the call m5(6)? public int m5(int n) if (n <= 0) return 0; return n + m5(n-1) + m5(n-3);
6. What is the result of the call m6( TIGERS )? public static void m6(string s) int n = s.length(); out.println(s); if (n > 1) String temp = s.substring(0,n-1); m6(temp); 7. What is the result of the call m7( TIGERS )? public static void m7(string s) int n = s.length(); if (n > 1) String temp = s.substring(0,n-1); m7(temp); out.println(s); 8. What is the output of the call m8( TIGERS,0); public static void m8(string s, int x) if (x < s.length()) char temp = s.charat(x); out.println(temp); m8(s, x+1); 9. What is the output of the call m9( TIGERS,0); public static void m9(string s, int x) if (x < s.length()) char temp = s.charat(x); m9(s, x+1); out.println(temp);
Binary Trees 1. Draw the tree with this data input: 25 30 35 26 15 10 40 17 12 8 18 2. Draw the tree with this data input: 17 10 8 18 12 40 25 26 30 35 15 3. Draw the tree with this data input: 8 10 12 15 17 18 25 26 30 35 40
Traversal Practice 50 25 100 20 32 150 12 120 175 2 17 Preorder: Postorder: In Order: Reverse Order: 400 50 450 27 100 420 780 410 435 800 Preorder: 795 895 Postorder: In Order: Reverse Order:
1. Draw the binary tree with the following input: 120 100 200 75 115 190 250 50 195 118 270 2. What is the pre-order of the tree from #1? 3. What is the post-order of the tree from #1? 4. Draw the tree that has a post order traversal of: 32 30 38 42 40 36 97 110 100 95 50 5. Draw the tree that has a pre order traversal of: 36 32 30 40 38 100 50 42 95 97 110
6. Draw the tree with the pre-order of: 100 46 29 4 32 50 47 58 110 105 200 170 305 7. Draw the tree with a post-order of 1 4 2 9 10 5 14 25 40 50 45 70 60 30 15 8. Draw the tree with the pre-order of: 221 75 40 80 112 90 170 250 240 300 296 280 298 351 9. Draw the tree with a post-order of 40 62 55 150 275 200 298 300 350 421 400 325 296 112
Given the class below, finish each recursive function to process this tree as specified. public class bintree private int data; private bintree left; private bintree right; public void inorder() //outputs tree in order public void postorder() //outputs tree in post order public bintree small() //This function returns the node with the smallest value
Binary Tree Questions public class TreeNode private Object value; private TreeNode left; private TreeNode right; public TreeNode(Object initvalue) value = initvalue; left = null; right = null; public TreeNode(Object initvalue, TreeNode initleft, TreeNode initright) value = initvalue; left = initleft; right = initright; public Object getvalue() return value; public TreeNode getleft() return left; public TreeNode getright() return right; public void setvalue(object thenewvalue) value = thenewvalue; public void setleft(treenode thenewleft) left = thenewleft; public void setright(treenode thenewright) right = thenewright; 01. How many nodes are there in a full binary tree with n levels? n 2 2n 2 n 2 n - 1 2 n-1
02. Assume that a binary tree has every level complete, except level-4, which lacks 4 nodes. How many nodes are in this almost full binary tree? 27 12 11 8 4 03. A special binary tree will be defined as a binary tree where every parent has exactly two children. If such a tree has n parents, how many leaves will the tree have? n n - 1 n + 1 2 n - n 2 n - 1 04. If the root is at level-0, and the binary tree is full, how many nodes will be at level-n? n 2n 2 n 2 n+1 n/2 05. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree if (p!= null) mystery(p.getleft()); out.print(p.getvalue() + " "); mystery(p.getright()); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order
06. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree if (p!= null) mystery(p.getleft()); mystery(p.getright()); out.print(p.getvalue() + " "); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order 07. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree if (p!= null) mystery(p.getright()); out.print(p.getvalue() + " "); mystery(p.getleft()); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order
08. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree Queue temp = new Queue(); if (p!= null) temp.add(p); while(!temp.isempty()) p = (TreeNode) temp.dequeue(); out.print(p.getvalue() + " "); if (p.getleft()!= null) temp.add(p.getleft()); if (p.getright()!= null) temp.add(p.getright()); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order 09. Consider the following mystery method. public static int mystery(treenode p) // precondition: p references the root of a binary search tree if (p == null) return 0; if (p.getleft() == null && p.getright() == null) return 1; return ( mystery(p.getleft()) + mystery(p.getright()) ); What type of binary tree information is returned by method mystery? The number of tree nodes The number of parent nodes The height of the tree The number of leaf nodes The sum of the data in each tree node
10. Consider the following mystery method. public static int mystery(treenode p) // precondition: p references the root of a binary search tree if (p == null) return 0; if (mystery(p.getleft()) > mystery(p.getright())) return 1 + mystery(p.getleft()); return 1 + mystery(p.getright()); What type of binary tree information is returned by method mystery? The number of tree nodes The number of parent nodes The height of the tree The number of leaf nodes The sum of the data in each tree node 11. Consider the following mystery method.. public static int mystery(treenode p) // precondition: p references the root of a binary search tree if (p == null) return 0; return 1 + mystery(p.getleft()) + mystery(p.getright()); What type of binary tree information is returned by method mystery? The number of tree nodes The number of parent nodes The height of the tree The number of leaf nodes The sum of the data in each tree node
12. Assume the following sequence of numbers is entered in a binary search tree during program execution. 6251, 6080, 9241, 1828, 4055, 2084, 2375, 9802, 2501, 5389 The data entry is followed by a call to a binary tree traversal, which prints the following numbers: 2501, 2375, 2084, 5389, 4055, 1828, 6080, 9802, 9241, 6251 Based on the display above, what type of binary tree traversal is performed by the program? inorder preorder postorder reverse inorder level order 13. Assume the following sequence of numbers is entered in a binary search tree during program execution. 1900, 1600, 1200, 4021, 3042, 1809, 5316, 2519 The data entry is followed by a call to a binary tree traversal, which prints the following numbers: 1900, 1600, 4021, 1200, 1809, 3042, 5316, 2519 Based on the display above, what type of binary tree traversal is performed by the program? inorder preorder postorder reverse inorder level order 14. Consider the following strange method. public static TreeNode strange(treenode p) TreeNode t; if (p == null) return null; t = new TreeNode(p.getValue()); t.setleft(strange(p.getright())); t.setright(strange(p.getleft())); return t; What type of binary tree information is returned by method strange? A reference to the same binary tree root as p A reference to a copy of the tree referenced to by p A reference to a mirror copy of the tree referenced to by p A reference to the node with the largest data value A reference to the node with the smallest data value
15. Consider the following incomplete method. public static int parents(treenode p) // precondition: p references the root of a binary search tree // postcondition: returns the number of nodes in the // binary search tree that are parents. if (p == null) return 0; /* missing code */ Which of the following three implementations of /* missing code */ will make the method work as intended? Implementation 1 if (p.getleft() == null && p.getright() == null) return 0; return 1 + parents(p.getleft()) + parents(p.getright()); Implementation 2 if (p.getleft() == null p.getright() == null) return 0; return 1 + parents(p.getleft()) + parents(p.getright()); Implementation 3 if (p.getleft() == null && p.getright() == null) return 1; return parents(p.getleft()) + parents(p.getright()); Implementation 1 only Implementation 2 only Implementation 3 only Implementations 1 and 2 only Implementations 2 and 3 only
Expression Trees Expression trees are useful as a vehicle for discussing the traversal of a tree. An expression tree is a binary tree which is used to represent a mathematical expression. For example, if we have the expression (2 * (4 + (5 + 3))), we could construct a tree to represent it. In an expression tree, the parent nodes are the operators, and the children are the operands. To find the result of this expression, we need to first solve (5 + 3), which is 8, then solve (4 + 8), which is 12, and then finally solve 2 * 12, which is 24. So our root node will contain the operator within the outermost set of parentheses, its left child will be the value "2", and the right child will be the remaining expression that needs to be solved, which would be (4 + (5 + 3)). and the following example:
1. On the back page, draw an expression tree for each of the following expressions: A. 3 + (2 * A) B B. 20 * (C 5) C. A * C 5 D. (17 % X) * 11 + Y E. (A + B + C) / 4 2. Use the following tree for question 2: 50 35 80 20 40 75 90 60 100 3. A. Draw the tree with 80 deleted. B. Draw the tree with 50 deleted. C. Draw the tree with 35 deleted.
Given this list, create a min heap tree: 7 3 19 22 13 2 14 27 6 9
Given this heap tree, indicate what it will look like after: 15 20 32 22 30 44 50 25 32 40 1. 17 is added 2. 45 is added 3. 5 is added 4. 20 is deleted 5. 44 is deleted 6. 15 is deleted