feat(search tree): implemented delete function
This commit is contained in:
parent
ef53a1371a
commit
abcdb0d127
2 changed files with 72 additions and 4 deletions
|
|
@ -49,10 +49,11 @@ typedef struct {
|
||||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree); \
|
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); \
|
FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value); \
|
||||||
|
FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \
|
||||||
\
|
\
|
||||||
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree);
|
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree);
|
||||||
|
|
||||||
#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name, key_less_than_function) \
|
#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name, compare_key_function, key_less_than_function) \
|
||||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
||||||
if (current_binary_search_tree != NULL) { \
|
if (current_binary_search_tree != NULL) { \
|
||||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||||
|
|
@ -132,6 +133,67 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr
|
||||||
return FLEDASTY_ERROR_NONE; \
|
return FLEDASTY_ERROR_NONE; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \
|
||||||
|
if (current_binary_search_tree == NULL) { \
|
||||||
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||||
|
while (current_node != NULL && !compare_key_function(current_node->key, key)) { \
|
||||||
|
if (key_less_than_function(key, current_node->key)) { \
|
||||||
|
current_node = current_node->left; \
|
||||||
|
} else { \
|
||||||
|
current_node = current_node->right; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (current_node == NULL) { \
|
||||||
|
return FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
FledastyBinarySearchTreeNode_##name *replacement_node = current_node->right; \
|
||||||
|
if (current_node->left != NULL) { \
|
||||||
|
replacement_node = current_node->left; \
|
||||||
|
while (replacement_node->right != NULL) { \
|
||||||
|
replacement_node = replacement_node->right; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (replacement_node != NULL) { \
|
||||||
|
replacement_node->left = current_node->left; \
|
||||||
|
if (current_node->left != NULL) { \
|
||||||
|
current_node->left->parent = replacement_node; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
replacement_node->right = current_node->right; \
|
||||||
|
if (current_node->right != NULL) { \
|
||||||
|
current_node->right->parent = replacement_node; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (replacement_node->parent->right == replacement_node) { \
|
||||||
|
replacement_node->parent->right = NULL; \
|
||||||
|
} else { \
|
||||||
|
replacement_node->parent->left = NULL; \
|
||||||
|
} \
|
||||||
|
replacement_node->parent = current_node->parent; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (current_node->parent != NULL) { \
|
||||||
|
if(current_node->parent->left == current_node) { \
|
||||||
|
current_node->parent->left = replacement_node; \
|
||||||
|
} else { \
|
||||||
|
current_node->parent->right = replacement_node; \
|
||||||
|
} \
|
||||||
|
} else { \
|
||||||
|
current_binary_search_tree->top = replacement_node; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
hallocy_free(current_node); \
|
||||||
|
current_binary_search_tree->size -= 1; \
|
||||||
|
\
|
||||||
|
return FLEDASTY_ERROR_NONE; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
||||||
if (current_binary_search_tree == NULL) { \
|
if (current_binary_search_tree == NULL) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
|
|
|
||||||
12
Tests/Main.c
12
Tests/Main.c
|
|
@ -55,7 +55,7 @@ FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int)
|
||||||
FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
|
FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
|
||||||
|
|
||||||
FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int)
|
FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int)
|
||||||
FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, integer_less_than_integer)
|
FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, compare_integers, integer_less_than_integer)
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
||||||
|
|
@ -330,10 +330,16 @@ int main() {
|
||||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 7, 14);
|
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 7, 14);
|
||||||
|
|
||||||
FledastyBinarySearchTreeNode_int_int **in_order_result = fledasty_binary_search_tree_int_int_in_order_traversal(&test_search_tree);
|
FledastyBinarySearchTreeNode_int_int **in_order_result = fledasty_binary_search_tree_int_int_in_order_traversal(&test_search_tree);
|
||||||
if (in_order_result != NULL) {
|
|
||||||
for (int i = 0; i < test_search_tree.size; i += 1) {
|
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||||
printf("%d\n", in_order_result[i]->key);
|
printf("In order (After Insert): %d\n", in_order_result[i]->key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hallocy_free(in_order_result);
|
||||||
|
|
||||||
|
printf("Error code: %d\n", fledasty_binary_search_tree_int_int_remove(&test_search_tree, 3));
|
||||||
|
in_order_result = fledasty_binary_search_tree_int_int_in_order_traversal(&test_search_tree);
|
||||||
|
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||||
|
printf("In order (After Remove): %d\n", in_order_result[i]->key);
|
||||||
}
|
}
|
||||||
|
|
||||||
hallocy_free(in_order_result);
|
hallocy_free(in_order_result);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue