Lab - Building a Binary Tree

Preface

This lab is quite different from others. You will not be writing any lines of code, and you will not be turning in any printouts. Instead, you will be required to solve a few puzzles and to demonstrate your success in person with one of the course TAs. (Don't worry - you will be writing plenty of code other weeks!)

Overview

In lecture and in the text, a claim has been made that the expandExternal(v) method can be used to create any proper binary tree, when starting with an initial tree consisting of a single node. Let's put this claim into action!

We are providing you with an applet which draws two binary trees on the screen. The first tree is generated at random by the program. The second tree begins as a single node. You must build this second tree so that it looks identical to the first tree. In particular, if you click the mouse on an external node of the second tree, the program will execute expandExternal on that node. You must find a sequence of such clicks to meet your goal.

To get full credit for completing this lab, you must do the following:

The Applet

The applet is based on a modification of the code given in Chapter 6 of the text for displaying a binary tree. Fortunately, you do not need to see the sourcecode, you simply can run the applet. The interface for the applet is quite simple. The program starts with an initial tree based on a default number of external nodes. However there is a text box for entering the number of external nodes, and a button which can be clicked to start over with a new tree of that size. Each time you hit the button, a new (random) tree is generated. To make things easy to see, the nodes are coded in the following way. Internal nodes are displayed as red circles; external nodes are displayed as black squares. As you add nodes to the bottom tree, the tree will get redrawn on the screen, often in a slightly different position so as to better balance the drawing.


There are three different ways to start the applet:

  1. The applet can be viewed with a web browser by going to the URL www.cs.luc.edu/~mhg/demos/BuildTree/

  2. The applet can be viewed from a DOS window using the appletviewer program which is part of Sun's JDK. To do this from the department labs, open a DOS window from the "Programming (java) --> jdk (dos)" menu, and do the following:
    DOSprompt>  e:
    DOSprompt>  cd prof_pub\mhg\comp271\labs\buildtree\
    DOSprompt>  appletviewer BuildTree.html
    

  3. If you wish, you can actually download the sourcecode yourself and then compile them in your favorite Java environment. The files can be downloaded from the web page or found on the department network, at:
       e:\prof_pub\mhg\comp271\labs\buildtree\files\
    


Last modified: 17 October 2002