  ## Review Exercises (Set B2)

1. Show the traces for each sort requested below when applied to the array shown.

{ 'b', 'f', 'c', 'h', 'a', 'e', 'g', 'd' }

1. Merge Sort
2. Quick Sort (without an initial shuffle)
2. The recursive form of the cost function for the quick sort in its worst case is given by $C(n) = C(n-1) + n$, and $C(1) = 0$.

1. Show how to solve this recurrence relation to derive the Big-O cost for the quick sort in the worst case.

2. In the recursive form of the cost function, describe what the two terms C(n-1) and n count.

3. Give the order in which the nodes of the following tree are visited when visited in:

1. Pre-Order
2. In-Order
3. Post-Order 4. If nodes are added to an empty binary search tree with the following keys and in the following order, draw the tree that results.

4, 1, 2, 7, 9, 3, 8, 9, 3, 5

5. Consider the following list: K, C, H, -, I, M, X, - , A, E, Y, -, and an initially empty priority queue implemented with a heap (where the highest priority is given to the letter closest to "Z" in the alphabet). Working from left to right, if the element is a letter, it is inserted into the priority queue -- if it is "-", the highest priority element is removed. Show the state of the heap in array form after each insertion/removal.

6. The keys D A, B, G, I, C, H, J, K, E, are inserted into an initially empty red-black tree in that order. Draw the structure of the tree after each insertion.

7. Draw an example of a binary search tree with 7 nodes/keys where the average number of comparisons needed to find any key in the tree is maximized.

8. Draw an example of a binary search tree with 15 nodes/keys where the average number of comparisons needed to find any key in the tree is minimized.

9. For a binary search tree where the keys have been inserted in random order, what is the Big-O form for the average number of comparisons needed to find any given key?

10. Draw the three trees below that would result upon Hibbard deletion of the node with key 1, 5, and 11, respectively. 11. For the following questions, consider this implementation of a BST:

public class BST, Value> {

private class Node{
private Key key;
private Value val;
private Node left;
private Node right;
private int count; // number of nodes in subtree
// topped by this node

public Node(Key key, Value val) {
this.key = key;
this.val = val;
}
}

private Node root;

// You may assume the below methods with { ... } bodies have been implemented:

public void put(Key key, Value val) { ... }
// inserts a node with key-val pair into this BST, it replaces
// the associated value if the key is already in the BST

public Node put(Node n, Key key, Value val) { ... }
// inserts a node with key-val pair into the subtree of this BST
// topped by node n, and returns the (possibly new) top node for
// this subtree

public int size() { ... }
// returns the number of nodes in this tree

public int size(Node n) { ... }
// returns the number of nodes in the subtree topped by node n

public Value get(Key key) { ... }
// returns the value associated with key in this BST

public int rank(Key key) { ... }
// returns the number of nodes with keys less than the given key
// in this BST

public int rank(Key key, Node n) { ... }
// returns the number of nodes with keys less than the given key
// in the subtree topped by node n

public Key select(int rank) { ... }
// returns the key that has the given rank

public Node select(Node n, int rank) { ... }
// returns the node that has rank keys less than its associated key in
// the subtree topped by node n

public Node deleteMin(Node n) { ... }
// deletes the node with minimum rank in the subtree topped by node n
// and returns the (possibly new) top node for this subtree

public void delete(Key key) {
root = delete(root, key);  //(see part b)
}
}

1. Complete the following method (to be added to the aforementioned BST class) so that it returns the "level" of a given node n. Recall, the level of a node n is the length of the shortest path between n and the root. So the root has level 0, its children have level 1, its children's children have level 2, and so on…

public int level(Node n) {

}

2. Complete the following method (to be added to the aforementioned BST class) so that the delete method mentioned previously performs the appropriate Hibbard deletion.

private Node delete(Node n, Key key) {

}


### Solutions

1. See the following notes
2. Pre-Order: A B D E C F H I J G; In-Order: D B E A I H J F C G (note this tree is not in symmetric order, so the result is not alphabetical); Post-Order: D E B I J H F G C A
3.
          |
+--------4-----+
|              |
-1--+        +--7-----+
|        |        |
-2--+    -5-    +--9-
|           |
-3-         -8-

4.
inserting D:

|
-D

inserting A:

|
+==D
‖
-A

inserting B:

|
+--B--+
|     |
-A    -D

inserting G:

|
+--B-----+
|        |
-A     +==G
‖
-D

inserting I:

|
+=====G--+
‖        |
+--B--+    -I
|     |
-A    -D

inserting C:

|
+========G--+
‖           |
+--B-----+    -I
|        |
-A     +==D
‖
-C

inserting H:

|
+========G-----+
‖              |
+--B-----+     +==I
|        |     ‖
-A     +==D    -H
‖
-C

inserting J:

|
+--------G-----+
|              |
+--B-----+     +--I--+
|        |     |     |
-A     +==D    -H    -J
‖
-C

inserting K:

|
+--------G-----+
|              |
+--B-----+     +--I-----+
|        |     |        |
-A     +==D    -H     +==K
‖              ‖
-C             -J

inserting E:

|
+-----G-----+
|           |
+=====D--+     +--I-----+
‖        |     |        |
+--B--+    -E    -H     +==K
|     |                 ‖
-A    -C                -J

5. Answers will vary, but the "tree" should be a single linear chain (essentially a linked list, where every node has only one child, with the other reference being empty)

6. Answers will vary, but the tree must be perfectly balanced (15 nodes can fit into 4 levels), and in symmetric order.

7. $O(\ln n)$