Main index | Section 3 | Options |
#include <sys/tree.h>
In the macro definitions, TYPE is the name tag of a user defined structure that must contain a field of type SPLAY_ENTRY, or RB_ENTRY, named ENTRYNAME. 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.
The function prototypes are declared with SPLAY_PROTOTYPE(), RB_PROTOTYPE(), or RB_PROTOTYPE_STATIC(). The function bodies are generated with SPLAY_GENERATE(), RB_GENERATE(), or RB_GENERATE_STATIC(). See the examples below for further explanation of how these macros are used.
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 structure is declared as follows: SPLAY_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 SPLAY_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 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.
Finally, the CMP argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type 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 SPLAY_INIT() macro initializes the tree referenced by head.
The splay tree can also be initialized statically by using the SPLAY_INITIALIZER() macro like this: SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
The SPLAY_INSERT() macro inserts the new element elm into the tree.
The SPLAY_REMOVE() macro removes the element elm from the tree pointed by head.
The SPLAY_FIND() macro can be used to find a particular element in the tree.
struct TYPE find, *res; find.key = 30; res = SPLAY_FIND(NAME, head, &find);
The SPLAY_ROOT(), SPLAY_MIN(), SPLAY_MAX(), and SPLAY_NEXT() macros can be used to traverse the tree:
for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
Or, for simplicity, one can use the SPLAY_FOREACH() macro: SPLAY_FOREACH(np, NAME, head)
The SPLAY_EMPTY() macro should be used to check whether a splay tree is empty.
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() or RB_PROTOTYPE_STATIC() 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(). Individual prototypes can be declared with RB_PROTOTYPE_INSERT(), RB_PROTOTYPE_INSERT_COLOR(), RB_PROTOTYPE_REMOVE(), RB_PROTOTYPE_REMOVE_COLOR(), RB_PROTOTYPE_FIND(), RB_PROTOTYPE_NFIND(), RB_PROTOTYPE_NEXT(), RB_PROTOTYPE_PREV(), and RB_PROTOTYPE_MINMAX() in case not all functions are required. The individual prototype macros expect NAME, TYPE, and ATTR arguments. The ATTR argument must be empty for global functions or static for static functions.
The function bodies are generated with the RB_GENERATE() or RB_GENERATE_STATIC() macro. These macros take the same arguments as the RB_PROTOTYPE() and RB_PROTOTYPE_STATIC() macros, but should be used only once. As an alternative individual function bodies are generated with the RB_GENERATE_INSERT(), RB_GENERATE_INSERT_COLOR(), RB_GENERATE_REMOVE(), RB_GENERATE_REMOVE_COLOR(), RB_GENERATE_FIND(), RB_GENERATE_NFIND(), RB_GENERATE_NEXT(), RB_GENERATE_PREV(), and RB_GENERATE_MINMAX() macros.
Finally, the CMP argument is the name of a function used to compare tree nodes with each other. The function takes two arguments of type 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() and RB_NFIND() macros 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(), RB_NEXT(), and RB_PREV() 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() or RB_FOREACH_REVERSE() macro: 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.
Both RB_FOREACH_FROM() and RB_FOREACH_REVERSE_FROM() may be used to continue an interrupted traversal in a forward or reverse direction respectively. The head pointer is not required. The pointer to the node from where to resume the traversal should be passed as their last argument, and will be overwritten to provide safe traversal.
The RB_EMPTY() macro should be used to check whether a red-black tree is empty.
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.
TREE (3) | February 25, 2020 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | I define UNIX as “30 definitions of regular expressions living under one roof.” | ” |
— Donald Knuth |