refactor(tree): store children as static array instead

dev^2
Jef Roosens 2023-01-29 20:33:11 +01:00
parent 06fca6f5ae
commit 193fc4b454
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 54 additions and 65 deletions

View File

@ -80,13 +80,13 @@ void vieter_tree_free(vieter_tree *tree) {
stack = realloc(stack, capacity * sizeof(vieter_tree_node *));
}
if (node->left != NULL) {
stack[size] = node->left;
if (node->children[0] != NULL) {
stack[size] = node->children[0];
size++;
}
if (node->right != NULL) {
stack[size] = node->right;
if (node->children[1] != NULL) {
stack[size] = node->children[1];
size++;
}
@ -113,7 +113,7 @@ bool vieter_tree_validate(vieter_tree *tree) {
expected_black_nodes++;
}
node = node->left;
node = node->children[0];
}
return vieter_tree_node_get(tree->root, vieter_tree_node_black) &&

View File

@ -16,15 +16,15 @@ bool vieter_tree_node_validate(vieter_tree_node *node,
// black.
bool correctly_colored_children =
vieter_tree_node_get(node, vieter_tree_node_black) ||
((node->left == NULL ||
vieter_tree_node_get(node->left, vieter_tree_node_black)) &&
(node->right == NULL ||
vieter_tree_node_get(node->right, vieter_tree_node_black)));
((node->children[0] == NULL ||
vieter_tree_node_get(node->children[0], vieter_tree_node_black)) &&
(node->children[1] == NULL ||
vieter_tree_node_get(node->children[1], vieter_tree_node_black)));
return correctly_colored_children &&
vieter_tree_node_validate(node->left, passed_black_nodes,
vieter_tree_node_validate(node->children[0], passed_black_nodes,
expected_black_nodes) &&
vieter_tree_node_validate(node->right, passed_black_nodes,
vieter_tree_node_validate(node->children[1], passed_black_nodes,
expected_black_nodes);
}
@ -48,10 +48,10 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = node;
right = parent;
children[0] = node->left;
children[1] = node->right;
children[2] = parent->right;
children[3] = grand_parent->right;
children[0] = node->children[0];
children[1] = node->children[1];
children[2] = parent->children[1];
children[3] = grand_parent->children[1];
} else if (node->key < parent->key && parent->key > grand_parent->key) {
root->key = node->key;
root->data = node->data;
@ -62,10 +62,10 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = node;
right = parent;
children[0] = grand_parent->left;
children[1] = node->left;
children[2] = node->right;
children[3] = parent->right;
children[0] = grand_parent->children[0];
children[1] = node->children[0];
children[2] = node->children[1];
children[3] = parent->children[1];
} else if (node->key > parent->key && parent->key < grand_parent->key) {
root->key = node->key;
root->data = node->data;
@ -76,10 +76,10 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = parent;
right = node;
children[0] = parent->left;
children[1] = node->left;
children[2] = node->right;
children[3] = grand_parent->right;
children[0] = parent->children[0];
children[1] = node->children[0];
children[2] = node->children[1];
children[3] = grand_parent->children[1];
} else {
root->key = parent->key;
root->data = parent->data;
@ -90,18 +90,18 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = parent;
right = node;
children[0] = grand_parent->left;
children[1] = parent->left;
children[2] = node->left;
children[3] = node->right;
children[0] = grand_parent->children[0];
children[1] = parent->children[0];
children[2] = node->children[0];
children[3] = node->children[1];
}
vieter_tree_node_set(left, vieter_tree_node_black);
vieter_tree_node_set(right, vieter_tree_node_black);
vieter_tree_node_unset(root, vieter_tree_node_black);
left->left = children[0];
left->right = children[1];
left->children[0] = children[0];
left->children[1] = children[1];
if (children[0] != NULL)
children[0]->parent = left;
@ -109,8 +109,8 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
if (children[1] != NULL)
children[1]->parent = left;
right->left = children[2];
right->right = children[3];
right->children[0] = children[2];
right->children[1] = children[3];
if (children[2] != NULL)
children[2]->parent = right;
@ -118,8 +118,8 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
if (children[3] != NULL)
children[3]->parent = right;
root->left = left;
root->right = right;
root->children[0] = left;
root->children[1] = right;
left->parent = root;
right->parent = root;

View File

@ -13,11 +13,7 @@ void vieter_tree_node_add_child(vieter_tree_node *parent, uint64_t key,
return;
}
if (key < parent->key) {
parent->left = child;
} else {
parent->right = child;
}
parent->children[key > parent->key] = child;
if (child != NULL) {
child->parent = parent;
@ -28,14 +24,14 @@ void vieter_tree_node_replace_with_child(vieter_tree_node *to_replace,
vieter_tree_node *replacement) {
to_replace->key = replacement->key;
to_replace->data = replacement->data;
to_replace->left = replacement->left;
to_replace->right = replacement->right;
to_replace->children[0] = replacement->children[0];
to_replace->children[1] = replacement->children[1];
if (to_replace->left != NULL)
to_replace->left->parent = to_replace;
if (to_replace->children[0] != NULL)
to_replace->children[0]->parent = to_replace;
if (to_replace->right != NULL)
to_replace->right->parent = to_replace;
if (to_replace->children[1] != NULL)
to_replace->children[1]->parent = to_replace;
}
vieter_tree_error vieter_tree_node_insert(vieter_tree_node *root, uint64_t key,
@ -49,12 +45,7 @@ vieter_tree_error vieter_tree_node_insert(vieter_tree_node *root, uint64_t key,
}
parent = node;
if (key < parent->key) {
node = parent->left;
} else {
node = parent->right;
}
node = node->children[key > parent->key];
}
vieter_tree_node *new_node = vieter_tree_node_init();
@ -79,11 +70,7 @@ vieter_tree_error vieter_tree_node_search_node(vieter_tree_node **out,
return vieter_tree_ok;
}
if (key < node->key) {
node = node->left;
} else {
node = node->right;
}
node = node->children[key > node->key];
}
return vieter_tree_not_present;
@ -114,13 +101,13 @@ vieter_tree_error vieter_tree_node_remove(void **out, vieter_tree_node *root,
*out = target->data;
if (target->left == NULL && target->right == NULL) {
if (target->children[0] == NULL && target->children[1] == NULL) {
vieter_tree_node_add_child(target->parent, target->key, NULL);
vieter_tree_node_free(target);
} else if ((target->left == NULL) ^ (target->right == NULL)) {
} else if ((target->children[0] == NULL) ^ (target->children[1] == NULL)) {
vieter_tree_node *child =
target->left != NULL ? target->left : target->right;
target->children[0] != NULL ? target->children[0] : target->children[1];
if (target->parent != NULL) {
vieter_tree_node_add_child(target->parent, child->key, child);
@ -131,10 +118,10 @@ vieter_tree_error vieter_tree_node_remove(void **out, vieter_tree_node *root,
}
} else {
vieter_tree_node *replacement = target->right;
vieter_tree_node *replacement = target->children[1];
while (replacement->left != NULL) {
replacement = replacement->left;
while (replacement->children[0] != NULL) {
replacement = replacement->children[0];
}
// We use replacement->key here because the right child can be NULL, so
@ -142,7 +129,7 @@ vieter_tree_error vieter_tree_node_remove(void **out, vieter_tree_node *root,
// will still get placed into the right location because of how binary
// trees work.
vieter_tree_node_add_child(replacement->parent, replacement->key,
replacement->right);
replacement->children[1]);
target->key = replacement->key;
target->data = replacement->data;

View File

@ -6,15 +6,17 @@
#include <stdbool.h>
typedef enum vieter_tree_node_flag {
vieter_tree_node_black = ((uint8_t)1) << 0
vieter_tree_node_black = ((uint8_t)1) << 0,
// This flag is used to index the children array. If this flag isn't set,
// it'll evaluate to false, or index 0 in the children array.
vieter_tree_node_right = ((uint8_t)1) << 1
} vieter_tree_node_flag;
typedef struct vieter_tree_node {
uint64_t key;
void *data;
struct vieter_tree_node *parent;
struct vieter_tree_node *left;
struct vieter_tree_node *right;
struct vieter_tree_node *children[2];
uint8_t flags;
} vieter_tree_node;