The assignment consists of two parts. In the first part, you will implement and demonstrate the red-black tree operations except for deletion. The second part consists of red-black exercises.
treetest2 commands
+------------------------------------------------+
| # create tree |
| 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 |
| # 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 |
| # 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, I strongly
suggest that you 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 will 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 exercise below asks for an "upright" example.
What To Hand In: