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

  1. Administrivia
    1. Exam 3/13!
    2. Review sheet up
    3. Jury duty for me 3/12
    4. Next week is review
    5. Hard deadline for Monday lab (so you can have time to study/review)
  2. Running times: why Θ is better than O! And why it's not entirely simple.
    1. Best case
    2. Worst case
    3. Average case
  3. Finishing up fib(1000) -- a homemade BigInt class (Java has BigInteger, but it's not difficult to write your own; often a choice -- learn to use a built in class, or write your own)
    1. arbitrarly big ints (well, at least until we run out of memory!)
    2. representation: list of digits. Biggest first? Smallest first?
    3. operations:
      1. new
      2. add(BigInt)
      3. toString
    4. Testing before use!!
  4. Tired of linear strutures? Time for a recursive structure. Namely, Binary Tree
    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.
  5. 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!).
  6. BST insert pseudocode
    
    void insert(Integer insertMe) {
        if (isEmpty())  // base case
            setRoot(insertMe);
        else if (belongsOnTheRight(insertMe))
    	right.insert(insertMe);
        else left.insert(insertMe);
    }
    
  7. Tree sort - given an unsorted list