From 91e07ff6d04bacc4451c5c985266f333690f3e5b Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 28 Aug 2025 05:17:35 -0500 Subject: [PATCH] feat(search tree): implemented get function for search tree --- Include/Fledasty/Trees/BinarySearchTree.h | 23 +++++++++++++++++++++++ Include/Fledasty/Utils/Error.h | 1 + Tests/Main.c | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index bc20566..c298be7 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -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; \ diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h index ce48894..f535aec 100644 --- a/Include/Fledasty/Utils/Error.h +++ b/Include/Fledasty/Utils/Error.h @@ -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 diff --git a/Tests/Main.c b/Tests/Main.c index d48edc3..8e0fb09 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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;