Introduction to Algorithms, 2022-3
Week 8: Exercises

1. Complete Tree

Write a function completeTree(h) that builds a complete binary tree of height h. Every node's value should be that node's depth.

2. Mirror Image

Write a function mirror(t) that modifies a binary tree, flipping it from left to right so that it looks like a mirror image of the original tree.

3. Maximum Value

a) Write a function max_in_tree(t) that returns the maximum value in any binary tree.

b) Modify the function to be more efficient if the input tree is known to be a binary search tree.

4. Ancestor Check

Write a function isAncestor(p, q) that takes pointers to two nodes p and q in a binary tree and returns true if p is an ancestor of q.

5. Tree Experiment

Write a function that takes an integer N and inserts N random values in the range from 0 .. N – 1 into a binary search tree. Let r be the ratio of the tree's height to the minimum possible height of any tree that holds N values. The function should compute and return r.

6. Recursive Tree

Write a class TreeSet that implements a set of values using a binary search tree, with methods add() and contains(). Implement these methods using recursion, with no loops. Hint: write recursive helper functions outside your TreeSet class that work with Node objects.

7. Most Common Word

Write a program that reads a text file and prints the most common word in the file, along with its count of occurrences. Assume that words are sequences of Latin letters (A .. Z, a .. z), and ignore case. Do not use a Python dictionary. Instead, use a binary search tree to store the word counts.

8. Tree Range

Write a method that determines how many values v in a binary search tree of unique integers fall in the range a ≤ v ≤ b, where a and b are function parameters. The function should run in time O(h + n), where h is the tree height and n is the number of integers in the range.

9. Biggest Gap

Write a function that returns the greatest difference between any two consecutive values in a binary search tree.

10. Tree Successor

Write a method which, given a binary search tree of non-negative integers and an integer i, returns the next highest value in the tree, i.e. the smallest value in the tree that is greater than i. Return -1 if there are no values greater than i. Assume that all integers in the tree are unique. The value i may or may not be present in the tree. Your function should run in time O(h), where h is the height of the tree.

11. Tree Compare

Write a function equal(t, u) that takes two Node objects representing the roots of two binary trees. The function should return True if the binary trees are identical, i.e. they have the same structure and the same values in corresponding nodes.

12. Rebalancing a Tree

Write a function that takes a binary tree and constructs a second tree that contains all the value from the first tree and is as balanced as possible.

13. Tree Check

Write a function that takes a binary tree and returns True if the tree satisfies the ordering requirements of a binary search tree.

14. Complete Tree

Write a function that takes a binary tree and returns True if the tree is complete.