From 73b09b510cd62fe61067d2fdcbc056cf33ca4501 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 7 Sep 2025 11:09:09 -0500 Subject: [PATCH] feat(search tree): implemented min and max functions --- Include/Fledasty/Trees/BinarySearchTree.h | 45 +++++++++++++++++++++++ Tests/Main.c | 8 ++++ 2 files changed, 53 insertions(+) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index d6cb787..39c7bf5 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -52,6 +52,9 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr FledastyError fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type *out); \ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \ \ +FledastyError fledasty_binary_search_tree_##name##_min(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type *key_out, value_type *value_out); \ +FledastyError fledasty_binary_search_tree_##name##_max(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type *key_out, value_type *value_out); \ + \ FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out); \ FledastyError fledasty_binary_search_tree_##name##_preorder_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out); \ FledastyError fledasty_binary_search_tree_##name##_post_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out); \ @@ -220,6 +223,48 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr return FLEDASTY_ERROR_NONE; \ } \ \ +FledastyError fledasty_binary_search_tree_##name##_min(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type *key_out, value_type *value_out) { \ + if (current_binary_search_tree == NULL || current_binary_search_tree->top == NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node->left != NULL) { \ + current_node = current_node->left; \ + } \ + \ + if (key_out != NULL) { \ + *key_out = current_node->key; \ + } \ + \ + if (value_out != NULL) { \ + *value_out = current_node->value; \ + } \ + \ + return FLEDASTY_ERROR_NONE; \ +} \ + \ +FledastyError fledasty_binary_search_tree_##name##_max(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type *key_out, value_type *value_out) { \ + if (current_binary_search_tree == NULL || current_binary_search_tree->top == NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node->right != NULL) { \ + current_node = current_node->right; \ + } \ + \ + if (key_out != NULL) { \ + *key_out = current_node->key; \ + } \ + \ + if (value_out != NULL) { \ + *value_out = current_node->value; \ + } \ + \ + return FLEDASTY_ERROR_NONE; \ +} \ + \ FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out) { \ if (current_binary_search_tree == NULL || out == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ diff --git a/Tests/Main.c b/Tests/Main.c index 5fa65e7..aa67066 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -369,6 +369,14 @@ int main() { printf("Search tree has key: 10\n"); } + int min_key, min_value; + fledasty_binary_search_tree_int_int_min(&test_search_tree, &min_key, &min_value); + printf("Smallest key is %d with the value %d\n", min_key, min_value); + + int max_key, max_value; + fledasty_binary_search_tree_int_int_max(&test_search_tree, &max_key, &max_value); + printf("Biggest key is %d with the value %d\n", max_key, max_value); + fledasty_binary_search_tree_int_int_free(&test_search_tree); printf("Done\n"); return 0;