|
ADS
Algorithms and Data Structures
|
The RedBlackTree of Morin (chapter 9), a left-leaning red-black tree that simulates a 2-4 tree, beyond the book: a binary search tree whose nodes are red or black with the black-height property (every path from a node down to a nil crosses the same number of black nodes), the no-red-edge property (a red node has no red child) and the left-leaning property (if the left child of a node is black, so is the right one); nil and the root are black. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include <array>#include <concepts>#include <cstddef>#include <cstdint>#include <optional>#include <utility>#include <vector>#include "ADS/tree/BinaryTreeAlgorithms.h"#include "ADS/tree/impl/LeftLeaningRedBlackTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::LeftLeaningRedBlackTree< K, V > |
| a DICTIONARY on the left-leaning red-black tree of Morin, a simulated 2-4 tree More... | |
| struct | ADS::LeftLeaningRedBlackTree< K, V >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The RedBlackTree of Morin (chapter 9), a left-leaning red-black tree that simulates a 2-4 tree, beyond the book: a binary search tree whose nodes are red or black with the black-height property (every path from a node down to a nil crosses the same number of black nodes), the no-red-edge property (a red node has no red child) and the left-leaning property (if the left child of a node is black, so is the right one); nil and the root are black.
Each black node and its red children are a node of a 2-4 tree, of degree 2 with no red child, 3 with a red left child, 4 with two, and the left-leaning property makes the representation of a 2-4 tree unique; hence the height is at most \( 2 \log_2 ( n + 1 ) \) (Lemma 9.1 and Lemma 9.2 of Morin). It is a DICTIONARY [see ADT.h] with the find(x) of an SSet (lowerBound()).
An insertion adds a red leaf and addFixup() restores the properties going up, by flipLeft() where the left-leaning property fails, flipRight() at a black grandparent whose right child is black (a 2-4 node of degree 3 becoming one of degree 4) and pushBlack() at a grandparent with two red children (the split of a 2-4 node of degree 5), the last one moving up; a removal splices a node with one child at most, adds its color to the child (possibly making it double black) and removeFixup() moves the double black up by the four cases of section 9.2.4 of Morin, pullBlack() merging two 2-4 nodes and the flips borrowing from a sibling. Both take \( O( \log n ) \) time, and Theorem 9.2 of Morin bounds the restructuring, rotations and changes of color, by \( O( 1 ) \) amortized per operation. absorb() joins a tree whose keys are larger (exercises 9.10 and 9.11): the smallest of them becomes a red node over the two trees, linked on the spine of the higher tree where the black heights match, and addFixup() restores the properties, \( O( h_1 + h_2 ) \) time.
The links to nil are null pointers: the double black that the removal of a leaf puts on nil is carried by a node of no key placed for the time of removeFixup() where the nil was, so that no sentinel is shared or written, and two trees can be joined with no pass on their nodes (where Morin's sentinel, written by the removal, ties every node to its tree; exercise 9.7 asks why the removal sets the parent of the spliced child: the child may be the sentinel, whose parent removeFixup() reads). This class is separate from the Red-Black tree of the book [see SearchTree.h], which is not left-leaning and repairs by the cases of Cormen et al.
Where it is meant to be faster than the RedBlackTree of Morin, for the benchmarks to check: the color is a byte, not an int; nil is a null pointer read by no load, where his sentinel is a node read at every test of a color; the operations are his, with the same number of rotations, so that the rest of the difference is the allocation and the layout of the nodes.
The contracts [see Contract.h]: insert() ensures that the key is there and remove() that it is not, absorb() needs a tree of larger keys; the invariant, an \( O( n ) \) check at the level 3, is the order of the keys, the parents and the three properties. The operations are defined in LeftLeaningRedBlackTree.tpp.