Topics: Binary Search Trees - Theory and Practice
The assignment is in two parts. In the first part, you code and demonstrate the binary search tree operations. The second part consists of some exercises on binary search trees.
How to Test. You will be testing your tree operations on files I provide which contain very simple commands invoking the various tree operations. These operations are:
| c | create an empty tree |
| i key | insert node with key key into the tree |
| d key | delete node with key key from the tree |
| p | do inorder tree walk, printing keys compactly (horizontally) |
| s key | search for node with key key in the tree |
| S | display (Show) the tree structure |
| # | echo the comment line |
As in the previous assignment, you need to provide a simple command interpreter to read and parse these commands. It need not be super robust and full of features; it only needs to be able to minimally interpret these commands and dispatch control to the appropriate procedure. You might want to start with the command interpreter you used for the last assignment. As before, the code for this interpreter should be in a separate module from the code for your binary search tree operations.
A Test Example. The following is an example of a set of test
commands:
| # create tree |
| c |
| # insert 10 items |
| i 372 |
| i 245 |
| i 491 |
| i 474 |
| i 440 |
| i 122 |
| i 418 |
| i 125 |
| i 934 |
| i 752 |
| # Print nodes in order |
| p |
| # Search for 474 and 817 |
| s 474 |
| s 817 |
| # Show tree structure |
| S |
| # Delete 372 and 555 |
| d 372 |
| d 555 |
| # Show structure again |
| S |
The following shows how your program script should look when running this test. Lines beginning with '#' are simply echoed comments from the set of test commands. Other lines are output by the tree operations.
Test Script
+-------------------------------------------------+
|# create tree |
|# insert 10 items |
|# Print nodes in order |
| |
|122 125 245 372 418 440 474 491 752 934 |
| |
|# Search for 474 and 817 |
| |
|Search key 474 found |
| |
|Search key 817 not found |
|# Show tree structure |
| |
|Structure of tree (rotated 90 degrees to left): |
| |
| 934 |
| 752 |
| 491 |
| 474 |
| 440 |
| 418 |
| 372 |
| 245 |
| 125 |
| 122 |
| |
|# Delete 372 and 555 |
| |
|Search key 555 not found |
|# Show structure again |
| |
|Structure of tree (rotated 90 degrees to left): |
| |
| 934 |
| 752 |
| 491 |
| 474 |
| 440 |
| 418 |
| 245 |
| 125 |
| 122 |
+-------------------------------------------------+
Notice that informative messages are given by the search operation, and also
by the delete operation when the key to be deleted is not found.
For the tests that you hand in, commands will be processed in batch mode (i.e., read from files). However, you may want to design the command interpreter so that it can read commands from standard input as well so that you can more conveniently test simple operations in the early stages.
Displaying The Structure Of The Tree.
You can use recursion to display the structure of the tree if the
tree is oriented on the page (or screen) with the root at the far left
and children to the right. The algorithm is:
ShowTree(x, depth) |> x points to root of subtree with given depth
if x not = NIL
then ShowTree(right[x], depth+1)
print key[x] right-justified in a field of (depth * 6) + 4
ShowTree(left[x], depth+1)
The procedure is originally called with
ShowTree(root[tree], 0).
What To Hand In. Hand in typescripts like the one given above for the test files:
Identify each script clearly and be sure to hand-draw the tree links for all displayed trees. Also, hand in well-documented code for both your command interpreter and your tree operations, in separate modules.