Assignment 7 -- due Monday, October 26 at the beginning of lab
CS 4521 Fall Semester, 2009
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: 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).

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: Three Red-Black Tree Exercises (15 points)

  1. (2 points) Do Exercise 13.1-4, page 276.
  2. (2 points) Answer the questions of Exercise 13.1-4 for AA-trees, which are discussed (very briefly) at the top of page 301. Hint: the answer to the previous question is in the previous paragraph on page 300.
  3. (1 points) Do exercise 13.1-5, p. 277.
  4. (2 points) Do exercise 13.1-6, p. 277. Justify your answers.
  5. (4 points) Exercise 13.3-2, p. 287: Starting with an empty rbtree, show its structure after inserting each of the keys 41, 38, 31, 12, 19, and 8, in that order. Draw the tree after each insertion. Indicate the color of each node.
  6. (4 points) Do Exercise 13.4-3, page 293: delete 8, 12, 19, 31, 38, and 41 from the red-black tree created in Exercise 13.3-2. Draw the tree after each insertion. Indicate the color of each node.

Page URL: http://www.d.umn.edu /~ddunham/cs4521f09/assignments/a7/assignment.html
Page Author: Doug Dunham
Last Modified: Monday, 19-Oct-2009 17:46:15 CDT
Comments to: ddunham@d.umn.edu