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 *)); stack = realloc(stack, capacity * sizeof(vieter_tree_node *));
} }
if (node->left != NULL) { if (node->children[0] != NULL) {
stack[size] = node->left; stack[size] = node->children[0];
size++; size++;
} }
if (node->right != NULL) { if (node->children[1] != NULL) {
stack[size] = node->right; stack[size] = node->children[1];
size++; size++;
} }
@ -113,7 +113,7 @@ bool vieter_tree_validate(vieter_tree *tree) {
expected_black_nodes++; expected_black_nodes++;
} }
node = node->left; node = node->children[0];
} }
return vieter_tree_node_get(tree->root, vieter_tree_node_black) && 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. // black.
bool correctly_colored_children = bool correctly_colored_children =
vieter_tree_node_get(node, vieter_tree_node_black) || vieter_tree_node_get(node, vieter_tree_node_black) ||
((node->left == NULL || ((node->children[0] == NULL ||
vieter_tree_node_get(node->left, vieter_tree_node_black)) && vieter_tree_node_get(node->children[0], vieter_tree_node_black)) &&
(node->right == NULL || (node->children[1] == NULL ||
vieter_tree_node_get(node->right, vieter_tree_node_black))); vieter_tree_node_get(node->children[1], vieter_tree_node_black)));
return correctly_colored_children && 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) && 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); expected_black_nodes);
} }
@ -48,10 +48,10 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = node; left = node;
right = parent; right = parent;
children[0] = node->left; children[0] = node->children[0];
children[1] = node->right; children[1] = node->children[1];
children[2] = parent->right; children[2] = parent->children[1];
children[3] = grand_parent->right; children[3] = grand_parent->children[1];
} else if (node->key < parent->key && parent->key > grand_parent->key) { } else if (node->key < parent->key && parent->key > grand_parent->key) {
root->key = node->key; root->key = node->key;
root->data = node->data; root->data = node->data;
@ -62,10 +62,10 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = node; left = node;
right = parent; right = parent;
children[0] = grand_parent->left; children[0] = grand_parent->children[0];
children[1] = node->left; children[1] = node->children[0];
children[2] = node->right; children[2] = node->children[1];
children[3] = parent->right; children[3] = parent->children[1];
} else if (node->key > parent->key && parent->key < grand_parent->key) { } else if (node->key > parent->key && parent->key < grand_parent->key) {
root->key = node->key; root->key = node->key;
root->data = node->data; root->data = node->data;
@ -76,10 +76,10 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = parent; left = parent;
right = node; right = node;
children[0] = parent->left; children[0] = parent->children[0];
children[1] = node->left; children[1] = node->children[0];
children[2] = node->right; children[2] = node->children[1];
children[3] = grand_parent->right; children[3] = grand_parent->children[1];
} else { } else {
root->key = parent->key; root->key = parent->key;
root->data = parent->data; root->data = parent->data;
@ -90,18 +90,18 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
left = parent; left = parent;
right = node; right = node;
children[0] = grand_parent->left; children[0] = grand_parent->children[0];
children[1] = parent->left; children[1] = parent->children[0];
children[2] = node->left; children[2] = node->children[0];
children[3] = node->right; children[3] = node->children[1];
} }
vieter_tree_node_set(left, vieter_tree_node_black); vieter_tree_node_set(left, vieter_tree_node_black);
vieter_tree_node_set(right, vieter_tree_node_black); vieter_tree_node_set(right, vieter_tree_node_black);
vieter_tree_node_unset(root, vieter_tree_node_black); vieter_tree_node_unset(root, vieter_tree_node_black);
left->left = children[0]; left->children[0] = children[0];
left->right = children[1]; left->children[1] = children[1];
if (children[0] != NULL) if (children[0] != NULL)
children[0]->parent = left; children[0]->parent = left;
@ -109,8 +109,8 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
if (children[1] != NULL) if (children[1] != NULL)
children[1]->parent = left; children[1]->parent = left;
right->left = children[2]; right->children[0] = children[2];
right->right = children[3]; right->children[1] = children[3];
if (children[2] != NULL) if (children[2] != NULL)
children[2]->parent = right; children[2]->parent = right;
@ -118,8 +118,8 @@ vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node) {
if (children[3] != NULL) if (children[3] != NULL)
children[3]->parent = right; children[3]->parent = right;
root->left = left; root->children[0] = left;
root->right = right; root->children[1] = right;
left->parent = root; left->parent = root;
right->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; return;
} }
if (key < parent->key) { parent->children[key > parent->key] = child;
parent->left = child;
} else {
parent->right = child;
}
if (child != NULL) { if (child != NULL) {
child->parent = parent; child->parent = parent;
@ -28,14 +24,14 @@ void vieter_tree_node_replace_with_child(vieter_tree_node *to_replace,
vieter_tree_node *replacement) { vieter_tree_node *replacement) {
to_replace->key = replacement->key; to_replace->key = replacement->key;
to_replace->data = replacement->data; to_replace->data = replacement->data;
to_replace->left = replacement->left; to_replace->children[0] = replacement->children[0];
to_replace->right = replacement->right; to_replace->children[1] = replacement->children[1];
if (to_replace->left != NULL) if (to_replace->children[0] != NULL)
to_replace->left->parent = to_replace; to_replace->children[0]->parent = to_replace;
if (to_replace->right != NULL) if (to_replace->children[1] != NULL)
to_replace->right->parent = to_replace; to_replace->children[1]->parent = to_replace;
} }
vieter_tree_error vieter_tree_node_insert(vieter_tree_node *root, uint64_t key, 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; parent = node;
node = node->children[key > parent->key];
if (key < parent->key) {
node = parent->left;
} else {
node = parent->right;
}
} }
vieter_tree_node *new_node = vieter_tree_node_init(); 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; return vieter_tree_ok;
} }
if (key < node->key) { node = node->children[key > node->key];
node = node->left;
} else {
node = node->right;
}
} }
return vieter_tree_not_present; 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; *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_add_child(target->parent, target->key, NULL);
vieter_tree_node_free(target); 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 = 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) { if (target->parent != NULL) {
vieter_tree_node_add_child(target->parent, child->key, child); 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 { } else {
vieter_tree_node *replacement = target->right; vieter_tree_node *replacement = target->children[1];
while (replacement->left != NULL) { while (replacement->children[0] != NULL) {
replacement = replacement->left; replacement = replacement->children[0];
} }
// We use replacement->key here because the right child can be NULL, so // 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 // will still get placed into the right location because of how binary
// trees work. // trees work.
vieter_tree_node_add_child(replacement->parent, replacement->key, vieter_tree_node_add_child(replacement->parent, replacement->key,
replacement->right); replacement->children[1]);
target->key = replacement->key; target->key = replacement->key;
target->data = replacement->data; target->data = replacement->data;

View File

@ -6,15 +6,17 @@
#include <stdbool.h> #include <stdbool.h>
typedef enum vieter_tree_node_flag { 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; } vieter_tree_node_flag;
typedef struct vieter_tree_node { typedef struct vieter_tree_node {
uint64_t key; uint64_t key;
void *data; void *data;
struct vieter_tree_node *parent; struct vieter_tree_node *parent;
struct vieter_tree_node *left; struct vieter_tree_node *children[2];
struct vieter_tree_node *right;
uint8_t flags; uint8_t flags;
} vieter_tree_node; } vieter_tree_node;