CS 241 Data Structures- Lab 7, Due 3/30 (right after break)

Feel free to work in pairs again; tell me when you demo who your partner is (so I can check them both off).

Tree Sort, via a Binary Search Tree

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 (hey! a recursive definition!).

Implement a binary search tree that stores ints (using a BinaryTree that stores Integers as the data structure); and then use that to implement and time tree sort. Compare the running times with those of merge or quick sort whichever sort you implemented.

To aid debugging, display the BST visually so it looks at least vaguely like a tree. If the inputs to your tree were: 10, 1, 3, 88, 100, 77, then displaying  10, 1, 3, 88,77, 100 would not be sufficient. Instead, it would be much nicer to display something like:

                100
        88
                77
10
                3
        1

(If you don't see that as a tree, try tipping your head to the left, think of 10 as the root, and use your imagination.) This is straightforward, if you understand two things.

  1. Display should be recursive; if you wish the right subtree to display first and then the root, and then the left subtree (as in the example) you should organize display() as:
    void display() {
        if (!empty()) { // i.e. if not the base case (where we do nothing!)
            display right
            display root
            display left
        }
    }
  2. Second (as demonstrated in class), you can achieve the indentation by passing the current indent level as a parameter to display. I.e. myTree.display(""). Then each recursive call to String display(String prefix) adds to that indentation, returnMe += left.display(prefix+"   ") to move right 3 more spaces for the next level of the tree.

Extra Credit:

Display using Graphics (as explained in class) instead of sideways with text.

Sort Timing

Once you have implemented tree sort (and know it works by displaying the trees as above), then implement heap sort, and time them on various sized lists. Do heap sort second, as only tree sort is really due Monday (we can slip heap sort to Weds).

Approach

Use your feature which disqualifies sorts as soon as they become too slow, say 10 seconds? Once disqualified they should simply print "slow!"
Demo your code (on *all* the sorts you've written!) during lab for credit. Do the cosmetic work so that the table prints nicely (i.e. all the columns line up, descriptive headings, space between the columns...). Include a simple way to display the trees without having to recompile.

Extra credit

Add a feature which analyzes the T(n):T(2n) ratios and on that basis estimates Θ for each. I.e. at the bottom of the column of times, print n^2, or nLogn, or n.