Sets, Maps, and BSTs Study Guide

Trees. A tree consists of a set of nodes and a set of edges that connect these nodes. As there exists only one path between any two nodes, there are no cycles in a tree. If a tree is rooted, every node except the root has exactly one parent. The root has no parents, and a node with no children is considered a leaf.

Binary Search Trees. A Binary Search Tree (BST) is a rooted binary tree that maintains several key conditions to help optimize search. For a node X, every key in the left subtree is less than X’s key and every key in the right subtree is greater than X’s key. This aids with operations such as search since when we look for the position of a key, we can move left or right within our tree starting from the root depending on how our key compares to the key of each node.

Runtime. BST’s help optimize our search so we do not always have to look at every single element in our tree when searching for a particular key. But how much does this optimize things? For a BST that is “bushy”, we can search in O(log N) time where N is the number of nodes. For a BST that is “spindly”, our search will take O(N) time. This is because search time depends on the height of our tree, where a bushy tree has a height of log N and a spindly tree has a height of N.

  1. [Adapted from Textbook 3.2.3] Give two orderings of the keys A X C S E R H that, when inserted into an empty BST, yield the best-case height. Draw the tree. Then, show the result of deleting the root from this tree.
  2. [Adapted from Textbook 3.2.4] Suppose that a certain BST has keys that are integers between 1 and 10, and we search for 5. Which sequence(s) of keys below are possible during the search for 5?
    • 10, 9, 8, 7, 6, 5
    • 4, 10, 8, 6, 5
    • 1, 10, 2, 9, 3, 8, 4, 7, 6, 5
    • 2, 7, 3, 8, 4, 5
    • 1, 2, 10, 4, 8, 5
  3. Q3a from CSE 373 19au MT (Solution)
  4. Q2a from CSE 373 19au Final (Solution)
  5. Q1a, Q1c from CS 61B 16sp MT2 (Solution)
  6. Q1 from CS 61B 19sp MT2 (Solution)