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

lib: red-black trees uses now only an entry field for compares.

parent acac3e7d
......@@ -37,15 +37,14 @@ rb_tree_new(cry_ordering_funptr compare)
struct RBNode*
rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer key, pointer data)
rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer entry)
{
assert(key != 0);
assert(entry != 0);
struct RBNode* node = cry_malloc(struct RBNode);
node->color = color;
node->parent = parent;
node->key = key;
node->data = data;
node->entry = entry;
node->left = 0;
node->right = 0;
......@@ -54,7 +53,7 @@ rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer key, po
void
rb_tree_clear(struct RBTree* tree, cry_free_funptr free_key, cry_free_funptr free_data)
rb_tree_clear(struct RBTree* tree, cry_free_funptr free_entry)
{
assert(tree != 0);
......@@ -67,15 +66,12 @@ rb_tree_clear(struct RBTree* tree, cry_free_funptr free_key, cry_free_funptr fre
void
rb_node_free(struct RBNode* node, cry_free_funptr free_key, cry_free_funptr free_data)
rb_node_free(struct RBNode* node, cry_free_funptr free_entry)
{
assert(node != 0);
if(free_key != 0)
free_key(cry_cast(pointer, node->key));
if(free_data != 0)
free_data(node->data);
if(free_entry != 0)
free_entry(cry_cast(pointer, node->entry));
cry_free(node);
}
......@@ -83,14 +79,14 @@ rb_node_free(struct RBNode* node, cry_free_funptr free_key, cry_free_funptr free
struct RBNode*
rb_tree_lookup(struct RBTree* tree, const_pointer key)
rb_tree_lookup(struct RBTree* tree, const_pointer entry)
{
assert(tree != 0 && key != 0);
assert(tree != 0 && entry != 0);
struct RBNode* node = tree->root;
while(node != 0) {
int result = tree->compare(key, node->key);
int result = tree->compare(entry, node->entry);
if(result < 0)
node = node->left;
......@@ -295,31 +291,31 @@ rb_remedy_double_black(struct RBTree* tree, struct RBNode* node_x, struct RBNode
struct RBNode*
rb_tree_insert(struct RBTree* tree, const_pointer key, pointer value)
rb_tree_insert(struct RBTree* tree, const_pointer entry)
{
assert(tree != 0 && key != 0);
assert(tree != 0 && entry != 0);
struct RBNode* node = tree->root;
struct RBNode* current_node = 0;
if(node == 0) {
tree->root = rb_node_new(BLACK, 0, key, value);
tree->root = rb_node_new(BLACK, 0, entry);
tree->nodes += 1;
return tree->root;
}
// general node insertion for binary search tree
while(current_node == 0) {
int result = tree->compare(key, node->key);
int result = tree->compare(entry, node->entry);
if(result < 0) {
if(node->left == 0)
current_node = node->left = rb_node_new(RED, node, key, value);
current_node = node->left = rb_node_new(RED, node, entry);
else
node = node->left;
} else if(result > 0) {
if(node->right == 0)
current_node = node->right = rb_node_new(RED, node, key, value);
current_node = node->right = rb_node_new(RED, node, entry);
else
node = node->right;
} else {
......@@ -356,16 +352,16 @@ rb_remove_external_node(struct RBNode* node)
struct RBNode*
rb_tree_remove(struct RBTree* tree, const_pointer key)
rb_tree_remove(struct RBTree* tree, const_pointer entry)
{
assert(tree != 0 && key != 0);
assert(tree != 0 && entry != 0);
struct RBNode* node = 0;
struct RBNode* current_node = 0;
struct RBNode* data_node = 0;
// search for a node with the same key within the binary tree
current_node = rb_tree_lookup(tree, key);
// search for a node with the same entry within the binary tree
current_node = rb_tree_lookup(tree, entry);
if(current_node == 0)
return current_node;
......@@ -377,14 +373,9 @@ rb_tree_remove(struct RBTree* tree, const_pointer key)
node = node->left;
// swap its entry information
const_pointer tmp_key = node->key;
pointer tmp_data = node->data;
node->key = current_node->key;
node->data = current_node->data;
current_node->key = tmp_key;
current_node->data = tmp_data;
const_pointer tmp_entry = node->entry;
node->entry = current_node->entry;
current_node->entry = tmp_entry;
data_node = node;
node = rb_remove_external_node(node);
......@@ -410,5 +401,3 @@ rb_tree_remove(struct RBTree* tree, const_pointer key)
return data_node;
}
......@@ -24,8 +24,7 @@
enum RBNodeColor { RED, BLACK };
struct RBNode {
const_pointer key;
pointer data;
const_pointer entry;
enum RBNodeColor color;
struct RBNode* parent;
struct RBNode* left;
......@@ -45,14 +44,14 @@ struct RBTree {
struct RBTree* rb_tree_new(cry_ordering_funptr compare);
void rb_tree_clear(struct RBTree* tree, cry_free_funptr free_key, cry_free_funptr free_data);
void rb_tree_clear(struct RBTree* tree, cry_free_funptr free_key);
struct RBNode* rb_tree_lookup(struct RBTree* tree, const_pointer key);
struct RBNode* rb_tree_insert(struct RBTree* tree, const_pointer key, pointer value);
struct RBNode* rb_tree_remove(struct RBTree* tree, const_pointer key);
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_remove(struct RBTree* tree, const_pointer entry);
struct RBNode* rb_node_new(enum RBNodeColor color, struct RBNode* parent, const_pointer key, pointer data);
void rb_node_free(struct RBNode* node, cry_free_funptr free_key, cry_free_funptr free_data);
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);
......
......@@ -12,7 +12,7 @@ test_red_black_tree_initialization(const_pointer data)
assert(tree->nodes == 0);
assert(rb_tree_lookup(tree, "unknown_key") == 0);
rb_tree_clear(tree, 0, 0);
rb_tree_clear(tree, 0);
cry_free(tree);
}
......@@ -26,7 +26,7 @@ test_red_black_tree_insertion(const_pointer data)
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys, cry_cast(int*, keys)) != 0);
assert(rb_tree_insert(tree, keys) != 0);
++keys;
}
......@@ -38,12 +38,12 @@ test_red_black_tree_insertion(const_pointer data)
struct RBNode* node = rb_tree_lookup(tree, keys);
assert(node != 0);
assert(node->key == keys);
assert(node->entry == keys);
++keys;
}
rb_tree_clear(tree, 0, 0);
rb_tree_clear(tree, 0);
cry_free(tree);
}
......@@ -58,7 +58,7 @@ test_red_black_tree_removal(const_pointer list)
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys, cry_cast(int*, keys)) != 0);
assert(rb_tree_insert(tree, keys) != 0);
++keys;
}
......@@ -68,33 +68,33 @@ test_red_black_tree_removal(const_pointer list)
// remove 3
data = rb_tree_remove(tree, keys + 4);
assert(data != 0 && data->key == keys + 4);
rb_node_free(data, 0, 0);
assert(data != 0 && data->entry == keys + 4);
rb_node_free(data, 0);
// remove 12
data = rb_tree_remove(tree, keys + 2);
assert(data != 0 && data->key == keys + 2);
rb_node_free(data, 0, 0);
assert(data != 0 && data->entry == keys + 2);
rb_node_free(data, 0);
// remove 17
data = rb_tree_remove(tree, keys + 9);
assert(data != 0 && data->key == keys + 9);
rb_node_free(data, 0, 0);
assert(data != 0 && data->entry == keys + 9);
rb_node_free(data, 0);
// remove 18
data = rb_tree_remove(tree, keys + 7);
assert(data != 0 && data->key == keys + 7);
rb_node_free(data, 0, 0);
assert(data != 0 && data->entry == keys + 7);
rb_node_free(data, 0);
// remove 15
data = rb_tree_remove(tree, keys + 3);
assert(data != 0 && data->key == keys + 3);
rb_node_free(data, 0, 0);
assert(data != 0 && data->entry == keys + 3);
rb_node_free(data, 0);
// remove 16
data = rb_tree_remove(tree, keys + 8);
assert(data != 0 && data->key == keys + 8);
rb_node_free(data, 0, 0);
assert(data != 0 && data->entry == keys + 8);
rb_node_free(data, 0);
// contains 4
assert(rb_tree_lookup(tree, keys) != 0);
......@@ -108,7 +108,7 @@ test_red_black_tree_removal(const_pointer list)
// contains 14
assert(rb_tree_lookup(tree, keys + 6) != 0);
rb_tree_clear(tree, 0, 0);
rb_tree_clear(tree, 0);
cry_free(tree);
}
......
......@@ -27,7 +27,7 @@ file_parse_red_black_tree(FILE* file)
case '+':
integer = cry_malloc(int);
*integer = atoi(buffer + 2);
if(rb_tree_insert(tree, integer, 0) == 0) {
if(rb_tree_insert(tree, integer) == 0) {
fprintf(stderr, "Value %d already added.\n", *integer);
cry_free(integer);
}
......@@ -38,7 +38,7 @@ file_parse_red_black_tree(FILE* file)
if( (node = rb_tree_remove(tree, &tmp)) == 0)
fprintf(stderr, "Value %d is not in tree.\n", tmp);
else
rb_node_free(node, cry_free, 0);
rb_node_free(node, cry_free);
break;
default:
......@@ -58,7 +58,7 @@ red_black_tree_graph(Agraph_t* graph, struct RBNode* node)
{
char key[32];
snprintf(key, sizeof(key), "%d", *cry_cast(int*, node->key));
snprintf(key, sizeof(key), "%d", *cry_cast(int*, node->entry));
Agnode_t* n = agnode(graph, key);
agset(n, "label", key);
......@@ -72,7 +72,7 @@ red_black_tree_graph(Agraph_t* graph, struct RBNode* node)
Agnode_t* child = red_black_tree_graph(graph, node->left);
agedge(graph, n, child);
} else {
snprintf(key, sizeof(key), "%d_left", *cry_cast(int*, node->key));
snprintf(key, sizeof(key), "%d_left", *cry_cast(int*, node->entry));
Agnode_t* child = agnode(graph, key);
agsafeset(child, "shape", "point", "");
agedge(graph, n, child);
......@@ -82,7 +82,7 @@ red_black_tree_graph(Agraph_t* graph, struct RBNode* node)
Agnode_t* child = red_black_tree_graph(graph, node->right);
agedge(graph, n, child);
} else {
snprintf(key, sizeof(key), "%d_right", *cry_cast(int*, node->key));
snprintf(key, sizeof(key), "%d_right", *cry_cast(int*, node->entry));
Agnode_t* child = agnode(graph, key);
agsafeset(child, "shape", "point", "");
agedge(graph, n, child);
......@@ -104,7 +104,7 @@ graph_generator(FILE* opfile, GVC_t* gvc, const char* graphtype)
if(tree->root != 0)
red_black_tree_graph(graph, tree->root);
rb_tree_clear(tree, free, 0);
rb_tree_clear(tree, free);
cry_free(tree);
if(graph != 0) {
......
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