A red-black tree is represented by a caller-allocated rb_tree_t structure. This structure describes various characteristics of the tree, such as the number of nodes in the tree, and includes a pointer to the root node of the tree. Nodes may be added to the tree using rt_add() or removed from the tree using rt_remove(). Additionally, the key on a given node may be changed using the rt_move() function. Nodes may be looked up with rt_find(), and rt_iter() will execute a user-defined function for each node in the tree in the specified order. To remove all entries in the tree, simply call the rt_flush() function. If you must manually iterate through the tree, you may call the rt_next() and rt_prev() functions to determine the next or previous nodes to visit.
There are three ways to traverse a binary tree. The first, known as "preorder," visits the root node, then traverses the left subtree in preorder, then traverses the right subtree in preorder. The second, known an "inorder," traverses the left subtree in inorder, then the root node, then the right subtree in inorder. (This particular ordering retrieves the nodes in lexical order; thus its name.) The third ordering is known as "postorder"; this ordering traverses the left subtree, the right subtree, then visits the root node. To iterate over the tree in one of these orderings, simply call rt_iter() (or rt_next() or rt_prev()) with the RBT_ORDER_PRE, RBT_ORDER_IN, or RBT_ORDER_POST flags. You may OR these flags with DB_FLAG_REVERSE to reverse the traversal ordering, if you wish.
Data Structures | |
struct | _rb_tree_s |
Red-black tree structure. More... | |
struct | _rb_node_s |
Red-black tree node structure. More... | |
Defines | |
#define | RB_TREE_MAGIC |
Red-black tree magic number. | |
#define | RB_TREE_INIT(comp, extra) |
Red-black tree static initializer. | |
#define | rt_verify(tree) |
Red-black tree verification macro. | |
#define | rt_frozen(tree) |
Determine if a red-black tree is frozen. | |
#define | rt_count(tree) |
Red-black tree count. | |
#define | rt_root(tree) |
Red-black tree root node. | |
#define | rt_comp(tree) |
Red-black tree comparison function. | |
#define | rt_extra(tree) |
Extra pointer data in a red-black tree. | |
#define | RBT_ORDER_PRE |
Preorder tree traversal method. | |
#define | RBT_ORDER_IN |
Inorder tree traversal method. | |
#define | RBT_ORDER_POST |
Postorder tree traversal method. | |
#define | RBT_ORDER_MASK |
Tree traversal method mask. | |
#define | rt_prev(tree, node_io, flags) |
Get the previous node. | |
#define | RB_NODE_MAGIC |
Red-black tree node magic number. | |
#define | RB_NODE_INIT(value) |
Red-black tree node static initializer. | |
#define | rn_verify(node) |
Red-black tree node verification macro. | |
#define | rn_color(node) |
Red-black tree node color. | |
#define | rn_tree(node) |
Red-black tree node's tree pointer. | |
#define | rn_parent(node) |
Red-black tree node's parent pointer. | |
#define | rn_left(node) |
Red-black tree node's left pointer. | |
#define | rn_right(node) |
Red-black tree node's right pointer. | |
#define | rn_key(node) |
Red-black tree node's key pointer. | |
#define | rn_value(node) |
Red-black tree node's value pointer. | |
#define | rn_isblack(node) |
Test if a given node is black. | |
#define | rn_isred(node) |
Test if a given node is red. | |
#define | rn_isleft(node) |
Test if a given node is the left node of its parent. | |
#define | rn_isright(node) |
Test if a given node is the right node of its parent. | |
#define | uncle(node) |
Locate the uncle of a node. | |
#define | flip(node) |
Flip the color of a node. | |
#define | _rn_clear(node) |
Clear a node. | |
#define | _rt_update_parent(tree, node, new) |
Update a node's parent. | |
#define | isleft(par, n) |
Determine if node is a left child of its parent. | |
#define | sel_lr(t, n) |
Select a child node based on a condition. | |
#define | sibling(par, n) |
Locate the sibling of a node. | |
#define | l_neph(par, n) |
Locate "closer" nephew of a node. | |
#define | r_neph(par, n) |
Locate "further" nephew of a node. | |
Typedefs | |
typedef _rb_tree_s | rb_tree_t |
Red-black tree. | |
typedef _rb_node_s | rb_node_t |
Red-black tree node. | |
typedef unsigned long(* | rb_iter_t )(rb_tree_t *tree, rb_node_t *node, void *extra) |
Red-black tree iteration callback. | |
typedef long(* | rb_comp_t )(rb_tree_t *tree, db_key_t *key1, db_key_t *key2) |
Red-black tree comparison callback. | |
typedef enum _rb_color_e | rb_color_t |
Red-black tree node color. | |
Enumerations | |
enum | _rb_color_e { RB_COLOR_NONE, RB_COLOR_RED, RB_COLOR_BLACK } |
Red-black tree node color. More... | |
Functions | |
long | rbtree_comp (rb_tree_t *tree, db_key_t *key1, db_key_t *key2) |
Red-black tree comparison function. | |
unsigned long | rt_init (rb_tree_t *tree, rb_comp_t comp, void *extra) |
Dynamically initialize a red-black tree. | |
unsigned long | rt_add (rb_tree_t *tree, rb_node_t *node, db_key_t *key) |
Add a node to a red-black tree. | |
unsigned long | rt_move (rb_tree_t *tree, rb_node_t *node, db_key_t *key) |
Move a node in a red-black tree. | |
unsigned long | rt_remove (rb_tree_t *tree, rb_node_t *node) |
Remove a node from a red-black tree. | |
unsigned long | rt_find (rb_tree_t *tree, rb_node_t **node_p, db_key_t *key) |
Find an entry in a red-black table. | |
unsigned long | rt_next (rb_tree_t *tree, rb_node_t **node_io, unsigned long flags) |
Get the next node. | |
unsigned long | rt_iter (rb_tree_t *tree, rb_node_t *start, rb_iter_t iter_func, void *extra, unsigned long flags) |
Iterate over each entry in a red-black tree. | |
unsigned long | rt_flush (rb_tree_t *tree, rb_iter_t flush_func, void *extra) |
Flush a red-black tree. | |
unsigned long | rn_init (rb_node_t *node, void *value) |
Dynamically initialize a red-black tree node. | |
rb_node_t * | _rb_locate (rb_tree_t *tree, rb_node_t *node, db_key_t *key) |
Locate or insert a red-black tree node. | |
void | _rb_rotate (rb_tree_t *tree, rb_node_t *child) |
Rotate tree nodes. |
|
For internal use only. This macro is used to clear a red-black tree node, so that it is no longer in the tree.
Definition at line 42 of file rt_remove.c. Referenced by rt_remove(). |
|
For internal use only.
This macro is used to update the parent of the given
Definition at line 64 of file rt_remove.c. Referenced by rt_remove(). |
|
For internal use only. This macro is used to flip the color of a specific node.
Definition at line 57 of file rt_add.c. Referenced by rt_add(). |
|
For internal use only.
This macro simply tests whether a given node
Definition at line 87 of file rt_remove.c. |
|
For internal use only.
This macro retrieves the "closer" nephew of the node
Definition at line 135 of file rt_remove.c. Referenced by rt_remove(). |
|
For internal use only.
This macro retrieves the "further" nephew of the node
Definition at line 151 of file rt_remove.c. Referenced by rt_remove(). |
|
This macro statically initializes a rb_node_t.
|
|
For internal use only. This is the magic number used for the red-black tree structure. Definition at line 2908 of file dbprim.h. Referenced by rn_init(). |
|
This macro statically initializes a rb_tree_t.
|
|
For internal use only. This is the magic number used for the red-black tree structure. Definition at line 2554 of file dbprim.h. Referenced by rt_init(). |
|
If this flag is passed to rt_iter(), an inorder iteration will be performed. Definition at line 2667 of file dbprim.h. Referenced by rt_next(). |
|
This flag mask may be used to obtain the tree traversal order. |
|
If this flag is passed to rt_iter(), a postorder iteration will be performed. Definition at line 2675 of file dbprim.h. Referenced by rt_next(). |
|
If this flag is passed to rt_iter(), a preorder iteration will be performed. Definition at line 2659 of file dbprim.h. Referenced by rt_next(). |
|
This macro retrieves the color of the
|
|
This macro safely tests whether a given red-black tree node is black.
Definition at line 3036 of file dbprim.h. Referenced by rt_add(), and rt_remove(). |
|
This macro safely tests whether a given red-black tree node is the left node of its parent.
Definition at line 3067 of file dbprim.h. Referenced by _rb_rotate(), rt_add(), and rt_next(). |
|
This macro safely tests whether a given red-black tree node is red.
Definition at line 3051 of file dbprim.h. Referenced by rt_add(), and rt_remove(). |
|
This macro safely tests whether a given red-black tree node is the right node of its parent.
|
|
This macro retrieves the key associated with the red-black tree node.
Definition at line 3007 of file dbprim.h. Referenced by _rb_locate(). |
|
This macro retrieves a pointer to the node's left node.
|
|
This macro retrieves a pointer to the node's parent node.
|
|
This macro retrieves a pointer to the node's right node.
|
|
This macro retrieves a pointer to the red-black tree the node is in.
|
|
This macro retrieves the value associated with the red-black tree's node. It may be treated as an lvalue to change that value. Care should be taken when using this option.
|
|
This macro verifies that a given pointer actually does point to a red-black tree node.
Definition at line 2934 of file dbprim.h. Referenced by rt_add(), rt_iter(), rt_move(), rt_next(), and rt_remove(). |
|
This macro retrieves the comparison function pointer.
|
|
This macro retrieves the total number of items actually in the red-black tree.
|
|
This macro retrieves the extra pointer data associated with a particular red-black tree.
|
|
This macro returns a non-zero value if the tree is currently frozen. The red-black tree may be frozen if there is an iteration in progress.
|
|
Obtains the previous node in the given iteration scheme. See rt_next() for more information.
|
|
This macro retrieves the root node of the tree.
|
|
This macro verifies that a given pointer actually does point to a red-black tree.
Definition at line 2589 of file dbprim.h. Referenced by rt_add(), rt_find(), rt_flush(), rt_iter(), rt_move(), rt_next(), and rt_remove(). |
|
For internal use only.
This macro simply returns a pointer to the right child of
Definition at line 104 of file rt_remove.c. |
|
For internal use only.
This macro retrieves the sibling of the node
Definition at line 119 of file rt_remove.c. Referenced by rt_remove(). |
|
For internal use only. This macro is used to locate the "uncle"--parent's sibling--of a given red-black tree node.
Definition at line 45 of file rt_add.c. Referenced by rt_add(). |
|
See the documentation for the enumeration _rb_color_e. |
|
This function pointer references a callback used to compare nodes in a red-black tree. It should return 0 for identical entries, less than 0 if the first key is less than the second, or greater than 0 if the first key is greater than the second.
|
|
This function pointer references a callback used by rb_iter() and rb_flush(). It should return 0 for success. A non-zero return value will terminate the operation and will become the return value of the call.
|
|
This structure represents a single node in a red-black tree. |
|
This structure is the basis of all red-black trees maintained by this library. |
|
This enumeration is used to specify the color of a node of a red-black tree. |
|
For internal use only.
This function is used to locate a red-black tree node with a key matching
Definition at line 35 of file _rb_locate.c. References RB_COLOR_BLACK, RB_COLOR_RED, _rb_node_s::rn_color, rn_key, _rb_node_s::rn_key, _rb_node_s::rn_left, _rb_node_s::rn_parent, _rb_node_s::rn_right, _rb_node_s::rn_tree, _rb_tree_s::rt_comp, _rb_tree_s::rt_count, and _rb_tree_s::rt_root. |
|
For internal use only.
This function is used to swap
Definition at line 35 of file _rb_rotate.c. References rn_isleft, _rb_node_s::rn_left, _rb_node_s::rn_parent, _rb_node_s::rn_right, and _rb_tree_s::rt_root. Referenced by rt_add(), and rt_remove(). |
|
This is a red-black tree comparison function, compatible with rb_comp_t, based around memcmp().
Definition at line 37 of file rbtree_comp.c. |
|
This function dynamically initializes a red-black tree node.
Definition at line 34 of file rn_init.c. References _db_key_s::dk_key, _db_key_s::dk_len, RB_COLOR_NONE, RB_NODE_MAGIC, _rb_node_s::rn_color, _rb_node_s::rn_key, _rb_node_s::rn_left, _rb_node_s::rn_magic, _rb_node_s::rn_parent, _rb_node_s::rn_right, _rb_node_s::rn_tree, and _rb_node_s::rn_value. |
|
This function adds a node to a red-black tree.
Definition at line 62 of file rt_add.c. References _rb_locate(), _rb_rotate(), flip, RB_COLOR_BLACK, RBT_FLAG_FREEZE, _rb_node_s::rn_color, rn_isblack, rn_isleft, rn_isred, rn_isright, _rb_node_s::rn_parent, _rb_node_s::rn_tree, rn_verify, _rb_tree_s::rt_flags, _rb_tree_s::rt_root, rt_verify, and uncle. Referenced by rt_move(). Here is the call graph for this function: ![]() |
|
This function looks up an entry matching the given
Definition at line 34 of file rt_find.c. References _rb_locate(), _rb_tree_s::rt_count, and rt_verify. Here is the call graph for this function: ![]() |
|
This function flushes a red-black tree--that is, it removes each node from the tree. If a
Definition at line 34 of file rt_flush.c. References RBT_FLAG_FREEZE, _rb_tree_s::rt_count, _rb_tree_s::rt_flags, rt_remove(), _rb_tree_s::rt_root, and rt_verify. Here is the call graph for this function: ![]() |
|
This function dynamically initializes a red-black tree.
Definition at line 34 of file rt_init.c. References RB_TREE_MAGIC, _rb_tree_s::rt_comp, _rb_tree_s::rt_count, _rb_tree_s::rt_extra, _rb_tree_s::rt_flags, _rb_tree_s::rt_magic, and _rb_tree_s::rt_root. |
|
This function iterates over every node in a red-black tree in the given traversal order, executing the given
Definition at line 34 of file rt_iter.c. References RBT_FLAG_FREEZE, RBT_ORDER_MASK, _rb_node_s::rn_tree, rn_verify, _rb_tree_s::rt_flags, rt_next(), and rt_verify. Here is the call graph for this function: ![]() |
|
This function moves an existing node in the red-black tree to correspond to the new key.
Definition at line 35 of file rt_move.c. References _rb_locate(), RBT_FLAG_FREEZE, _rb_node_s::rn_tree, rn_verify, rt_add(), _rb_tree_s::rt_flags, rt_remove(), and rt_verify. Here is the call graph for this function: ![]() |
|
This function obtains the next node in the given iteration scheme. The
Definition at line 34 of file rt_next.c. References DB_FLAG_REVERSE, RBT_ORDER_IN, RBT_ORDER_MASK, RBT_ORDER_POST, RBT_ORDER_PRE, rn_isleft, rn_isright, _rb_node_s::rn_left, _rb_node_s::rn_parent, _rb_node_s::rn_right, _rb_node_s::rn_tree, rn_verify, _rb_tree_s::rt_root, and rt_verify. Referenced by rt_iter(). |
|
This function removes the given node from the specified red-black tree.
Definition at line 154 of file rt_remove.c. References _rb_rotate(), _rn_clear, _rt_update_parent, l_neph, r_neph, RB_COLOR_BLACK, RB_COLOR_RED, RBT_FLAG_FREEZE, _rb_node_s::rn_color, rn_isblack, rn_isred, _rb_node_s::rn_left, _rb_node_s::rn_parent, _rb_node_s::rn_right, _rb_node_s::rn_tree, rn_verify, _rb_tree_s::rt_count, _rb_tree_s::rt_flags, _rb_tree_s::rt_root, rt_verify, and sibling. Referenced by rt_flush(), and rt_move(). Here is the call graph for this function: ![]() |