Description
Problem Set 6
Binary Search Tree (BST)
- Given the following sequence of integers:
10, 17, 3, 90, 22, 7, 40, 15
- Starting with an empty binary search tree, insert this sequence of integers one at a time into this tree. Show the final tree. Assume that the tree will not keep any duplicates. This means when a new item is attempted to be inserted, it will not be inserted if it already exists in the tree.
- How many item-to-item comparisons in all did it take to build this tree? (Assume one comparison for equality check, and another to branch left or right.)
- For the tree built in the above problem:
- What is the worst case number of comparisons for a successful search in this tree? For an unsuccessful (failed) search? (Assume one comparison for equality check, and another to branch left or right.)
- What is the average case number of comparisons for a successful search in this tree?
- From this tree, delete 17: find the node (y) that has the smallest value in its right subtree, write y’s value over 17, and delete y. How much work in all (locating 17, then locating y , then deleting y) did it take to complete the deletion? Assume the following (a) you are using two pointers to navigate down the tree, a tracking pointer, and a lagging pointer, (b) 1 unit of work for an equality comparison between target and tree item, one for an inequality check to branch left or right, and 1 unit of work for a pointer assignment.
- Given the following BST node class:
public class BSTNode<T extends Comparable<T>> {
T data;
BSTNode<T> left, right;Â Â Â Â Â Â Â Â public BSTNode(T data) {Â Â Â Â Â Â Â Â Â Â Â Â this.data = data; Â Â Â Â Â Â Â Â Â Â Â Â this.left = null;Â Â Â Â Â Â Â Â Â Â Â Â this.right = null;
}
public String toString() {Â Â Â Â Â Â Â Â Â Â Â Â return data.toString();
}Â Â Â }
Consider the following method to insert an item into a BST that does not allow duplicate keys:
public class BST<T extends Comparable<T>> {
BSTNode<T> root;Â Â Â Â Â Â Â Â int size;
…Â Â Â Â Â Â Â Â public void insert(T target)Â Â Â Â Â Â Â Â Â throws IllegalArgumentException {
BSTNode ptr=root, prev=null; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c=0;
while (ptr != null) {
c = target.compareTo(ptr.data);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (c == 0) {
throw new IllegalArgumentException(“Duplicate key”);
}Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â prev = ptr;
ptr = c < 0 ? ptr.left : ptr.right;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
BSTNode tmp = new BSTNode(target);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â size++;
if (root == null) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â root = tmp;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return;
}Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (c < 0) {
prev.left = tmp;
} else {
prev.right = tmp;
}Â Â Â Â Â Â Â Â }
Write a recursive version of this method, using a helper method if necessary.
- * With the same BSTNode class as in the previous problem, write a method to count all entries in the tree whose keys are in a given range of values. Your implementation should make as few data comparisons as possible.
// Accumulates, in a given array list, all entries in a BST whose keys are in a given range,    // including both ends of the range – i.e. all entries x such that min <= x <= max.     // The accumulation array list, result, will be filled with node data entries that make the cut.
// The array list is already created (initially empty) when this method is first called.
public static <T extends Comparable<T>>
void keysInRange(BSTNode<T> root, T min, T max, ArrayList<T> result) {
/* COMPLETE THIS METHOD */
}
- With the same BSTNode class as in the previous problem, write a method that would take a BST with keys arranged in ascending order, and “reverse” it so all the keys are in descending order. For example:
25Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 25
/Â Â Â \Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /Â Â Â \
10Â Â Â Â 40Â Â Â Â Â —>Â Â Â Â Â Â Â 40Â Â Â Â Â 10
/Â \Â Â /Â \Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /Â \Â Â Â /Â \
2Â Â 20 30Â Â 45Â Â Â Â Â Â Â Â Â Â Â Â 45Â 30Â 20Â Â 2
/Â Â Â \Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /Â Â Â Â \
15Â Â Â 35Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 35Â Â Â Â 15
The modification is done in the input tree itself, NO new tree is created.
public static <T extends Comparable<T>>Â Â Â Â Â void reverseKeys(BSTNode<T> root) {
/* COMPLETE THIS METHOD */
- * A binary search tree may be modified as follows: in every node, store the number of nodes in its right subtree. This modification is useful to answer the question: what is the k-th largest element in the binary search tree? (k=1 refers to the largest element, k=2 refers to the second largest element, etc.)
You are given the following enhanced binary search tree node implementation:
public class BSTNode<T extends Comparable<T>> {
T data;
BSTNode<T> left, right;
int rightSize;Â // number of entries in right subtree
…
}
Implement the following recursive method to find the k-th largest entry in a BST:
public static <T extends Comparable<T>> T kthLargest(BSTNode<T> root, int k) {
/* COMPLETE THIS METHOD */
}