refactor(search tree): made in order traversal output consistent with rest of the library

This commit is contained in:
Mineplay 2025-08-30 13:35:09 -05:00
parent c8c2d5623d
commit 86acb0560a
3 changed files with 36 additions and 35 deletions

View file

@ -138,7 +138,7 @@ FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *curr
} \
\
FledastyError fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key, value_type *out) { \
if (current_hash_table == NULL) { \
if (current_hash_table == NULL || out == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\

View file

@ -33,26 +33,26 @@
#include "../Utils/Error.h"
#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \
typedef struct FledastyBinarySearchTreeNode_##name { \
key_type key; \
value_type value; \
\
struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \
} FledastyBinarySearchTreeNode_##name; \
\
typedef struct { \
size_t size, depth; \
FledastyBinarySearchTreeNode_##name *top; \
} FledastyBinarySearchTree_##name; \
\
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##_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); \
\
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree);
#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \
typedef struct FledastyBinarySearchTreeNode_##name { \
key_type key; \
value_type value; \
\
struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \
} FledastyBinarySearchTreeNode_##name; \
\
typedef struct { \
size_t size, depth; \
FledastyBinarySearchTreeNode_##name *top; \
} FledastyBinarySearchTree_##name; \
\
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##_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##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out);
#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) { \
@ -141,7 +141,7 @@ 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) { \
if (current_binary_search_tree == NULL) { \
if (current_binary_search_tree == NULL || out == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
@ -223,20 +223,20 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
if (current_binary_search_tree == NULL) { \
return NULL; \
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; \
} \
\
FledastyBinarySearchTreeNode_##name **in_order_list = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \
if (in_order_list == NULL) { \
return NULL; \
*out = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \
if (*out == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
size_t current_index = 0, current_stack_size = 0; \
bool *added_stack = (bool*)hallocy_calloc(current_binary_search_tree->size + 1, sizeof(bool)); \
if (added_stack == NULL) { \
return NULL; \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
@ -246,7 +246,7 @@ FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_or
current_index += 1; \
} else { \
if (!added_stack[current_index]) { \
in_order_list[current_stack_size] = current_node; \
(*out)[current_stack_size] = current_node; \
current_stack_size += 1; \
added_stack[current_index] = true; \
if (current_node->right != NULL) { \
@ -263,10 +263,10 @@ FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_or
} \
\
if (hallocy_free(added_stack) != HALLOCY_ERROR_NONE) { \
return NULL; \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
return in_order_list; \
return FLEDASTY_ERROR_NONE; \
}
#endif

View file

@ -332,15 +332,16 @@ int main() {
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 10, 20);
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, &in_order_result);
for (int i = 0; i < test_search_tree.size; i += 1) {
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);
fledasty_binary_search_tree_int_int_remove(&test_search_tree, 3);
fledasty_binary_search_tree_int_int_in_order_traversal(&test_search_tree, &in_order_result);
for (int i = 0; i < test_search_tree.size; i += 1) {
printf("In order (After Remove): %d\n", in_order_result[i]->key);
}