RB_FOREACH_REVERSE (3)
Leading comments
$OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $ Copyright 2002 Niels Provos <provos@citi.umich.edu> All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, th...
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_REVERSE RB_INIT RB_INSERT RB_REMOVE - implementations of splay and red-black treesSYNOPSIS
In sys/tree.h (See libbsd(7) for include usage.) Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP Fn SPLAY_GENERATE NAME TYPE FIELD CMP Fn SPLAY_ENTRY TYPE Fn SPLAY_HEAD HEADNAME TYPE Ft struct TYPE * Fn SPLAY_INITIALIZER SPLAY_HEAD *head Fn SPLAY_ROOT SPLAY_HEAD *head Ft bool Fn SPLAY_EMPTY SPLAY_HEAD *head Ft struct TYPE * Fn SPLAY_NEXT NAME SPLAY_HEAD *head struct TYPE *elm Ft struct TYPE * Fn SPLAY_MIN NAME SPLAY_HEAD *head Ft struct TYPE * Fn SPLAY_MAX NAME SPLAY_HEAD *head Ft struct TYPE * Fn SPLAY_FIND NAME SPLAY_HEAD *head struct TYPE *elm Ft struct TYPE * Fn SPLAY_LEFT struct TYPE *elm SPLAY_ENTRY NAME Ft struct TYPE * Fn SPLAY_RIGHT struct TYPE *elm SPLAY_ENTRY NAME Fn SPLAY_FOREACH VARNAME NAME SPLAY_HEAD *head Ft void Fn SPLAY_INIT SPLAY_HEAD *head Ft struct TYPE * Fn SPLAY_INSERT NAME SPLAY_HEAD *head struct TYPE *elm Ft struct TYPE * Fn SPLAY_REMOVE NAME SPLAY_HEAD *head struct TYPE *elm Fn RB_PROTOTYPE NAME TYPE FIELD CMP Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP Fn RB_GENERATE NAME TYPE FIELD CMP Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP Fn RB_ENTRY TYPE Fn RB_HEAD HEADNAME TYPE Fn RB_INITIALIZER RB_HEAD *head Ft struct TYPE * Fn RB_ROOT RB_HEAD *head Ft bool Fn RB_EMPTY RB_HEAD *head Ft struct TYPE * Fn RB_NEXT NAME RB_HEAD *head struct TYPE *elm Ft struct TYPE * Fn RB_PREV NAME RB_HEAD *head struct TYPE *elm Ft struct TYPE * Fn RB_MIN NAME RB_HEAD *head Ft struct TYPE * Fn RB_MAX NAME RB_HEAD *head Ft struct TYPE * Fn RB_FIND NAME RB_HEAD *head struct TYPE *elm Ft struct TYPE * Fn RB_NFIND NAME RB_HEAD *head struct TYPE *elm Ft struct TYPE * Fn RB_LEFT struct TYPE *elm RB_ENTRY NAME Ft struct TYPE * Fn RB_RIGHT struct TYPE *elm RB_ENTRY NAME Ft struct TYPE * Fn RB_PARENT struct TYPE *elm RB_ENTRY NAME Fn RB_FOREACH VARNAME NAME RB_HEAD *head Fn RB_FOREACH_REVERSE VARNAME NAME RB_HEAD *head Ft void Fn RB_INIT RB_HEAD *head Ft struct TYPE * Fn RB_INSERT NAME RB_HEAD *head struct TYPE *elm Ft struct TYPE * Fn RB_REMOVE NAME RB_HEAD *head struct TYPE *elmDESCRIPTION
These macros define data structures for different types of trees: splay trees and red-black trees.In the macro definitions, Fa TYPE is the name tag of a user defined structure that must contain a field of type Vt SPLAY_ENTRY , or Vt RB_ENTRY , named Fa ENTRYNAME . The argument Fa HEADNAME is the name tag of a user defined structure that must be declared using the macros Fn SPLAY_HEAD , or Fn RB_HEAD . The argument Fa NAME has to be a unique name prefix for every tree that is defined.
The function prototypes are declared with Fn SPLAY_PROTOTYPE , Fn RB_PROTOTYPE , or Fn RB_PROTOTYPE_STATIC . The function bodies are generated with Fn SPLAY_GENERATE , Fn RB_GENERATE , or Fn 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 Fn O lp]m + nrp]lg n . The amortized cost for a sequence of m accesses to a splay tree is Fn O lg n .
A splay tree is headed by a structure defined by the Fn SPLAY_HEAD macro. A structure is declared as follows:
Fn SPLAY_HEAD HEADNAME TYPE head
where Fa HEADNAME is the name of the structure to be defined, and struct Fa TYPE is the type of the elements to be inserted into the tree.
The Fn 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 Fn SPLAY_PROTOTYPE macro, where Fa NAME is a unique identifier for this particular tree. The Fa TYPE argument is the type of the structure that is being managed by the tree. The Fa FIELD argument is the name of the element defined by Fn SPLAY_ENTRY .
The function bodies are generated with the Fn SPLAY_GENERATE macro. It takes the same arguments as the Fn SPLAY_PROTOTYPE macro, but should be used only once.
Finally, the Fa CMP argument is the name of a function used to compare tree nodes 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 Fn SPLAY_INIT macro initializes the tree referenced by Fa head .
The splay tree can also be initialized statically by using the Fn SPLAY_INITIALIZER macro like this:
Fn SPLAY_HEAD HEADNAME TYPE head = Fn SPLAY_INITIALIZER &head ;
The Fn SPLAY_INSERT macro inserts the new element Fa elm into the tree.
The Fn SPLAY_REMOVE macro removes the element Fa elm from the tree pointed by Fa head .
The Fn 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 Fn SPLAY_ROOT , Fn SPLAY_MIN , Fn SPLAY_MAX , and Fn 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 Fn SPLAY_FOREACH macro:
Fn SPLAY_FOREACH np NAME head
The Fn 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 Fn O lg n . The maximum height of a red-black tree is Fn 2lg n + 1 .
A red-black tree is headed by a structure defined by the Fn RB_HEAD macro. A structure is declared as follows:
Fn RB_HEAD HEADNAME TYPE head
where Fa HEADNAME is the name of the structure to be defined, and struct Fa TYPE is the type of the elements to be inserted into the tree.
The Fn 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 Fn RB_PROTOTYPE or Fn RB_PROTOTYPE_STATIC macro, where Fa NAME is a unique identifier for this particular tree. The Fa TYPE argument is the type of the structure that is being managed by the tree. The Fa FIELD argument is the name of the element defined by Fn RB_ENTRY .
The function bodies are generated with the Fn RB_GENERATE or Fn RB_GENERATE_STATIC macro. These macros take the same arguments as the Fn RB_PROTOTYPE and Fn RB_PROTOTYPE_STATIC macros, but should be used only once.
Finally, the Fa CMP argument is the name of a function used to compare tree nodes 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 Fn RB_INIT macro initializes the tree referenced by Fa head .
The red-black tree can also be initialized statically by using the Fn RB_INITIALIZER macro like this:
Fn RB_HEAD HEADNAME TYPE head = Fn RB_INITIALIZER &head ;
The Fn RB_INSERT macro inserts the new element Fa elm into the tree.
The Fn RB_REMOVE macro removes the element Fa elm from the tree pointed by Fa head .
The Fn RB_FIND and Fn 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 Fn RB_ROOT , Fn RB_MIN , Fn RB_MAX , Fn RB_NEXT , and Fn 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 Fn RB_FOREACH or Fn RB_FOREACH_REVERSE macro:
Fn RB_FOREACH np NAME head
The Fn 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 Fn 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 Fn RB_INSERT and Fn 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, Fn RB_REMOVE and Fn SPLAY_REMOVE return the pointer to the removed element otherwise they return NULL to indicate an error.