refactor(search tree): made in order traversal output consistent with rest of the library
This commit is contained in:
parent
c8c2d5623d
commit
86acb0560a
3 changed files with 36 additions and 35 deletions
|
|
@ -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) { \
|
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; \
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
|
|
||||||
|
|
@ -33,26 +33,26 @@
|
||||||
|
|
||||||
#include "../Utils/Error.h"
|
#include "../Utils/Error.h"
|
||||||
|
|
||||||
#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \
|
#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \
|
||||||
typedef struct FledastyBinarySearchTreeNode_##name { \
|
typedef struct FledastyBinarySearchTreeNode_##name { \
|
||||||
key_type key; \
|
key_type key; \
|
||||||
value_type value; \
|
value_type value; \
|
||||||
\
|
\
|
||||||
struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \
|
struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \
|
||||||
} FledastyBinarySearchTreeNode_##name; \
|
} FledastyBinarySearchTreeNode_##name; \
|
||||||
\
|
\
|
||||||
typedef struct { \
|
typedef struct { \
|
||||||
size_t size, depth; \
|
size_t size, depth; \
|
||||||
FledastyBinarySearchTreeNode_##name *top; \
|
FledastyBinarySearchTreeNode_##name *top; \
|
||||||
} FledastyBinarySearchTree_##name; \
|
} FledastyBinarySearchTree_##name; \
|
||||||
\
|
\
|
||||||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree); \
|
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##_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##_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##_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);
|
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) \
|
#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) { \
|
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) { \
|
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; \
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
|
@ -223,20 +223,20 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr
|
||||||
return FLEDASTY_ERROR_NONE; \
|
return FLEDASTY_ERROR_NONE; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree) { \
|
FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out) { \
|
||||||
if (current_binary_search_tree == NULL) { \
|
if (current_binary_search_tree == NULL || out == NULL) { \
|
||||||
return NULL; \
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
FledastyBinarySearchTreeNode_##name **in_order_list = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \
|
*out = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \
|
||||||
if (in_order_list == NULL) { \
|
if (*out == NULL) { \
|
||||||
return NULL; \
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
size_t current_index = 0, current_stack_size = 0; \
|
size_t current_index = 0, current_stack_size = 0; \
|
||||||
bool *added_stack = (bool*)hallocy_calloc(current_binary_search_tree->size + 1, sizeof(bool)); \
|
bool *added_stack = (bool*)hallocy_calloc(current_binary_search_tree->size + 1, sizeof(bool)); \
|
||||||
if (added_stack == NULL) { \
|
if (added_stack == NULL) { \
|
||||||
return NULL; \
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
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; \
|
current_index += 1; \
|
||||||
} else { \
|
} else { \
|
||||||
if (!added_stack[current_index]) { \
|
if (!added_stack[current_index]) { \
|
||||||
in_order_list[current_stack_size] = current_node; \
|
(*out)[current_stack_size] = current_node; \
|
||||||
current_stack_size += 1; \
|
current_stack_size += 1; \
|
||||||
added_stack[current_index] = true; \
|
added_stack[current_index] = true; \
|
||||||
if (current_node->right != NULL) { \
|
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) { \
|
if (hallocy_free(added_stack) != HALLOCY_ERROR_NONE) { \
|
||||||
return NULL; \
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
return in_order_list; \
|
return FLEDASTY_ERROR_NONE; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -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, 10, 20);
|
||||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 7, 14);
|
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) {
|
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||||
printf("In order (After Insert): %d\n", in_order_result[i]->key);
|
printf("In order (After Insert): %d\n", in_order_result[i]->key);
|
||||||
}
|
}
|
||||||
|
|
||||||
hallocy_free(in_order_result);
|
hallocy_free(in_order_result);
|
||||||
|
|
||||||
printf("Error code: %d\n", fledasty_binary_search_tree_int_int_remove(&test_search_tree, 3));
|
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_in_order_traversal(&test_search_tree, &in_order_result);
|
||||||
for (int i = 0; i < test_search_tree.size; i += 1) {
|
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||||
printf("In order (After Remove): %d\n", in_order_result[i]->key);
|
printf("In order (After Remove): %d\n", in_order_result[i]->key);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue