Lecture 9 - Problems

For reference, here’s the basic BST class from the lecture videos (for simplicity, this one doesn’t keep parent pointers):

/** BST: a binary tree, in which:
 * - all values in left are < value
 * - all values in right are > value
 * - left and right are BSTs */
public class BST {
  int value;
  BST left;
  BST right;
}
  1. The BST removal method presented is recursive: it finds the in-order successor, copies its value in place of the value to be removed, then recursively removes the successor’s original node. Step 3 of the four-step procedure for understanding recursive methods requires that we verify that each recursive call makes progress towards the base case. Prove that the recursive removal of the successor node makes progress towards the base case.

  2. What is the runtime of BST removal?

  3. Implement the following constructor for the BST class above, which takes a sorted array and builds a binary search tree that can be searched in O(\log n) time. In other words, it constructs a tree that achieves the best-case runtime of BST search. Hint: this is best done recursively!

  4. Suppose you are constructing a BST containing numbers 1 through 100. You don’t get to choose the insertion order, but you know that it will be random in the sense that every number is equally likely to be the next inserted value. In this scenario, if the number of nodes currently in the tree is n, what do you think is the expected-case (average-case) runtime of each call to insert?