Recall that the description of the AVL tree project contains information about the series of projects that includes this one and the next, as well as the set-up and the code base.
As a reminer, it is important that in all these projects your should read the code documentation carefully. It's there to provide specific, contextual information for understandning what the given code does and how the code you write needs to fit in to this.
The goal of this project is to understand the implement the rotations that maintain the properties of traditional/general red-black trees.
Here's the class heirarchy again:
The class RedBlackTreeMap
and its node
classes contain the code for verifying the red-black tree
properties.
Here is the class heirarchy for nodes, this time showing the RB node
classes as well:
The RBNode
interface defines methods to get information
specific to nodes in a red-black tree: isRed()
,
blackHeight()
(should be necessarily only for verification and
debugging), redden()
, and blacken()
.
The rest of the set up is very similar to that of AVL trees:
the code for put()
is inherited from
RecursiveBSTMap
, and it calls putFixup()
which returns a node to replace the one on which it is called;
following that, verify()
will be called if debugging mode
is on.
Although a put operation (or a subsequent fixup) may result in
a violation temporarily, those shold be fixed by the time
verify()
.
If a violation is found during verification, then an appropriate exception
will be thrown:
DoubleRedException
--two red nodes occur in a row.
InconsistentBlackHeightException
--a node occurs whose
children have different black heights.
Additionally, a RedNullException
will be thrown during
fixup if your code ever calls redden()
on a null-node object.
(Nulls are considered black.)
Write the putFixup()
method in
TraditionalRedBlackTreeMap.TradRBRealNode
.
Remember that double-red violations should be handled at
the level above the violation.
Since there is (or should be) only one violation at a time,
the node above the violation will be (or should be) black.
Here is how I recommend organzing your code: If the node on which this is called is black, then check to see whether there is a L-R, L-L, R-L, or R-R double red violation (or, of course, no violation at all). Handle these four cases separately, but remember that L-R will fall through to L-L (that is, our way of fixing L-L is transforming it to L-R) and R-L will fall through to R-R. Moreover, L-L and R-R will each be subdivided into "red uncle" and "black uncle" subcases.
I recommend you write helper functions rotateLeft()
and rotateRight()
.
Stubs are provided for them.
Test using TRBBSTMTest
.
Copy the file you modified (TraditionalRedBlackTreeMap.java
)
to your turn-in folder /cslab/class/cs345/(your id)/trad-rb
.
To keep up with the course, this should be finished by March 17.