CS 241 Data Structures- Lab 7

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:


(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.

Demonstrate your program for credit;