|
PatchworkOS
2ca1c69
A non-POSIX operating system.
|
Augmented Red-Black Tree. More...
Augmented Red-Black Tree.
A Red-Black Tree (RBT) is a tree structure that maintains sorted data to allow for efficient insertion, deletion, and lookup operations with a worst case time complexity of O(log n).
The name "Red-Black Tree" can be a bit confusing. To the user of the tree, it simply acts as a highly optimized sorted linked list.
The tree structure allows for more efficient operations compared to a standard linked list (O(log n) vs O(n)), and the red-black properties ensure that the tree remains balanced, preventing it from degenerating into a linear structure. However, the user of the tree does not need to be concerned with these implementation details.
O(1) access.The tree supports an optional update callback that is called whenever a node is inserted, removed or swapped. This allows for the tree to be "augmented" with additional data. For example, if you wanted to track the global minimum of some value in each node, you could do so by updating the minimum value in the update callback, such that you no longer need to traverse the tree to find the minimum. Very useful for the scheduler.
Data Structures | |
| struct | rbnode_t |
| Red-Black Tree Node. More... | |
| struct | rbtree_t |
| Red-Black Tree. More... | |
Macros | |
| #define | RBNODE_OPPOSITE(direction) ((rbnode_direction_t)(1 - (direction))) |
| Get the opposite direction (left <-> right). | |
| #define | RBNODE_FROM_PARENT(node) ((rbnode_direction_t)((node)->parent->children[RBNODE_RIGHT] == (node) ? RBNODE_RIGHT : RBNODE_LEFT)) |
| Get the direction of a node from its parent. | |
| #define | RBNODE_CREATE |
| Create a Red-Black Tree Node initializer. | |
| #define | RBTREE_FOR_EACH(elem, tree, member) |
| Iterates over a Red-Black Tree in ascending order. | |
| #define | RBTREE_FOR_EACH_REVERSE(elem, tree, member) |
| Iterates over a Red-Black Tree in descending order. | |
Typedefs | |
| typedef int64_t(* | rbnode_compare_t) (const rbnode_t *a, const rbnode_t *b) |
| Comparison function for Red-Black Tree nodes. | |
| typedef void(* | rbnode_update_t) (rbnode_t *node) |
| Update function for Red-Black Tree nodes. | |
Enumerations | |
| enum | rbnode_color_t { RBNODE_RED = 0 , RBNODE_BLACK = 1 } |
| Red-Black Tree Node Colors. More... | |
| enum | rbnode_direction_t { RBNODE_LEFT = 0 , RBNODE_RIGHT = 1 , RBNODE_AMOUNT = 2 } |
| Red-Black Tree Node Directions. More... | |
Functions | |
| void | rbtree_init (rbtree_t *tree, rbnode_compare_t compare, rbnode_update_t update) |
| Initialize a Red-Black Tree. | |
| rbnode_t * | rbtree_rotate (rbtree_t *tree, rbnode_t *node, rbnode_direction_t direction) |
| Rotate a node in the Red-Black Tree. | |
| rbnode_t * | rbtree_find_min (rbnode_t *node) |
| Find the minimum node in a subtree. | |
| rbnode_t * | rbtree_find_max (rbnode_t *node) |
| Find the maximum node in a subtree. | |
| void | rbtree_swap (rbtree_t *tree, rbnode_t *a, rbnode_t *b) |
| Swap two nodes in the Red-Black Tree. | |
| void | rbtree_insert (rbtree_t *tree, rbnode_t *node) |
| Insert a node into the Red-Black Tree. | |
| void | rbtree_remove (rbtree_t *tree, rbnode_t *node) |
| Remove a node from the Red-Black Tree. | |
| void | rbtree_fix (rbtree_t *tree, rbnode_t *node) |
| Move the node to its correct position in the Red-Black Tree. | |
| bool | rbtree_is_empty (const rbtree_t *tree) |
| Check if the Red-Black Tree is empty. | |
| rbnode_t * | rbtree_next (const rbnode_t *node) |
| Get the next node in the tree, in predecessor order. | |
| rbnode_t * | rbtree_prev (const rbnode_t *node) |
| Get the previous node in the tree, in predecessor order. | |
| #define RBNODE_OPPOSITE | ( | direction | ) | ((rbnode_direction_t)(1 - (direction))) |
| #define RBNODE_FROM_PARENT | ( | node | ) | ((rbnode_direction_t)((node)->parent->children[RBNODE_RIGHT] == (node) ? RBNODE_RIGHT : RBNODE_LEFT)) |
| #define RBNODE_CREATE |
Create a Red-Black Tree Node initializer.
| #define RBTREE_FOR_EACH | ( | elem, | |
| tree, | |||
| member | |||
| ) |
Iterates over a Red-Black Tree in ascending order.
| #define RBTREE_FOR_EACH_REVERSE | ( | elem, | |
| tree, | |||
| member | |||
| ) |
Iterates over a Red-Black Tree in descending order.
Comparison function for Red-Black Tree nodes.
Should return:
a is less than b.a is equal to b.a is greater than b.| a | The first node to compare. |
| b | The second node to compare. |
| typedef void(* rbnode_update_t) (rbnode_t *node) |
| enum rbnode_color_t |
| enum rbnode_direction_t |
| void rbtree_init | ( | rbtree_t * | tree, |
| rbnode_compare_t | compare, | ||
| rbnode_update_t | update | ||
| ) |
| rbnode_t * rbtree_rotate | ( | rbtree_t * | tree, |
| rbnode_t * | node, | ||
| rbnode_direction_t | direction | ||
| ) |
Rotate a node in the Red-Black Tree.
| tree | The tree containing the node to rotate. |
| node | The node to rotate. |
| direction | The direction to rotate. |
Definition at line 41 of file rbtree.c.
Swap two nodes in the Red-Black Tree.
Needed as the structure is intrusive, so we can't just swap the data.
| tree | The tree containing the nodes to swap. |
| a | The first node to swap. |
| b | The second node to swap. |
Definition at line 216 of file rbtree.c.
Move the node to its correct position in the Red-Black Tree.
Should be called whenever the metric used for comparison changes.
rbtree_remove() followed by rbtree_insert(), could be optimized?| tree | The tree containing the node to update. |
| node | The node to update. |
Definition at line 530 of file rbtree.c.