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.
Defines | |
#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 | rt_prev(tree, node_io, flags) |
Get the previous node. | |
#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. | |
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 *, rb_node_t *, void *) |
Red-black tree iteration callback. | |
typedef long(* | rb_comp_t )(rb_tree_t *, db_key_t *, db_key_t *) |
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 | |
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. |
|
This macro statically initializes a rb_node_t.
|
|
This macro statically initializes a rb_tree_t.
|
|
If this flag is passed to rt_iter(), an inorder iteration will be performed. |
|
If this flag is passed to rt_iter(), a postorder iteration will be performed. |
|
If this flag is passed to rt_iter(), a preorder iteration will be performed. |
|
This macro retrieves the color of the
|
|
This macro safely tests whether a given red-black tree node is black.
|
|
This macro safely tests whether a given red-black tree node is the left node of its parent.
|
|
This macro safely tests whether a given red-black tree node is red.
|
|
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.
|
|
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.
|
|
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.
|
|
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. |
|
This function dynamically initializes a red-black tree node.
|
|
This function adds a node to a red-black tree.
|
|
This function looks up an entry matching the given
|
|
This function flushes a red-black tree--that is, it removes each node from the tree. If a
|
|
This function dynamically initializes a red-black tree.
|
|
This function iterates over every node in a red-black tree in the given traversal order, executing the given
|
|
This function moves an existing node in the red-black tree to correspond to the new key.
|
|
This function obtains the next node in the given iteration scheme. The
|
|
This function removes the given node from the specified red-black tree.
|