CS241: Spring 2015 -- Lecture 19, Binary trees, Binary search trees, & tree sort!

  1. On when what perception contradicts reason: look!
  2. leftovers
    1. The inductive step...
    2. Merge?
    3. Quick?
    4. Radix?
    5. Table?
  3. Binary trees
    1. Metaphors (mixed): family tree, vegetable
    2. Terminology (even more mixed): root, leaf, sibling, parent, child, subtree, ancestor, descendant, interior node, level, depth (of a node), height (of a tree), traversal
    3. Formal Recursive Definition (suitable for memorization) - a binary tree is either empty, or it has a root and two subtrees, right and left (each is a binary tree). According to this definition every leaf has two empty subtrees! Be aware of the difference between formal use of the definition and casual description -- the latter is used for talking/thinking about binary trees, the former for writing code.
  4. A binary search tree (BST) is a binary tree with the property that every node in the left subtree is less than the root and every node in the right subtree is greater than or equal to the root. Every subtree in a BST has this property as well (recursively!).
  5. BST insert pseudocode
    
    void insert(Integer insertMe) {
        if (isEmpty())  // base case
            setRoot(insertMe);
        else if (belongsOnTheRight(insertMe))
    	right.insert(insertMe);
        else left.insert(insertMe);
    }
    
  6. Tree sort - given an unsorted list