feat(search tree): implemented min and max functions

This commit is contained in:
Mineplay 2025-09-07 11:09:09 -05:00
parent 191f57c1bd
commit 73b09b510c
2 changed files with 53 additions and 0 deletions

View file

@ -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; \

View file

@ -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;