Week 04 Tutorial
Binary Search Trees and Balanced Trees

[Show with no answers]   [Show with all answers]

  1. (Splay trees)
    1. Show how a Splay tree would be constructed if the following values were inserted into an initially empty tree in the order given:

      5 3 8 7 4
    2. Let \( t \) be your answer to part a, and consider the following sequence of operations:

      SearchSplay (t, 7);
      SearchSplay (t, 8);
      SearchSplay (t, 6);
      

      Show the tree after each operation.

    [show answer]

  2. (AVL trees)

    Answer the following question without the help of the treeLab program from the lecture.

    Show how an AVL tree would be constructed if the following values were inserted into an initially empty tree in the order given:

    12 10 8 6 4 2

    [show answer]

  3. (2-3-4 trees)

    Show how a 2-3-4 tree would be constructed if the following values were inserted into an initially empty tree in the order given:

    1 2 3 4 5 8 6 7 9 10

    Once you have built the tree, count the number of comparisons needed to search for each of the following values in the tree:

    1  7  9  13

    [show answer]

  4. Implement the following function that prints the height difference between the left subtree and the right subtree of every node in a given binary tree, (BSTree t). The function returns the height of a given binary tree. Use the following function interface:

    int printHeightDiff (BSTree t) { ... }

    [show answer]