1. Answer the questions below about the following binary tree: A B C D E F G H a. b. c. d. e. f. g. h. i. What is the root element? A How many elements are in the tree? 8 How many leaves are in the tree? 3 What is the height of the tree? 4 What is the height of the left subtree? 3 What is the height of the right subtree? –1 What is the level of F? 3 What is the depth of C? 2 What is the depth of C? 1 j. What is the parent of F? D k. What are the descendants of B? o. C, D, E, F, G, and H What are the ancestors of F? A, B, and D What would the output be if the elements were written out during an inOrder traversal? C, H, E, B, F, D, G, A What would the output be if the elements were written out during a postOrder traversal? H, E, C, F, G, D, B, A What would the output be if the elements were written out p. during a preOrder traversal? A, B, C, E, H, D, F, G What would the output be if the elements were written out l. m. n. during a breadth-first traversal? A, B, C, D, E, F, G, H 2. a. What is the maximum number of leaves possible in a binary tree with 10 elements? Construct such a tree. 5 A B D C E H F I G J b. What is the minimum number of leaves possible in a binary tree with 10 elements? Construct such a tree. 1 A B C D E F G H I J 3. a. Construct a two-tree that is not complete. A B D C E F G H I b. Construct a complete tree that is not a two-tree. A B C D c. Construct a complete two-tree that is not full. A B D H C E F G I d. How many leaves are there in a two-tree with 17 elements? 9 e. How many leaves are there in a two-tree with 731 elements? 366 f. A non-empty two-tree must always have an odd number of elements. Why? Hint: Use the Binary Tree Theorem and the fact that the number of leaves must be an integer. By part 3 of the binary tree theorem, if t is a two tree, leaves(t) = (n(t) + 1) / 2.0 Therefore, 2.0 leaves(t) – 1 = n(t), which implies that n(t) must be odd. g. How many elements are there in a full binary tree of height 4? 31 h. How many elements are there in a full binary tree of height 12? 8191 i. Use the Binary Tree Theorem to determine the number of leaves in a full binary tree with 63 elements. Because a full binary tree must also be a two tree, leaves(t) = (n(t) + 1) / 2.0 = (63 + 1) / 2.0 = 32 k. Construct a complete two-tree that is not full, but in which the heights of the left and right subtrees are equal. A B C D H 4. E I J F KL G M For the following binary tree, show the order in which elements would be visited for an inOrder, postOrder, preOrder, and breadthFirst traversal. 60 38 14 97 48 68 25 62 65 inOrder: 14, 25, 38, 48, 60, 62, 68, 85, 97 postOrder: 25, 14, 48, 38, 62, 85, 68, 97, 60 preOrder: 60, 38, 14, 25, 48, 97, 68, 62, 85 breadthFirst: 60, 38, 97, 14, 48, 68, 25, 62, 85 5. Show that a binary tree with n elements has 2n + 1 subtrees (including the entire tree). How many of these subtrees are empty? For each of the n elements, there are two subtrees, so this accounts for 2n subtrees. The entire tree is also a subtree, so the total number of subtrees is 2n + 1. Each of the n elements is the root of a non-empty subtree, so there are n non-empty subtrees and therefore n + 1 empty subtrees. 6. Show that if t is a non-empty, complete binary tree, then height(t) = floor(log2(n(t))) Hint: Let t be a complete binary tree of height k >= 0, and let t1 be a full binary tree of height k – 1. Then n(t1) + 1 <= n(t). Use Part 4 of the Binary Tree Theorem to show that floor(log2(n(t1) + 1)) = k, and use Part 1 of the Binary Tree Theorem to show that floor (log2(n(t))) < k + 1. Because a complete binary tree of height k is full through level k – 1, n(t1) + 1 <= n(t). Both the logarithm and floor functions are non-decreasing, so we get (a) floor(log2(n(t1) + 1)) <= floor(log2(n(t))) Because t1 is a full binary tree, we know from Part 4 of the Binary Tree Theorem that (n(t1) + 1) / 2.0 = 2height (t1) = 2k – 1 This implies that n(t1) + 1 = 2k, and so (b) floor(log2(n (t1) + 1)) = k. By Part 1 of the Binary Tree Theorem, (n(t) + 1) / 2.0 <= 2height (t) = 2k This implies that n(t) + 1 <= 2k + 1, and so n(t) + 1 < 2k + 1, and therefore log2(n(t)) < k + 1, from which we obtain (c) floor(log2(n(t))) < k + 1 Combining the results from b, a, and c, we get k = floor(log2(n (t1) + 1)) <= floor(log2(n(t))) < k + 1 All of these expressions are integers, and so floor(log2(n(t))) = k 7. Give an example of a non-empty binary tree that is not a two-tree but leaves(t) = (n(t) + 1) / 2 Hint: The denominator is 2, not 2.0, so integer division is performed. A B C D 8. Let t be a non-empty tree. Show that if n(t) + 1 leaves(t) = 2.0 then either both subtrees of t are empty or both subtrees of t are non-empty. Note: Do not use Part 3 of the Binary Tree Theorem. This exercise can be used in the proof of Part 3. Let t be a non-empty binary tree with n(t) + 1 leaves(t) = 2.0 If t consists of a single element, then both of its subtrees must be empty, and we are done. Otherwise, at least one – call it t1 – of t’s subtrees must be non-empty. Assume that the other subtree is empty. Then all of the leaves of t must be in t1, and t1 must have one less element than t. So we have leaves(t1) = leaves(t) = (n(t) + 1) / 2.0 > n(t) / 2.0 = (n(t1) + 1) / 2.0. By Part 1 of the Binary Tree Theorem, this last expression is >= leaves(t1). Then leaves(t1) > leaves(t1) This contradiction shows that both of t’s subtrees must be non-empty, completing the proof of the claim. 9. Show that in any complete binary tree t, at least half of the elements are leaves. Hint: if t is empty, there are no elements, so the claim is vacuously true. If the leaf at the highest index is a right child, then t is a two-tree, and the claim follows from part 3 of the Binary Tree Theorem. Otherwise, t was formed by adding a left child to the complete two-tree with n(t) – 1 elements. After the hint, all that remains is to prove the claim if t is formed by adding a left child to the complete two-tree with n(t) – 1 elements. That two-tree must have had n(t)-1+1 2.0 leaves. But t has the same number of leaves because adding a child to an element in a two-tree does not change the number of leaves. So t has n(t) / 2.0 leaves: exactly half the number of elements (t must have an even number of elements because a two-tree has an odd number of elements). 10. Let t be a nonempty binary tree. Use the Strong Form of the Principle of Mathematical Induction to show each of the following a. n(t)+1 2.0 <= 2ℎ𝑒𝑖𝑔ℎ𝑡(𝑡) For k = 0, 1, 2, …, let Sk be the statement: If t is a non-empty binary tree of height k, then n(t)+1 2.0 <= 2height(t) Base Case. Let t be a non-empty binary tree of height 0. Then n(t) = 1 and 2height(t) = 1. We have 1+1 2.0 <= 1 Inductive Case. Let k be any integer >= 0, and assume that S0, S1, … Sk are true. We need to show that Sk+1 is true. Let t be a non-empty binary tree of height k + 1. Both leftTree(t) and rightTree(t) have height <= k, so both satisfy the induction hypothesis. That is, (n(leftTree(t)) + 1) / 2.0 <= 2height(leftTree(t)) And (n(rightTree(t)) + 1) / 2.0 <= 2height(rightTree(t)) n(t) = n(leftTree(t)) + n(rightTree(t)) + 1, and so we have (n(t) + 1) / 2.0 = (n(leftTree(t)) + n(rightTree(t)) + 1 + 1) / 2.0 = (n(leftTree(t)) + 1) / 2.0 + (n(rightTree(t)) + 1) / 2.0 <= 2height(leftTree(t)) + 2height(rightTree(t)) <= 2height(t) – 1 + 2height(t) – 1 // because the height of // either subtree is less // than the height of t = 2 height(t). We conclude that Sk+1 is true. Therefore, by the Principle of Mathematical Induction, n(t)+1 2.0 <= 2height(t) for any non-empty binary tree t. b. If t is a two-tree, then leaves(t) = n(t)+1 2.0 For k = 0, 1, 2, …, let Sk be the statement If t is a two tree of height k, then leaves(t) = (n(t) + 1) / 2.0. Base Case. If k = 0, then t consists of a single element, and it is a leaf. We have leaves(t) = 1 = (1 + 1) / 2.0 = (n(t) + 1) / 2.0 Inductive Case. Let k be any integer >= 0, and assume that S0, S1, … Sk are true. We need to show that Sk+1 is true. Let t be a non-empty two tree of height k + 1. Because t is a two tree, both leftTree(t) and rightTree(t) are two trees with height less than k + 1, and so the induction hypothesis applies to both of those subtrees. Also, each leaf in t is either in leftTree(t) or in rightTree(t), and t has one more element than those subtrees. We have leaves(t) = leaves(leftTree(t)) + leaves(rightTree(t)) = (n(leftTree(t)) + 1) / 2.0 + (n(rightTree(t)) + 1) / 2.0 = (n(leftTree(t)) + 1 + n(rightTree(t)) + 1) / 2.0 = (n(t) + 1) / 2.0. That is, Sk+1 is true. Therefore, by the Principle of Mathematical Induction, Sk is true for any non-negative integer k. c. If t is a full tree, then n(t)+1 2.0 = 2ℎ𝑒𝑖𝑔ℎ𝑡(𝑡) For k = 0, 1, 2, …, let Sk be the statement If t is a full tree of height k, then (n(t) + 1) / 2.0 = 2height(t). Base Case. If k = 0, then t consists of a single element, and the height of t is 0. We have (n(t) + 1) / 2.0 = (1 + 1) / 2.0 = 1 = 20 = 2height(t). Inductive Case. Let k be any integer >= 0, and assume that S0, S1, … Sk are true. We need to show that Sk+1 is true. Let t be a non-empty binary tree of height k + 1 Because t is full, both leftTree(t) and rightTree(t) are full with height less than k+1, so the induction hypothesis applies to both leftTree(t) and rightTree(t). We have (n(leftTree(t)) + 1) / 2.0 = 2height(leftTree(t)) and (n(rightTree(t)) + 1) / 2.0 = 2height(rightTree(t)). The number of elements in t is one more than the number of elements in leftTree(t) and rightTree(t), and the height of those subtrees is one less than the height of t. So we get (n(t) + 1) / 2.0 = (n(leftTree(t)) + n(rightTree(t)) + 2) / 2.0 = (n(leftTree(t))+ 1) / 2.0 + (n(rightTree(t)) + 1 / 2.0 = 2height(leftTree(t)) + 2height(rightTree(t)) = 2height(t)– 1 + 2height(t) – 1 = 2height(t) We conclude that Sk+1 is true. Therefore, by the Principle of Mathematical Induction, Sk is true for any non-negative integer k. .