Assignment 7 -- due Wednesday, March 7 at the beginning of lab
CS 4521 Spring Semester, 2007
20 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 except for deletion. The second part consists of red-black exercises.

Part 1: Coding Red-Black Tree Operations (12 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 and treetest2. If you used good abstraction in designing your binary search tree command interpreter, few modifications will be needed to convert it to a red-black tree command interpreter. Here is an example using treetest2:

              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:

  1. Clearly marked scripts of treetest1 and treetest2 demonstrating your red-black tree insertion 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 (8 points)

  1. Do exercise 13.1-5, p. 277.
  2. Do exercise 13.1-6, p. 277. Justify your answers.
  3. 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.

Page URL: http://www.d.umn.edu /~ddunham/cs4521s07/assignments/a7/assignment.html
Page Author: Doug Dunham
Last Modified: Tuesday, 27-Feb-2007 17:55:57 CST
Comments to: ddunham@d.umn.edu