NAME
SPLAY_PROTOTYPE, SPLAY_GENERATE, SPLAY_ENTRY, SPLAY_HEAD, SPLAY_INITIALIZER, SPLAY_ROOT, SPLAY_EMPTY, SPLAY_NEXT, SPLAY_MIN, SPLAY_MAX, SPLAY_FIND, SPLAY_LEFT, SPLAY_RIGHT, SPLAY_FOREACH, SPLAY_INIT, SPLAY_INSERT, SPLAY_REMOVE, RB_PROTOTYPE, RB_PROTOTYPE_STATIC, RB_GENERATE, RB_GENERATE_STATIC, RB_ENTRY, RB_HEAD, RB_INITIALIZER, RB_ROOT, RB_EMPTY, RB_NEXT, RB_PREV, RB_MIN, RB_MAX, RB_FIND, RB_NFIND, RB_LEFT, RB_RIGHT, RB_PARENT, RB_FOREACH, RB_FOREACH_SAFE, RB_FOREACH_REVERSE, RB_FOREACH_REVERSE_SAFE, RB_INIT, RB_INSERT, RB_REMOVE — implementations of splay and red-black treesLIBRARY
library “libbsd”SYNOPSIS
#include <sys/tree.h> (See libbsd(7) for include usage.)SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP); SPLAY_GENERATE(NAME, TYPE, FIELD, CMP); SPLAY_ENTRY(TYPE); SPLAY_HEAD(HEADNAME, TYPE); struct TYPE *
SPLAY_INITIALIZER(SPLAY_HEAD *head); SPLAY_ROOT(SPLAY_HEAD *head); int
SPLAY_EMPTY(SPLAY_HEAD *head); struct TYPE *
SPLAY_NEXT(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE *
SPLAY_MIN(NAME, SPLAY_HEAD *head); struct TYPE *
SPLAY_MAX(NAME, SPLAY_HEAD *head); struct TYPE *
SPLAY_FIND(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE *
SPLAY_LEFT(struct TYPE *elm, SPLAY_ENTRY NAME); struct TYPE *
SPLAY_RIGHT(struct TYPE *elm, SPLAY_ENTRY NAME); SPLAY_FOREACH(VARNAME, NAME, SPLAY_HEAD *head); void
SPLAY_INIT(SPLAY_HEAD *head); struct TYPE *
SPLAY_INSERT(NAME, SPLAY_HEAD *head, struct TYPE *elm); struct TYPE *
SPLAY_REMOVE(NAME, SPLAY_HEAD *head, struct TYPE *elm);
RB_PROTOTYPE(NAME, TYPE, FIELD, CMP); RB_PROTOTYPE_STATIC(NAME, TYPE, FIELD, CMP); RB_GENERATE(NAME, TYPE, FIELD, CMP); RB_GENERATE_STATIC(NAME, TYPE, FIELD, CMP); RB_ENTRY(TYPE); RB_HEAD(HEADNAME, TYPE); RB_INITIALIZER(RB_HEAD *head); struct TYPE *
RB_ROOT(RB_HEAD *head); int
RB_EMPTY(RB_HEAD *head); struct TYPE *
RB_NEXT(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE *
RB_PREV(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE *
RB_MIN(NAME, RB_HEAD *head); struct TYPE *
RB_MAX(NAME, RB_HEAD *head); struct TYPE *
RB_FIND(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE *
RB_NFIND(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE *
RB_LEFT(struct TYPE *elm, RB_ENTRY NAME); struct TYPE *
RB_RIGHT(struct TYPE *elm, RB_ENTRY NAME); struct TYPE *
RB_PARENT(struct TYPE *elm, RB_ENTRY NAME); RB_FOREACH(VARNAME, NAME, RB_HEAD *head); RB_FOREACH_SAFE(VARNAME, NAME, RB_HEAD *head, TEMP_VARNAME); RB_FOREACH_REVERSE(VARNAME, NAME, RB_HEAD *head); RB_FOREACH_REVERSE_SAFE(VARNAME, NAME, RB_HEAD *head, TEMP_VARNAME); void
RB_INIT(RB_HEAD *head); struct TYPE *
RB_INSERT(NAME, RB_HEAD *head, struct TYPE *elm); struct TYPE *
RB_REMOVE(NAME, RB_HEAD *head, struct TYPE *elm);
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 typeSPLAY_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.
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.
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;
SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
struct TYPE find, *res; find.key = 30; res = SPLAY_FIND(NAME, &head, &find);
for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
SPLAY_FOREACH(np, NAME, &head)
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.
RB_HEAD(HEADNAME, TYPE) head;
RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);
struct TYPE find, *res; find.key = 30; res = RB_FIND(NAME, &head, &find);
for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
RB_FOREACH(np, NAME, &head)
EXAMPLES
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.#include <sys/tree.h> #include <err.h> #include <stdio.h> #include <stdlib.h> struct node { RB_ENTRY(node) entry; int i; }; int intcmp(struct node *, struct node *); void print_tree(struct node *); int intcmp(struct node *e1, struct node *e2) { return (e1->i < e2->i ? -1 : e1->i > e2->i); } RB_HEAD(inttree, node) head = RB_INITIALIZER(&head); RB_PROTOTYPE(inttree, node, entry, intcmp) RB_GENERATE(inttree, node, entry, intcmp) int testdata[] = { 20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18, 7, 11, 14 }; void print_tree(struct node *n) { struct node *left, *right; if (n == NULL) { printf("nil"); return; } left = RB_LEFT(n, entry); right = RB_RIGHT(n, entry); if (left == NULL && right == NULL) printf("%d", n->i); else { printf("%d(", n->i); print_tree(left); printf(","); print_tree(right); printf(")"); } } int main(void) { int i; struct node *n; for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) { if ((n = malloc(sizeof(struct node))) == NULL) err(1, NULL); n->i = testdata[i]; RB_INSERT(inttree, &head, n); } RB_FOREACH(n, inttree, &head) { printf("%d\n", n->i); } print_tree(RB_ROOT(&head)); printf("\n"); return (0); }
SEE ALSO
queue(3bsd)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);
for (var = SPLAY_MIN(NAME, &head); var != NULL; var = nxt) { nxt = SPLAY_NEXT(NAME, &head, var); SPLAY_REMOVE(NAME, &head, var); free(var); }
AUTHORS
The author of the tree macros is Niels Provos.May 10, 2019 | Debian |