Link

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. What is the best case BST height in terms of N? Worst case?
  2. If shuffling yields log N tree height (with extremely high probability), why don’t we simply shuffle our input data before building our BST to avoid worst case behavior?
  3. [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.
  4. [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, 7, 53
    • 1, 10, 2, 9, 3, 8, 4, 7, 6, 5
    • 2, 7, 3, 8, 4, 5
    • 1, 2, 10, 4, 8, 5
  5. Give an example of a key for which there is no natural compare method.
  6. Do there exist any objects for which it is impossible to define a total order? In other words, can we always write a compare method if we’re willing to do the work, or are some data types fundamentally impossible to compare other than for equality?
  7. When we delete from a BST, we always chose either the predecessor or successor as a replacement for the root. We said that these two items always have zero or one children. Why?
  8. Is the delete operation commutative? In other words, if we delete X, then Y, do we always get the same tree as if we delete Y, then X?

  9. Q1a from CS 61B 15sp MT2
  10. Q1a from CS 61B 16sp MT2
  11. Q1c from CS 61B 16sp MT2
  12. Q7 from CS 61B 16sp MT2
  13. Q1b from CS 61BL 16su MT2
  14. Q1a, Q1d, Q1f from CS 61B 18sp MT2
  15. Q5 from CS 61B 18sp MT2
  16. Q1 from CS 61B 19sp MT2