RED-BLACK TREES
A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions: - Every search path from the root to a leaf consists of the same number of black nodes.
- Each red node (except for the root) has a black parent.
- Each leaf node is black.
Every operation on a red-black tree is bounded as O "lg n". The maximum height of a red-black tree is 2lg "n + 1".
A red-black tree is headed by a structure defined by the RB_HEAD macro. A structure is declared as follows:
RB_HEAD HEADNAME TYPE
head;
where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree.
The RB_ENTRY macro declares a structure that allows elements to be connected in the tree.
In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the RB_PROTOTYPE macro, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by RB_ENTRY.
The function bodies are generated with the RB_GENERATE macro. It takes the same arguments as the RB_PROTOTYPE macro, but should be used only once.
Finally, the CMP argument is the name of a function used to compare tree noded with each other. The function takes two arguments of type
.Vt "struct TYPE *" . If the first argument is smaller than the second, the function returns a value smaller than zero. If they are equal, the function returns zero. Otherwise, it should return a value greater than zero. The compare function defines the order of the tree elements.
The RB_INIT macro initializes the tree referenced by head.
The red-black tree can also be initialized statically by using the RB_INITIALIZER macro like this:
RB_HEAD HEADNAME TYPE
head
=
RB_INITIALIZER &head;
The RB_INSERT macro inserts the new element elm into the tree.
The RB_REMOVE macro removes the element elm from the tree pointed by head.
The RB_FIND macro can be used to find a particular element in the tree.
struct TYPE find, *res;
find.key = 30;
res = RB_FIND(NAME, head, &find);
The RB_ROOT, RB_MIN, RB_MAX, and RB_NEXT macros can be used to traverse the tree:
"for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
Or, for simplicity, one can use the RB_FOREACH macro:
RB_FOREACH np NAME head
The RB_EMPTY macro should be used to check whether a red-black tree is empty.
NOTES
Trying to free a tree in the following way is a common error:
SPLAY_FOREACH(var, NAME, head) {
SPLAY_REMOVE(NAME, head, var);
free(var);
}
free(head);
Since var is freed, the FOREACH macro refers to a pointer that may have been reallocated already. Proper code needs a second variable.
for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
nxt = SPLAY_NEXT(NAME, head, var);
SPLAY_REMOVE(NAME, head, var);
free(var);
}
Both RB_INSERT and SPLAY_INSERT return NULL if the element was inserted in the tree successfully, otherwise they return a pointer to the element with the colliding key.
Accordingly, RB_REMOVE and SPLAY_REMOVE return the pointer to the removed element otherwise they return NULL to indicate an error.
AUTHORS
The author of the tree macros is
.An Niels Provos .