CS241: Spring 2015 -- Lecture 23, Exam feedback, binary tree, display and heap

  1. Exam feedback
    1. What's a 65 anyways?
    2. How much material have we covered; how long is break?
  2. Review: trees and BST and tree sort
  3. Writing BinaryTree, and GenericBinaryTree (continued)
    1. As always
      1. representation: root, right, left
      2. operations:
        1. constructors
        2. accessors
        3. isEmpty()
        4. toString - plain & recursive,
          1. how to make it look like (anything like) a tree?
          2. recursion to the rescue! add an indent parameter
        5. How to paint a tree (instead of printing it sideways)
          1. write void paint(Graphics g, int x, int y, int w, int h), or void paint(Graphics g, Rectangle r)
          2. if (!empty) // avoid infinite recursion
                draw the root in the middle, near the top
                left.paint(g, the rectangle which is the left half of the remainder of r, below the root)
                right.paint(g, the rectangle which is the right half of the remainder of r, below the root) 
                            
            Like this:
          3. Two Component methods JFrame inherits
            • Rectangle getBounds()
            • Insets getInsets()
      3. Testing before use!!
    2. Making a generic BinaryTree class
      1. Another kind of abstraction: the pointed brackets enclose another kind of parameter
        class BinaryTree<T> {
        
            T root;
            BinaryTree<T> right, left;
        
            BinaryTree() {
        
            }
        
      2. Just like ArrayList, now we can specify any type for the root (but watch out! anything you do with the root has to be doable by anything T can be!)
  4. Binary tree methods: int ht(), int countRoots(), int countLeaves(), countInteriorNodes(), traverse() (preorder/inorder /postorder)
  5. BSTs - what could go wrong? Comparing Objects? Degeneracy!
  6. Heaps - a solution to the worst case of a BST
    1. always has a near-delta shape (no degeneracy!)
    2. every root is <= both children's roots, thus the smallest element is at the root
  7. Heap methods
    1. insert
      	insert new element at the insertion point (first empty slot, to the right of the step on the near-delta)
      	swap up to it's proper place (to preserve heapness)
      	i.e.
      	if ! at root of heap
      		if it is < its parent (i.e. is out of place) {
      			swap roots with parent
      			recurse on parent
      		}	    	
                      
      running time?
    2. delete root
      	save the root (to return)
      	swap from rightmost, bottom row (to the left of the step on the near-delta) and trim the old root
      	swap the root down to the proper place
      	i.e.
      	if ! a leaf
      		if it is > either child (or just the left, if there's no right) {
      			swap roots with smaller child
      			recurse on that child
      		}	    	
                      
      running time?
  8. representation; an array!
  9. heapsort
    	for each element in the list
    		insert in the heap
    	list.clear();
    	while !heap.isEmpty()
    		list.add(heap.deleteRoot());