The goals of this lab include doing the specific tasks outlined below
and understanding general concepts behind the tasks.
This lab assignment is a study of binary search trees. A binary search
tree is organized so that one can search for information (via a search
key) using binary search. Each node in a binary search tree contains the
key (info field), and two pointers to subtrees (or children).
The top node of a tree is called the root. Nodes that have no
children are called leaves. A path is a connected set
of nodes within the tree.
To make searching easier in a binary search tree, it must have the
binary search tree property which is the following:
``Let x and y be nodes in a binary search tree. If y is in the
left subtree of x, then the key of y <= the key of x. If y is in the
right subtree of x, then the key of x <= the key of y.''.
Lab 9 table of contents
In this lab, there are three classes that you will be working with. The first class, TreeList, implements the functionality of a binary search tree of strings. The second class, Display, issues commands to the animator, samba, so that you can physically see what each operation on the tree is doing. The last class, Stack, will be used to display different paths along the tree.
The first part of the lab gives you a chance to experiment with the binary search tree using samba. You will use the add, delete, and search functions. You will also create files so that the order in which the nodes are inserted into the binary search tree
In the second part of the lab, you will make the following modifications to tree.cc:
In this section of the lab you'll copy files and then compile and run the usetree program using the samba animator.
First change into your cps100e subdirectory (type pwd
to verify where you are). Create a
lab9 subdirectory by typing mkdir lab9 and change
into this subdirectory (be sure to check that you're
in the lab9 subdirectory.) Now copy the files for the lab
(don't forget the . when copying).
You should see the files listed below (these are links to the files in case you use Netscape, and for users outside of Duke).
Be sure you're in the lab9 subdirectory, and check to see that all files are there (type ls). Then, from an xterm window (at the prompt [1] ola@teer8% or similar) compile the first version of the program by typing: make usetree. This should compile several files and link them together with the library libtapestry.a. Now run the program by typing: usetree | samba at the prompt. Two samba windows should pop up. Move the window that is named Tree_0 somewhere out of the way, but keep it visible. This is where the animation will take place. Click on the start button in the Polka Control Panel window. Then go back to the xterm window and use the menus to run usetree. When you are finished, you must quit usetree (by typing 'q' and hitting the return key) and quit samba by clicking on the Quit button in the Polka Control Panel window.
In this part of the lab, you will read a tree from a file, you will observe what happens when you insert nodes into the tree, delete nodes from a tree, and search for a node in a tree. You will use options from the usetree menu to do this.
Do the following operations:
Now using emacs create a file called test1.dat which contains 4 words in it (one per line). When usetree reads in this file the tree should look like the following:
You should also create a file called test2.dat using the same words as in test1.dat except in a different order such that the tree the new file produces looks like following:
Finally, you should create a file called tree3.dat which contains 7 words. It should produce a fully "balanced" binary tree (a tree that has the same number of nodes in each subtree). It should look like the following:
In this section you will implement the DisplayStack function in tree.cc. You will use then modify the ComputeHeight function so that the path from the root to the "deepest node" in the tree is stored in a stack which is then displayed by DisplayStack. Finally, you will modify the ComputeDiameter function so that it will store the path of the diameter of the tree in a stack.
Implement the function DisplayStack in tree.cc. It takes two parameters: Stack<TreeNode *> stack, a stack of pointers to TreeNode structures, and Display & display, the object that issues the samba commands for the tree. Use a while loop that does the following for each node in the stack:
After you have changed the color of every node in the stack, prompt the user to hit the return key (Note: Use cerr instead of cout for prompting the user. Any data that goes to cout gets sent to samba rather than to the user screen)
Finally, write another while loop that sets every node that is in the stack back to the default color. The member function SetNodeDefaultColor in the Display class will be used to do this. You can see the Inorder Traversal menu option to test your code. This member function calls DisplayStack to display an inorder traversal of the binary tree.
Hint: You will need to make a copy of the stack that is being passed into the DisplayStack function.
The height of a tree is the maximum number of nodes from the root of the tree to a leaf or a tree.
In this section of the lab, you will modify the ComputeHeight function so that it returns a Stack of pointers to TreeNode structures that contains the path from the root node to the deepest leaf in the tree. The code will look like the following:
Note: The TreeList member function ComputeTreeHeight that calls ComputeHeight will also have to be modified. ComputeTreeHeightIt should store stack.Size() as its return value, then call DisplayStack to display the path from the root to the leaf node.
Modify the ComputeDiameter function so that it returns a Stack of TreeNode pointers that contains the longest path between two leaf nodes in the tree. The new function prototype will look like the following:
submit100e lab9.N README tree.cc test1.dat test2.dat test3.dat
You can enter the files in any order. Remember that every assignment
must have a README file submitted with it (please use all capital
letters). Include your name, the date, and an estimate of how long
you worked on the assignment in the README file. You must also
include a list of names of all those people with whom you collaborated
on the assignment.
You also must turn in the inlab questions either by turning in the sheet during lab or by submitting the answers with your README.
Add a new member function SaveTree for the TreeList class. This member function saves the words in a tree to a file. The function should be declared inside the TreeList class in tree.h:
After the file has been saved, when usetree loads this file, the tree should be exactly the same as the tree was when it was saved. You will need to modify usetree.cc so that your save function is called. You can do this by either adding a Menu option to save the tree, or when the user quits usetree, you ask the user for the name of a file which to save the tree to. Use the provided PromptString function for user input.
To submit the extra credit, run the command below, but substitute your section for N.
submit100e lab9.N.xtra tree.cc tree.h usetree.cc