Exercises - Lecture 23

Goals

Example 2-3 Tree:
23tree

23A

  1. Given the example 2-3 tree above, which of the following changes could be made while keeping it as a 2-3 tree?

    1. Move 50 into its parent and 52 into the first key slot of the same node
    2. Move 15 into the node containing 12 and remove the leaf that previously contained 15
    3. Move 32 down to the node containing 48, shifting the 48 and its child pointers to the right by one spot
  2. Starting from an empty 2-3 tree, insert the following numbers in the order given. Draw the tree after each insertion.

    18, 30, 48, 17, 35, 22, 50

Problems - Lecture 23

Consider implementing a 2-3 tree. Let’s represent our 2-3 tree nodes as follows:

class Node {
  Node c1; // left subtree, all values < k1
  int k1;  // left key
  Node c2; // middle subtree, k1 < values < k2
  int k2;  // right key
  Node c3; // right subtree, k2 < values
  int degree; // number of keys; must be either 1 or 2
}
  1. Write some pseudocode for a search method that returns whether a key is in the tree:

    /** Return true if key is in the 2-3 tree rooted at n. */
    boolean search(Node n, int key) {
      // your implementation here
    }
  2. Write some pseudocode for an insert method with the following spec; Java doesn’t support multiple return values, but let’s pretend we can return a 2-tuple.

    /** Insert the given key into the 2-3 tree rooted at Node n.
    Pre: n is the root of a 2-3 tree, or null for an empty tre.
    Returns the root of the updated tree and a boolean:
    - If the boolean is true, then the returned node is a degree-1 node resulting
      from a splitting operation; the new split nodes are already attached as its
      children, and the node value being "kicked up" and needs to be added into its
      parent
    - If the boolean is false, no further changes to the tree structure are needed
    */
    (Node, boolean) insert(Node n, int key) {
      // your implementation here
    }