feat(tree): started standard binary tree
parent
7fb172c291
commit
2ecd2eae4d
6
Makefile
6
Makefile
|
@ -25,7 +25,7 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
|||
# object file is also recompiled if only a header is changed.
|
||||
# -MP: generate a dummy target for every header file (according to the docs it
|
||||
# prevents some errors when removing header files)
|
||||
CFLAGS ?= -MMD -MP -Wall -Werror -Wextra
|
||||
CFLAGS ?= -MMD -MP -Wall -Wextra
|
||||
CFLAGS += $(INC_FLAGS)
|
||||
|
||||
.PHONY: all
|
||||
|
@ -48,6 +48,10 @@ $(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
|||
test: build-test
|
||||
@ $(foreach bin,$(BINS_TEST),./$(bin);)
|
||||
|
||||
test-mem: build-test
|
||||
@ $(foreach bin,$(BINS_TEST),valgrind --tool=memcheck --error-exitcode=1 \
|
||||
--track-origins=yes --leak-check=full ./$(bin);)
|
||||
|
||||
.PHONY: build-test
|
||||
build-test: $(BINS_TEST)
|
||||
|
||||
|
|
|
@ -2,14 +2,29 @@
|
|||
#define VIETER_TREE
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct vieter_tree vieter_tree;
|
||||
|
||||
typedef enum vieter_tree_error {
|
||||
vieter_tree_ok = 0,
|
||||
vieter_tree_already_present = 1
|
||||
vieter_tree_already_present = 1,
|
||||
vieter_tree_not_present = 2
|
||||
} vieter_tree_error;
|
||||
|
||||
vieter_tree_error vieter_tree_insert(uint64_t key, void *data);
|
||||
vieter_tree *vieter_tree_init();
|
||||
|
||||
void vieter_tree_free(vieter_tree *tree);
|
||||
|
||||
vieter_tree_error vieter_tree_insert(vieter_tree *tree, uint64_t key,
|
||||
void *data);
|
||||
|
||||
vieter_tree_error vieter_tree_search(void **out, vieter_tree *tree,
|
||||
uint64_t key);
|
||||
|
||||
vieter_tree_error vieter_tree_remove(void **out, vieter_tree *tree,
|
||||
uint64_t key);
|
||||
|
||||
uint64_t vieter_tree_size(vieter_tree *tree);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
#include "node.h"
|
||||
|
||||
vieter_tree_node *vieter_tree_node_init() {
|
||||
return calloc(1, sizeof(vieter_tree_node));
|
||||
}
|
||||
|
||||
vieter_tree_error vieter_tree_node_insert(vieter_tree_node *node, uint64_t key,
|
||||
void *data) {
|
||||
vieter_tree_node *parent = node;
|
||||
|
||||
while (node != NULL) {
|
||||
if (node->key == key) {
|
||||
return vieter_tree_already_present;
|
||||
}
|
||||
|
||||
parent = node;
|
||||
|
||||
if (key < node->key) {
|
||||
node = parent->left;
|
||||
} else {
|
||||
node = parent->right;
|
||||
}
|
||||
}
|
||||
|
||||
vieter_tree_node *new_node = vieter_tree_node_init();
|
||||
new_node->key = key;
|
||||
new_node->data = data;
|
||||
new_node->parent = parent;
|
||||
|
||||
if (key < node->key) {
|
||||
parent->left = new_node;
|
||||
} else {
|
||||
parent->right = new_node;
|
||||
}
|
||||
|
||||
return vieter_tree_ok;
|
||||
}
|
||||
|
||||
vieter_tree_error vieter_tree_node_search(void **out, vieter_tree_node *node,
|
||||
uint64_t key) {
|
||||
while (node != NULL) {
|
||||
if (node->key == key) {
|
||||
*out = node->data;
|
||||
|
||||
return vieter_tree_ok;
|
||||
}
|
||||
|
||||
if (key < node->key) {
|
||||
node = node->left;
|
||||
} else {
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
|
||||
return vieter_tree_not_present;
|
||||
}
|
||||
|
||||
vieter_tree_error vieter_tree_node_remove(void **out, vieter_tree_node *node,
|
||||
uint64_t key) {
|
||||
return vieter_tree_not_present;
|
||||
}
|
|
@ -1,13 +1,23 @@
|
|||
#ifndef VIETER_BALANCED_TREE_NODE
|
||||
#define VIETER_BALANCED_TREE_NODE
|
||||
#ifndef VIETER_TREE_NODE
|
||||
#define VIETER_TREE_NODE
|
||||
|
||||
#include "vieter_tree.h"
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
} vieter_tree_node;
|
||||
|
||||
vieter_tree_node *vieter_tree_node_init();
|
||||
|
||||
vieter_tree_error vieter_tree_node_insert(vieter_tree_node *node, uint64_t key, void *data);
|
||||
|
||||
vieter_tree_error vieter_tree_node_search(void **out, vieter_tree_node *node, uint64_t key);
|
||||
|
||||
vieter_tree_error vieter_tree_node_remove(void **out, vieter_tree_node *node, uint64_t key);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,59 @@
|
|||
#include "vieter_tree.h"
|
||||
#include "node.h"
|
||||
#include "vieter_tree.h"
|
||||
|
||||
struct vieter_tree {
|
||||
uint64_t size;
|
||||
vieter_tree_node *root;
|
||||
};
|
||||
|
||||
vieter_tree *vieter_tree_init() { return calloc(1, sizeof(vieter_tree)); }
|
||||
|
||||
uint64_t vieter_tree_size(vieter_tree *tree) { return tree->size; }
|
||||
|
||||
vieter_tree_error vieter_tree_insert(vieter_tree *tree, uint64_t key,
|
||||
void *data) {
|
||||
if (tree->size == 0) {
|
||||
tree->root = vieter_tree_node_init();
|
||||
tree->root->key = key;
|
||||
tree->root->data = data;
|
||||
tree->size = 1;
|
||||
|
||||
return vieter_tree_ok;
|
||||
}
|
||||
|
||||
vieter_tree_error res = vieter_tree_node_insert(tree->root, key, data);
|
||||
|
||||
if (res != vieter_tree_ok) {
|
||||
return res;
|
||||
}
|
||||
|
||||
tree->size++;
|
||||
|
||||
return vieter_tree_ok;
|
||||
}
|
||||
|
||||
vieter_tree_error vieter_tree_search(void **out, vieter_tree *tree,
|
||||
uint64_t key) {
|
||||
if (tree->size == 0) {
|
||||
return vieter_tree_not_present;
|
||||
}
|
||||
|
||||
return vieter_tree_node_search(out, tree->root, key);
|
||||
}
|
||||
|
||||
vieter_tree_error vieter_tree_remove(void **out, vieter_tree *tree,
|
||||
uint64_t key) {
|
||||
if (tree->size == 0) {
|
||||
return vieter_tree_not_present;
|
||||
}
|
||||
|
||||
vieter_tree_error res = vieter_tree_node_remove(out, tree->root, key);
|
||||
|
||||
if (res != vieter_tree_ok) {
|
||||
return res;
|
||||
}
|
||||
|
||||
tree->size--;
|
||||
|
||||
return vieter_tree_ok;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
#include "acutest.h"
|
||||
#include "vieter_tree.h"
|
||||
|
||||
#define TEST_SIZE(tree, size) \
|
||||
TEST_CHECK(vieter_tree_size(tree) == size); \
|
||||
TEST_MSG("Size: %zu", vieter_tree_size(tree))
|
||||
|
||||
void test_init() {
|
||||
vieter_tree *tree = vieter_tree_init();
|
||||
TEST_CHECK(tree != NULL);
|
||||
TEST_SIZE(tree, 0);
|
||||
/* vieter_tree_free(tree); */
|
||||
}
|
||||
|
||||
void test_insert() {
|
||||
vieter_tree *tree = vieter_tree_init();
|
||||
|
||||
for (uint64_t i = 0; i < 25; i++) {
|
||||
TEST_CHECK(vieter_tree_insert(tree, i, NULL) == vieter_tree_ok);
|
||||
}
|
||||
|
||||
void *out;
|
||||
|
||||
for (uint64_t i = 0; i < 25; i++) {
|
||||
TEST_CHECK(vieter_tree_insert(tree, i, NULL) == vieter_tree_already_present);
|
||||
TEST_CHECK(vieter_tree_search(&out, tree, i) == vieter_tree_ok);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{NULL, NULL}
|
||||
};
|
Loading…
Reference in New Issue