Description

These macros define data structures for different types of trees: splay trees
and red-black trees.

In the macro definitions, TYPE is the name tag of a user-defined
structure that must contain a field named FIELD, of type SPLAY_ENTRY or
RB_ENTRY. The argument HEADNAME is the name tag of a user defined structure
that must be declared using the macros SPLAY_HEAD() or RB_HEAD(). The argument
NAME has to be a unique name prefix for every tree that
is defined.

Splay Trees

A splay tree is a self-organizing data structure. Every operation on the
tree causes a splay to happen. The splay moves the requested node
to the root of the tree and partly rebalances it. This has
the benefit that request locality causes faster lookups as the requested nodes
move to the top of the tree. On the other hand, every lookup
causes memory writes

The Balance Theorem bounds the total access time for m operations and
n inserts on an initially empty tree as O((m + n)lg n).
The amortized cost for a sequence of m accesses to a splay tree
is O(lg n).

A splay tree is headed by a structure defined by the SPLAY_HEAD()
macro. A SPLAY_HEAD structure is declared as follows:

SPLAY_HEAD(HEADNAME, TYPE) head;

where HEADNAME is the name of the structure to be defined, and
structTYPE is the type of the elements to be inserted into
the tree.

The SPLAY_ENTRY() macro declares a structure that allows elements to be connected
in the tree.

To use the functions that manipulate the tree structure, their prototypes need
to be declared with the SPLAY_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 SPLAY_ENTRY().

The function bodies are generated with the SPLAY_GENERATE() macro. It takes the
same arguments as the SPLAY_PROTOTYPE() macro, but should be used only once.

The CMP argument is the name of a function used to compare
trees' nodes with each other. The function takes two arguments of type
structTYPE *. 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 SPLAY_INIT() macro initializes the tree referenced by head.

The splay tree can also be initialized statically by using the SPLAY_INITIALIZER()
macro as follows:

SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);

The SPLAY_INSERT() macro inserts the new element elm into the tree. Upon
success, NULL is returned. If a matching element already exists in the
tree, the insertion is aborted, and a pointer to the existing element
is returned.

The SPLAY_REMOVE() macro removes the element elm from the tree pointed by
head. Upon success, a pointer to the removed element is returned. NULL
is returned if elm is not present in the tree.

The SPLAY_FIND() macro can be used to find a particular element in
the tree.

The SPLAY_EMPTY() macro should be used to check whether a splay tree
is empty.

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 RB_HEAD structure is declared as follows:

RB_HEAD(HEADNAME, TYPE) head;

where HEADNAME is the name of the structure to be defined, and
structTYPE 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.

To use the functions that manipulate the tree structure, their prototypes need
to be declared with the RB_PROTOTYPE() or RB_PROTOTYPE_STATIC() macros, 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() or RB_GENERATE_STATIC() macros. These
macros take the same arguments as the RB_PROTOTYPE() and RB_PROTOTYPE_STATIC() macros, but
should be used only once.

The CMP argument is the name of a function used to compare
trees' nodes with each other. The function takes two arguments of type
structTYPE *. If the first argument is smaller 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 as follows:

RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head)

The RB_INSERT() macro inserts the new element elm into the tree. Upon
success, NULL is returned. If a matching element already exists in the
tree, the insertion is aborted, and a pointer to the existing element
is returned.

The RB_REMOVE() macro removes the element elm from the tree pointed by
head. RB_REMOVE() returns elm.

The RB_FIND() and RB_NFIND() macros can be used to find a particular
element in the tree. RB_FIND() finds the node with the same key
as elm. RB_NFIND() finds the first node greater than or equal to the
search key.

Or, for simplicity, one can use the RB_FOREACH() or RB_FOREACH_REVERSE() macros:

RB_FOREACH(np, NAME, &head)

The macros RB_FOREACH_SAFE() and RB_FOREACH_REVERSE_SAFE() traverse the tree referenced by head in
a forward or reverse direction respectively, assigning each element in turn to
np. However, unlike their unsafe counterparts, they permit both the removal of np
as well as freeing it from within the loop safely without interfering
with the traversal.

The RB_EMPTY() macro should be used to check whether a red-black tree
is empty.

Examples

Example 1 Declare a red-black tree holding integers.

The following example demonstrates how to declare a red-black tree holding integers.
Values are inserted into it and the contents of the tree
are printed in order. Lastly, the internal structure of the tree
is printed.