CS 1723/1721: Project 3
Binary Trees: Traversals and Drawing
[Due 30 November 2001]

Rules: This is the third of four major individual assignments that you are required to complete in this class. You must design and code this project on your own. You can ask for debugging help, particularly from the tutors, a TA or the instructor, but this project is to be your own work. The project relies on previous work with binary trees and with queues.

Overview: The ultimate goal of this project is to draw a picture of a binary tree. (This project is similar to Problem 18.12 on pages 601-602 of your text.) To help with this, you will use tree traversals to calculate numbers related to the placement of tree nodes. You will also use traversals to draw the tree itself. Here are individual portions of the project:

  1. Traversals used to calculate node numbers:

    1. Use the supplied software (or other software) to create a binary tree with a String at each node, where inorder for the tree is the same as alphabetic order for the Strings. (This is known as a binary search tree. See Binary Search Tree Example.)

    2. Use an inorder traversal to calculate and store in each node the depth of that node in the tree. (You could also do this during the insertion in the previous step.) The depth is the number of tree links you need to follow from the root to get to the given node, where the root has depth of 1. (You could instead assume root has depth 0 if you wish.)

    3. Use another inorder traversal to calculate and store in the node the inorder number of each node in the tree, starting with 1 for the node coming first in inorder. This essentially gives a count of the nodes, using inorder (the same as alphabetic order) for the strings at the nodes.

    4. Use yet another traversal, this time a level-order traversal (see below) to calculate and store in the node the breadth of each node in the tree. Here the breadth gives a count of the nodes at a given level, from left-to-right, starting with 1. (This part is required, but is not needed to draw the tree.)

    5. Your code must also calculate the maximum depth, the maximum inorder number, and the maximum breadth.

  2. Calls to methods that will draw circles and lines to represent the tree:

    1. You should imagine horizontal lines, one for each depth number (as you go down, from depth 1 down to the maximum depth). Imagine also vertical lines corresponding to each inorder number, starting at 1 on the left and counting up to the maximum inorder number. Each node should be placed in its proper location, according to its depth (y-coordinate) and inorder number (x-coordinate).

    2. Here is an example image of a drawing of the tree. This drawing started with 19 strings: the months in order followed by the days in order, each using only 3 characters, that is, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", in this order. These were inserted into the binary search tree before depicting it.

      1. Example image that uses a separate column for each node: Inorder example.

    3. For each node in the tree, call a method placeCircle(s, row, col);, where s is the string at the node, row (the row) is the depth of the node, and col (the column) is the column number, which is the inorder number.

    4. For each line in the tree connecting two nodes, call a method placeLine(row1, col1, row2, col2);, where row1 and col1 give the row and column of the first node, and row2 and col2 give the row and column of the second node.

    5. This part can have the methods placeCircle and placeLine just print the locations of each circle and line.

  3. Creating a Java applet that will draw a picture of the tree:

    1. Write a java applet that will actually draw the circles and lines inside an applet window. You should draw the lines first. Then to draw the circle, you can first fill in a white circle (to erase that part of the line inside the circle), then a black circular line around the node, and finally the characters at the node inside the circle (if there is room).

Hand-in Requirements:

Supplied Code: You may make use of the following three programs, whose code is supplied in the links below:

More Detailed Description of the Classes and Methods to be Supplied:

Sample Input:

Development and Debug Strategy: Obviously you should do Part I above first, then Part II, and finally work on Part III. It is not expected that everyone will complete all parts, so if you do some partial work, you should turn in listings for partial credit.

Remember: You must use a level-order traversal to calculate the breath at each node even though you do not use the breadth in your picture..

Extras for those who would like to do more than the above: Here are two examples of applets that do more.


Revision date: 2001-11-12. (Please use ISO 8601, the International Standard.)