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}
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.
What is the runtime of BST removal?
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!
/** Construct a BST containing the elements A[start..end] that is balanced,
* i.e. can be searched in O(log n) time.
* Precondition: values is a sorted array. */
public BST(int[] values, int start, int end) {
// your code here
}
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
?