feat(search tree): implemented insert function
This commit is contained in:
parent
20263fe645
commit
d6f12fe535
2 changed files with 101 additions and 49 deletions
|
|
@ -33,55 +33,98 @@
|
|||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \
|
||||
typedef struct FledastyBinarySearchTreeNode_##name { \
|
||||
key_type key; \
|
||||
value_type value; \
|
||||
\
|
||||
struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \
|
||||
} FledastyBinarySearchTreeNode_##name; \
|
||||
\
|
||||
typedef struct { \
|
||||
size_t size, depth; \
|
||||
FledastyBinarySearchTreeNode_##name *top; \
|
||||
} FledastyBinarySearchTree_##name; \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree);
|
||||
#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \
|
||||
typedef struct FledastyBinarySearchTreeNode_##name { \
|
||||
key_type key; \
|
||||
value_type value; \
|
||||
\
|
||||
struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \
|
||||
} FledastyBinarySearchTreeNode_##name; \
|
||||
\
|
||||
typedef struct { \
|
||||
size_t size, depth; \
|
||||
FledastyBinarySearchTreeNode_##name *top; \
|
||||
} FledastyBinarySearchTree_##name; \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree); \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value); \
|
||||
|
||||
#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name) \
|
||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
||||
if (current_binary_search_tree != NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||
while (current_node != NULL) { \
|
||||
if (current_node->left != NULL) { \
|
||||
current_node = current_node->left; \
|
||||
} else if (current_node->right != NULL) { \
|
||||
current_node = current_node->right; \
|
||||
} else if (current_node->parent != NULL) { \
|
||||
current_node = current_node->parent; \
|
||||
if (current_node->left != NULL) { \
|
||||
if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_node->left = NULL; \
|
||||
} else { \
|
||||
if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_node->right = NULL; \
|
||||
} \
|
||||
} else { \
|
||||
hallocy_free(current_node); \
|
||||
current_node = NULL; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name, key_less_than_function) \
|
||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
||||
if (current_binary_search_tree != NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||
while (current_node != NULL) { \
|
||||
if (current_node->left != NULL) { \
|
||||
current_node = current_node->left; \
|
||||
} else if (current_node->right != NULL) { \
|
||||
current_node = current_node->right; \
|
||||
} else if (current_node->parent != NULL) { \
|
||||
current_node = current_node->parent; \
|
||||
if (current_node->left != NULL) { \
|
||||
if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_node->left = NULL; \
|
||||
} else { \
|
||||
if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_node->right = NULL; \
|
||||
} \
|
||||
} else { \
|
||||
hallocy_free(current_node); \
|
||||
current_node = NULL; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value) { \
|
||||
if (current_binary_search_tree == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \
|
||||
\
|
||||
new_node->left = NULL; \
|
||||
new_node->right = NULL; \
|
||||
new_node->parent = NULL; \
|
||||
\
|
||||
new_node->key = key; \
|
||||
new_node->value = value; \
|
||||
\
|
||||
if (current_binary_search_tree->top == NULL) { \
|
||||
current_binary_search_tree->top = new_node; \
|
||||
} else { \
|
||||
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||
while (new_node->parent == NULL) { \
|
||||
if (key_less_than_function(key, current_node->key)) { \
|
||||
if (current_node->left == NULL) { \
|
||||
new_node->parent = current_node; \
|
||||
current_node->left = new_node; \
|
||||
} else { \
|
||||
current_node = current_node->left; \
|
||||
} \
|
||||
} else { \
|
||||
if (current_node->right == NULL) { \
|
||||
new_node->parent = current_node; \
|
||||
current_node->right = new_node; \
|
||||
} else { \
|
||||
current_node = current_node->right; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
current_binary_search_tree->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
11
Tests/Main.c
11
Tests/Main.c
|
|
@ -34,6 +34,7 @@
|
|||
|
||||
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
||||
static inline size_t integer_hash_function(const int key) { return key; }
|
||||
static inline int integer_less_than_integer(const int first_key, const int second_key) { return first_key < second_key; }
|
||||
|
||||
FLEDASTY_STACK_DEFINE(int, int)
|
||||
FLEDASTY_STACK_IMPLEMENT(int, int)
|
||||
|
|
@ -54,7 +55,7 @@ FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int)
|
|||
FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
|
||||
|
||||
FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int)
|
||||
FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int)
|
||||
FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, integer_less_than_integer)
|
||||
|
||||
int main() {
|
||||
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
||||
|
|
@ -319,6 +320,14 @@ int main() {
|
|||
fledasty_string_free(&normal_test_string);
|
||||
|
||||
FledastyBinarySearchTree_int_int test_search_tree = { 0, 0, NULL };
|
||||
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 5, 10);
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 3, 6);
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 4, 8);
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 1, 2);
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 8, 16);
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 10, 20);
|
||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 7, 14);
|
||||
|
||||
fledasty_binary_search_tree_int_int_free(&test_search_tree);
|
||||
printf("Done\n");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue