Вы находитесь на странице: 1из 2

CS271: Data Structures

Red-Black Tree Worksheet


1. The purpose of this assignment is to create a working Red-Black Tree class.
It requires only a few modifications from the Binary Search Trees. I sug-
gest you start by making a separate copy (in a separate directory) of the
binary search tree class code. Put all your RBT code in a file named rbt.h
(containing both the class declaration and all the class method definitions).
Rename the class name as RBT. Get the code to compile and work in the
same way as the binary search tree.

2. Next add the color field. Add a color field of type int to the Node struc-
ture. Set the initial color for all nodes as red whenever you create a new node
in your code.
Note that I added a special printing function to my in order walk to print both
the depth and color of each node along with the key value. This way I can
tell the tree structure (depth) and keep track of color as well.

3. The next step is to add the special nil node. Since nil is a reserved word,
you’ll have to create a different name (I called mine nilnode). I suggest
you create a node pointer in addition to root as a private data member of
your class. In the constructor, create a nil node and set its color to black; its
pointers and key field shouldn’t matter.
Now traverse through your code and set all NULL pointers (leaf nodes and
root parent) to point to the nil node. This will require some special thinking
as you will have to keep track of two nil nodes when you copy from another
red black tree in the copy constructor and assignment operator. You will still
want your search method to return a true NULL if it can’t find a particular
key (don’t return a pointer to the nil node).
Test this fully to make sure it is working correctly before moving on. The
same binary search tree code should be working.

4. Now add the RotateLeft and RotateRight methods. Again, test these
and make sure that they are fully working including special cases. The code
is short so this step shouldn’t take long.

5. The next big step is to implement the new insert along with the RBInsertFixup
method. The code is specified in the book and so if you copy carefully, you
should be able to get this working fairly quickly. Test it on a number of cases
to be sure both the tree structure is working properly and coloring is correct
(note there are 3 cases x2 for each side of the tree, in addition to special root
cases).

6. Finally, implement the remove method. Again, the code is in the text, so
copy carefully and test fully all cases.

7. [Optional]. We have proven in class that the average height of the binary
search tree is O(lg n) while the worse case is linear. We have also proven
that the worse case height of a red black tree is O(lg n). So the red black
tree seems to be the better choice and doesn’t sacrifice much in the way of
execution speed and adds only moderate additional coding complexity. But
how different are these two tree structures really in practice?
Write a program that generates a stream of random integers. Insert each
integer into both a binary search tree and also a red black tree (same insertion
stream into each structure). Add a method to each class to compute the height
of the structure and the number of elements. Use the data to get a practical
view of how the height of each tree structure varies. In order to compute a
good average, you may have to insert several different random streams of
the same length and average over their heights. Try different stream lengths
to see how the height grows as a function of n. Produce a small write-up of
your findings.

8. Submit your rbt.h header file that contains your RBT templated class. If
you completed the optional part, submit both your write-up (pdf) and the
code that you used to generate the data.

Вам также может понравиться