Commit b52f8421 authored by Chris Müller's avatar Chris Müller

lib: add iterative node traversal methods in red-black trees.

lib: add release code for rb_tree_clear.
parent 1ec2c1af
...@@ -52,13 +52,24 @@ rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer entry) ...@@ -52,13 +52,24 @@ rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer entry)
} }
static rb_subtree_free(struct RBNode* node, cry_free_funptr fun)
{
if(node == 0)
return;
rb_subtree_free(node->left, fun);
rb_subtree_free(node->right, fun);
rb_node_free(node, fun);
}
void void
rb_tree_clear(struct RBTree* tree, cry_free_funptr free_entry) rb_tree_clear(struct RBTree* tree, cry_free_funptr fun)
{ {
assert(tree != 0); assert(tree != 0);
// free elements rb_subtree_free(tree->root, fun);
tree->root = 0; tree->root = 0;
tree->nodes = 0; tree->nodes = 0;
...@@ -401,3 +412,95 @@ rb_tree_remove(struct RBTree* tree, const_pointer entry) ...@@ -401,3 +412,95 @@ rb_tree_remove(struct RBTree* tree, const_pointer entry)
return data_node; return data_node;
} }
struct RBNode*
rb_node_first(struct RBTree* tree)
{
assert(tree != 0);
if(tree->root == 0)
return 0;
struct RBNode* node = tree->root;
while(node->left != 0) {
node = node->left;
}
return node;
}
struct RBNode*
rb_node_last(struct RBTree* tree)
{
assert(tree != 0);
if(tree->root == 0)
return 0;
struct RBNode* node = tree->root;
while(node->right != 0) {
node = node->right;
}
return node;
}
struct RBNode*
rb_node_next(struct RBNode* node)
{
assert(node != 0);
struct RBNode* tmp = 0;
if(node->right != 0) {
tmp = node->right;
while(tmp->left != 0)
tmp = tmp->left;
return tmp;
}
tmp = node->parent;
while(tmp != 0 && node == tmp->right) {
node = tmp;
tmp = tmp->parent;
}
return tmp;
}
struct RBNode*
rb_node_prev(struct RBNode* node)
{
assert(node != 0);
struct RBNode* tmp = 0;
if(node->left != 0) {
tmp = node->left;
while(tmp->right != 0)
tmp = tmp->right;
return tmp;
}
tmp = node->parent;
while(tmp != 0 && node == tmp->left) {
node = tmp;
tmp = tmp->parent;
}
return tmp;
}
...@@ -44,14 +44,19 @@ struct RBTree { ...@@ -44,14 +44,19 @@ struct RBTree {
struct RBTree* rb_tree_new(cry_ordering_funptr compare); struct RBTree* rb_tree_new(cry_ordering_funptr compare);
void rb_tree_clear(struct RBTree* tree, cry_free_funptr free_key); void rb_tree_clear(struct RBTree* tree, cry_free_funptr fun);
struct RBNode* rb_tree_lookup(struct RBTree* tree, const_pointer entry); struct RBNode* rb_tree_lookup(struct RBTree* tree, const_pointer entry);
struct RBNode* rb_tree_insert(struct RBTree* tree, const_pointer entry); struct RBNode* rb_tree_insert(struct RBTree* tree, const_pointer entry);
struct RBNode* rb_tree_remove(struct RBTree* tree, const_pointer entry); struct RBNode* rb_tree_remove(struct RBTree* tree, const_pointer entry);
struct RBNode* rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer entry); struct RBNode* rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer entry);
void rb_node_free(struct RBNode* node, cry_free_funptr entry_key); void rb_node_free(struct RBNode* node, cry_free_funptr fun);
struct RBNode* rb_node_first(struct RBTree* tree);
struct RBNode* rb_node_last(struct RBTree* tree);
struct RBNode* rb_node_next(struct RBNode* node);
struct RBNode* rb_node_prev(struct RBNode* node);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment