Assignment 8 -- due Tuesday, October 30 at the beginning of lab
CS 4521 Fall Semester, 2012
35 Points

Topics: Red-black tree insertion implementation, theory

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.

### Part 1: Coding Red-Black Tree Operations (20 points)

As described in the text and in class, red-black trees are a specialization of binary search trees which guarantee balance (within limits) so that searches, inserts, and deletes are O(lg n). You should have a working version of the last assignment (binary search trees) to do this assignment. In addition to the binary search tree operations from the last assignment, implementing red-black trees will require:
• Coding of LEFT-ROTATE and RIGHT-ROTATE (pseudocode for the former is given in the book but you will have to write the latter)
• Addition of a color field to the binary search tree node definition
• Use of the sentinel T.nil instead of NIL
• Coding of RB-INSERT and RB-INSERT-FIXUP (the pseudocode is given in the book, but recall that the "else" clause must be provided by you - by interchanging "left" and "right")
• Coding of RB-DELETE and RB-DELETE-FIXUP (the pseudocode is given in the book, but recall that the "else" clause must be provided by you - by interchanging "left" and "right")
Use your binary search tree command interpreter to test your red-black tree implementation using the following test files: treetest1, treetest2, treetest3, treetest4, and treetest5. Few modifications of the binary search tree command interpreter will be needed to convert it to a red-black tree command interpreter. Here is an example using treetest2:

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).

Helpful skeleton code: Here is a Makefile, a "command interpreter" cmdint.cpp (note the commented-out stuff, which will be useful later), a .h file for the Red-Black tree class rbtree.h, and skeleton code for the Red-Black tree class implementation rbtree.cpp. When you download these files, be sure to remove the ".txt" suffixes (by moving them to the same file name without the ".txt").

What To Hand In:

1. Clearly marked scripts of treetest1, treetest2, treetest3, treetest4, and treetest5, demonstrating your red-black tree implementation. Be sure to hand-draw the tree edges.
2. Well documented code for your red-black tree implementation.
3. The solutions to the exercises below.

### Part 2: Red-Black Tree Exercises (15 points)

1. (2 points) Do Exercise 13.1-4, pages 311-312.
2. (2 points) Answer the questions of Exercise 13.1-4 for AA-trees, which are discussed (very briefly) at the top of page 338. Hint: the answer to the previous question is in the previous paragraph on page 337.
3. (1 points) Do exercise 13.1-5, p. 312.