f10-binary-tree #27
2 changed files with 150 additions and 91 deletions
|
|
@ -49,6 +49,8 @@ typedef struct {
|
||||||
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); \
|
||||||
|
\
|
||||||
|
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree);
|
||||||
|
|
||||||
#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name, key_less_than_function) \
|
#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name, 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) { \
|
||||||
|
|
@ -92,6 +94,9 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \
|
FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \
|
||||||
|
if (new_node == NULL) { \
|
||||||
|
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||||
|
} \
|
||||||
\
|
\
|
||||||
new_node->left = NULL; \
|
new_node->left = NULL; \
|
||||||
new_node->right = NULL; \
|
new_node->right = NULL; \
|
||||||
|
|
@ -125,6 +130,52 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr
|
||||||
\
|
\
|
||||||
current_binary_search_tree->size += 1; \
|
current_binary_search_tree->size += 1; \
|
||||||
return FLEDASTY_ERROR_NONE; \
|
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; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
FledastyBinarySearchTreeNode_##name **in_order_list = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \
|
||||||
|
if (in_order_list == NULL) { \
|
||||||
|
return NULL; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
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; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||||
|
while (current_stack_size < current_binary_search_tree->size) { \
|
||||||
|
if (current_node->left != NULL && !added_stack[current_index + 1]) { \
|
||||||
|
current_node = current_node->left; \
|
||||||
|
current_index += 1; \
|
||||||
|
} else { \
|
||||||
|
if (!added_stack[current_index]) { \
|
||||||
|
in_order_list[current_stack_size] = current_node; \
|
||||||
|
current_stack_size += 1; \
|
||||||
|
added_stack[current_index] = true; \
|
||||||
|
if (current_node->right != NULL) { \
|
||||||
|
current_node = current_node->right; \
|
||||||
|
current_index += 1; \
|
||||||
|
added_stack[current_index] = false; \
|
||||||
|
} \
|
||||||
|
} else { \
|
||||||
|
current_node = current_node->parent; \
|
||||||
|
added_stack[current_index + 1] = false; \
|
||||||
|
current_index -= 1; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (hallocy_free(added_stack) != HALLOCY_ERROR_NONE) { \
|
||||||
|
return NULL; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
return in_order_list; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -329,6 +329,14 @@ 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);
|
||||||
|
if (in_order_result != NULL) {
|
||||||
|
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||||
|
printf("%d\n", in_order_result[i]->key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hallocy_free(in_order_result);
|
||||||
fledasty_binary_search_tree_int_int_free(&test_search_tree);
|
fledasty_binary_search_tree_int_int_free(&test_search_tree);
|
||||||
printf("Done\n");
|
printf("Done\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue