CS241: Spring 2017 -- Lecture 16, tree, table and radix

  1. 2 things about merge sort
    1. Why is the MergeSort in examples from class O(n^2)?
    2. How to fix it?
  2. Write a method which is passed an int[] list, and two indices, firstI, and lastI; it should reverse all the values between firstI and lastI, inclusive. E.g. if the initial list were: 1, 2, 3, 4, 5, 6; firstI=2, lastI=4, the result should be: 1, 2, 5, 4, 3, 6
  3. Table sort (also known as counting sort) pseudocode (repris)
    	tableSort(List aList) {
    		//count em
    		create an array of counters
    		for each element, e {
                        counter[e]++
    		}
    		// spew
    		clear aList
    		for each counter (counter[i]) {
                        emit that many i's (i.e. insert that many into aList)
    		}
    	}
            
  4. 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) {
                    for each element, e of aList{
                        qs[value at d].enqueue(e);
                    }
    
                    clear aList
                    for each queue from 0-9 {
                        while (!queue[i].isEmpty()) {
                            aList.add(queue[i].dequeue());
                        } // while
                    } // for each q
                } // for each digit
    	} // radixSort
            
    A question: Is this easier to understand as:
    	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
            }
            
  5. Proof by induction
    1. But first, the natural numbers (N)!
      1. 1 ∈ N
      2. n ∈ N => n+1 ∈ N
    2. To prove some proposition P(n) for all n
      1. Establish the base case - P(1)
      2. Show inductive step - P(k)=>P(k+1)
  6. Binary trees (repris)
    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.
  7. 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!).
  8. BST insert pseudocode
    
    void insert(Integer insertMe) {
        if (isEmpty())  // base case
            setRoot(insertMe);
        else if (belongsOnTheRight(insertMe))
    	right.insert(insertMe);
        else left.insert(insertMe);
    }
    
  9. Tree sort - given an unsorted list