This repository contains a red black tree implementation for the Advanced Programming course exam.
From [ 1 ], a red-black tree is a binary tree that satisfies the following red-black properties:
- Every node is either red or black.
- The root is black.
- Every leaf ( NIL) is black.
- If a node is red, then both its children are black.
- For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
The include folder contains the implementation
of the RBTree class (and the catch.hpp header). For readability reasons, the implementation
is split among different files as follows:
RBTree.h: declaration of the RBTree classRBTNode.h: implementation of the private struct NodeRBTCheckPropertiesFunctions.h: implementation of functions that check black propertiesRBTConstIterator.h: implementation of the constant forward iteratorRBTInterface.h: implementation of the public methodsRBTPrivateFunctions: implementation of the private functions
In the test folder there are all the tests that verify the public interface and a simple benchmark. There are 8 test cases:
- Initialization
- Insertion
- Deletion
- Searching
- Move semantics
- Copy semantics
- Iterator
- Output operator
The benchmark compares the contains function with the find function of the std::set container.
- I considered the NIL node as
nullptr. Nodeis a private nested struct of theRBTreeclass.- I used
std::unique_ptrto avoid any memory leaks. - At the end of the
insertanderasefunctions I put an assertion that checks if the tree still satisfies the red black properties. When benchmarking the assertion is disabled withNDEBUGflag.
mkdir RBT-build
cd RBT-build
cmake ../
cmake --build .
./RBT-test
./RBT-benchmark
[ 1 ] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms. The MIT Press, 2nd edition, 2001