feat(search tree): implemented get function for search tree

This commit is contained in:
Mineplay 2025-08-28 05:17:35 -05:00
parent abcdb0d127
commit 91e07ff6d0
3 changed files with 28 additions and 0 deletions

View file

@ -49,6 +49,7 @@ typedef struct {
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); \
FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \
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);
@ -111,6 +112,11 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr
} else { \
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
while (new_node->parent == NULL) { \
if (compare_key_function(key, current_node->key)) { \
current_node->value = value; \
return FLEDASTY_ERROR_VALUE_REPLACED; \
} \
\
if (key_less_than_function(key, current_node->key)) { \
if (current_node->left == NULL) { \
new_node->parent = current_node; \
@ -133,6 +139,23 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \
if (current_binary_search_tree == NULL) { \
return NULL; \
} \
\
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; \
} \
} \
\
return current_node; \
} \
\
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; \

View file

@ -31,6 +31,7 @@ typedef enum {
FLEDASTY_ERROR_VALUE_NOT_FOUND = 4,
FLEDASTY_ERROR_KEY_NOT_FOUND = 5,
FLEDASTY_ERROR_INVALID_VALUE = 6,
FLEDASTY_ERROR_VALUE_REPLACED = 7
} FledastyError;
#endif

View file

@ -343,6 +343,10 @@ int main() {
}
hallocy_free(in_order_result);
FledastyBinarySearchTreeNode_int_int *node = fledasty_binary_search_tree_int_int_get(&test_search_tree, 7);
printf("Search tree get result: %d\n", node->value);
fledasty_binary_search_tree_int_int_free(&test_search_tree);
printf("Done\n");
return 0;