The assignment consists of two parts. In the first part, you will implement and demonstrate the red-black tree operations. The second part consists of red-black exercises.
treetest2 commands
+------------------------------------------------+
| # create tree (done in command interpreter) |
| c |
| |
| # insert 10 items |
| i 122 |
| i 125 |
| i 245 |
| i 372 |
| i 418 |
| i 440 |
| i 474 |
| i 491 |
| i 752 |
| i 934 |
| |
| # Print nodes in order using inorder-tree-walk |
| P |
| |
| # Show tree structure |
| S |
+------------------------------------------------+
Note that since the nodes are inserted in key order, a straight binary search
tree implementation would link the nodes linearly in the equivalent of a
linked list, not a balanced tree.
This is shown below when treetest2 is used as input to an
ordinary binary tree.
treetest2 script using binary search tree insertion
+-------------------------------------------------------------+
| # create tree (done in command interpreter) |
| # insert 10 items |
| # Print nodes in order using inorder-tree-walk |
| |
| |
| 122 125 245 372 418 440 474 491 752 934 |
| # Show tree structure |
| |
| Structure of tree (rotated 90 degrees to left): |
| |
| 934
| 752 |
| 491 |
| 474 |
| 440 |
| 418 |
| 372 |
| 245 |
| 125 |
| 122 |
+-------------------------------------------------------------+
A red-black tree implementation maintains a (limited) balance:
treetest2 script using red-black tree insertion
+--------------------------------------------------+
| # create tree (done in command interpreter) |
| # insert 10 items |
| # Print nodes in order using inorder-tree-walk |
| |
| 122 125 245 372 418 440 474 491 752 934 |
| |
| # Show tree structure |
| |
| Structure of tree (rotated 90 degrees to left): |
| |
| 934(R) |
| 752(B) |
| 491(R) |
| 474(B) |
| 440(B) |
| 418(B) |
| 372(B) |
| 245(B) |
| 125(B) |
| 122(B) |
+--------------------------------------------------+
To debug your red-black tree implementation, you might want to
modify your command
interpreter to recognize commands to do left and right rotates. Even though
such commands will not explicitly be in the test files, for debugging
purposes you may want to test rotations in isolation before integrating them
into the insert and delete operations. Then you can easily check the working
of your rotation functions by doing a ShowTree
on their results.
To show a red-black tree (rotated 90 degrees to the left), modify the
ShowTree for binary search trees to also print out the color
of the node (R or B) after it
- the last exercises below ask for "upright" examples.
Note that treetest5 for this assignment was generated using random numbers, so there are a number of duplicate keys, both for insert and delete. Just insert any duplicate keys. That way when you delete any duplicate keys, each will be deleted and you won't generate any "search key not found" messages. Also, to give you an idea of the result of treetest5, you should end up with a well balanced tree of 30 nodes with black root 306.
It is not necessary to echo delete (or insert) operations (though you might want to echo them for debugging purposes - you can turn off the echoing once your program is running correctly). However you should print appropriate messages for successful and unsuccessful searches (there will be about 100 of these in treetest4 -- and fewer in the other tests).
What To Hand In: