CS 112 Lab 6

Agenda

Assignment 4 overview

Assignment 4 asks you to display a binary search tree using breadth first (also called level order) traversal.

The BST obtained by inserting numbers from a JSON string "[ 8, 4, 2, 1, 3, 6, 5, 7, 9 ]" using this traversal is shown below,

Today's lab overview

In today's lab, we will use a simpler version using preorder traversal to display a tree. The methods we use in the lab will help you in displaying your tree for the homework assignment. We will display the tree using both a graphical window and via text to the console.

An example of a randomly generated tree displayed via preorder traversal is shown below,


Graphical preorder display


Text preorder display

Today's example and associated tasks

Download class files for the lab below,

  • DisplayTree.java

    This class defines a tree datastructure, generates a random tree and includes associated methods to display the tree. The method preorderDisplayGraphics() is given in the class. You are required to fill in the preorderDisplayText() method to display the tree as shown above. See comments in this method for hints to help in displaying the tree correctly.

    Task: fill in the preorderDisplayText() method to display the tree using text.

  • Driver.java

    This is the client driver class to test our DisplayTree. It open a graphics window for displaying the tree and adds a callback for mouseClick that prompts the user to enter a JSON string.

Solutions

DisplayTree.java

Driver.java

URL

http://cs-people.bu.edu/tvashwin/cs112_spring08/lab06.html