CS241: Spring 2017 -- Lecture 18, tree and radix implementations

  1. Radix sort
    1. Radix sort pseudocode
      	radixSort(List aList) {
                  create an array of queues - Queue[] qs = new Queue[10];
                  initialize each Queue (! better than 10 null ptrs!)
      
                  for each digit, d (from smallest to largest significance, i.e. 1, then 10, etc) {
                      listToQueues() 
                      clear aList
                      queuesToList() 
                  } // for each digit
      	} // radixSort
      
              listToQueues() {
                  for each element, e of aList{
                      qs[value at d].enqueue(e);
                  }
              }
      
              queuesToList() {
                  for each queue from 0-9 {
                      while (!queue[i].isEmpty()) {
                          aList.add(queue[i].dequeue());
                      } // while
                  } // for each q
              }
                      
    2. Theoretical running time
    3. Empirical running time
    4. What's the problem? How to fix it??
  2. Binary trees (repris) 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.
  3. 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!).
  4. BST insert pseudocode
    
    void insert(Integer insertMe) {
        if (isEmpty())  // base case
            setRoot(insertMe);
        else if (belongsOnTheRight(insertMe))
    	right.insert(insertMe);
        else left.insert(insertMe);
    }
    
  5. Tree sort - given an unsorted list
  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
    3. 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?
    4. representation; an array!
    5. heapsort
      	for each element in the list
      		insert in the heap
      	list.clear();
      	while !heap.isEmpty()
      		list.add(heap.deleteRoot());