Exercises - Lecture 9

9A

  1. Which of the following are binary search trees?

  2. Write the values printed by an in-order traversal of each of the following BSTs. What do you notice about the resulting sequences?

  3. Draw the binary tree resulting from the following sequence of insertions:

    t = new BST();
    t.insert(10)
    t.insert(15)
    t.insert(16)
    t.insert(8)
    t.insert(16)
    t.insert(9)
    t.insert(11)
    t.insert(-1)
  4. Draw the binary tree resulting from the following sequence of insertions:

    t = new BST();
    t.insert(-1)
    t.insert(8)
    t.insert(9)
    t.insert(10)
    t.insert(11)
    t.insert(15)
    t.insert(16)
    t.insert(16)
  5. Implement the minimum method in the following BST class, which finds the smallest value in a BST:

    /** 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;
    
      /** Returns the minimum value in BST n.
        * pre: n is not null */
     public static int minimum(Node n) {
       // your code here
     }
    }

9B

  1. T / F Searching a binary search tree has a better worst-case runtime than searching a linked list.
  2. T / F Searching a binary search tree has a better best-case runtime than searching a linked list if the element is not in the list/tree.

9C

  1. Perform the following operations in sequence on the tree given below. Draw the resulting tree after each operation.

remove(9)
remove(4)
remove(10)