Educational Objectives: On successful completion of this assignment, the student should be able to
Background Knowledge Required: Be sure that you have mastered the
material in these chapters before beginning the project:
Binary Trees and Iterators,
Binary Tree Construction,
Associative Binary Trees,
Generic Associative Containers,
Set and Map Abstractions,
Operational Objectives: Implement a generic container class TRBT<T,P> based on the red-black tree algorithms for insertion and removal of elements and satisfying the conditions detailed in this document.
Deliverables: One file:
trbt.h # contains the red-black tree definitions and implementations
A red-black tree is a binary search tree whose nodes have a color attribute and which satisfies the following additional properties:
Because a red-black tree (RBT) is a binary search tree (BST), there is a well defined binary search algorithm in an RBT that follows descending paths. An important feature of RBTs is that they have height bounded above by 2 log2 (n + 1), where n is the number of nodes in the RBT, so an RBT must be fairly "bushy". (In stark contrast, a BST can be severely "leggy", with height n - 1. We can define "bushy" and "leggy" asymptotically as having height O(log n) and Ω(n), respectively.) It follows that
Theorem 1. BST search in an RBT has worst case run time is O(log n), where n is the number of nodes.
The challenge is to make sure that the RBT properties are maintained as we insert and remove elements. It turns out that the RBT properties do not necessarily hold after an ordinary BST insert or remove operation, but that there are "repair" algorithms that bring the resulting BST back into compliance with the RBT rules. These algorithms restructure the BST by pruning and re-hanging subtrees and are called rotations.
Rotations are constant time algorithms, and they are combined into repair algorithms that iterate along a descending path in the RBT. It follows that BST insert or remove, followed by RBT repair, has run time O(log n). Consequently
Theorem 2. RBT insert and remove have worst case run time O(log n).
Detailed specifications of the algorithms, including proof of correctness of the asserted outcomes, are found in the text [Cormen, Ch 13].
Define and implement the classes TRBT<T,P> and TRBTIterator<T,P> in the file rbt.h. Also place all supporting definitions and implementations, such as operator overloads, in this file.
Test your classes using the distributed test harness fcset.cpp.
Write a brief description of your development and test methods and results and place this in the file header documentation of rbt.h.
Submit the project using the script LIB/submitscripts/proj4submit.sh.
T is the element type stored in the container.
P is the predicate type used to determine order in the container.
TRBT<T,P> should implement unimodal associative container semantics.
TRBTIterator<T,P> should implement const bidirectional iterator semantics.
TRBT<T,P> and TRBTIterator<T,P> should be in the fsu namespace.
TRBT<T,P> should use composition with TBinaryTree<T>, as illustrated in TBST<T,P> (distributed in the file LIB/tcpp/tbst.h).
TRBTIterator<T,P> should use composition with TBinaryTreeIterator<T>, as illustrated in TBSTIterator<T,P> (distributed in the file LIB/tcpp/tbst.h).
Create TRBT<T,P> as a derived class of TBST<T,P>. In this plan, a good bit of code can be re-used (by inheritance), including complete re-use of BSTIterator using a typedef statement. However there may be a small runtime penalty in some of the operations, not enough to affect the asymptotic runtime. The file tbst.h will be in the library to facilitate this plan.
A big advantage of this plan is that there is a predefined testing method already implemented as part of TBST:
void TestRBT () const;
You may observe that following this requirement will automatically ensure that several of the earlier requirements are satisfied.
TRBT<T,P> should function correctly with the following definition of SetType:
typedef fsu::CSet < T , fsu::TRBT < T , P > , P > SetType;
That is, objects of type SetType should exhibit correct behavior for a unimodal ordered set of elements of type T and order predicate P as defined in the file cset.h and tested using fcset.cpp.
Detailed understanding of the definition and implementation of TBinaryTree<T>, TBST<T,P> and their associated iterator and navigator classes will be both required and substantially helpful.
There are only two operations (other than the usual mandatory constructors, destructor, and assignment operator) that require new or modified implementation from those in the parent class TBST<T,P> - the two associative mutation methods:
Iterator Insert (const ValueType&); size_t Remove (const ValueType&);
These can be implemented using calls to base class methods of the same name or by going all the way back to calls to TBinaryTree<T> insertion and removal methods
InsertRoot(t); InsertLeft(t) InsertRight(t) Remove(i)
In either case, you will need to finish off an insertion or removal with a red-black repair process that rebalances the tree with calls to the TBinaryTree<T> rotation methods
RotateLeft(n) RotateRight(n)
and then resets the colors of nodes after the rotation.
You may want to encapsulate the repair process with a couple of private methods
InsertRestore (...); RemoveRestore (...);
that are called after new node is inserted or removed from the tree.
One implementation uses these precise prototypes and functional descriptions:
void InsertRestore (const TBinaryTreeNavigator& n); // Called after inserting a new node n // Restores the tree to the RBT status void RemoveRestore ( TBinaryTreeNavigator<T>& child, // the child of the node to remove TBinaryTreeNavigator<T>& parent, // the parent of the node to remove int left // whether the child is left or right ) // Called before removing a node // Assumes the node to remove has at most one child // Restores the tree to the RBT status
The functionality test program fcset.cpp will be used in our assessment. You may also find it useful to build up a simplifiued test harness as you develope TRBT.
The structural integrity test program strbt.cpp will also be used in our assessment of TRBT.