feat(search tree): implemented post order traversal
This commit is contained in:
parent
36d25840eb
commit
b2681dcebd
2 changed files with 43 additions and 1 deletions
|
|
@ -54,6 +54,7 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr
|
|||
\
|
||||
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); \
|
||||
\
|
||||
bool fledasty_binary_search_tree_##name##_has_key(const FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \
|
||||
static inline bool fledasty_binary_search_tree_##name##_is_empty(const FledastyBinarySearchTree_##name *current_binary_search_tree) { return current_binary_search_tree == NULL || current_binary_search_tree->size == 0; }
|
||||
|
|
@ -297,6 +298,40 @@ FledastyError fledasty_binary_search_tree_##name##_preorder_traversal(FledastyBi
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_post_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out) { \
|
||||
if (current_binary_search_tree == NULL || out == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
*out = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \
|
||||
if (*out == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
size_t current_stack_size = 0; \
|
||||
FledastyBinarySearchTreeNode_##name *previous_node = NULL; \
|
||||
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||
while (current_stack_size < current_binary_search_tree->size) { \
|
||||
if (current_node->parent == previous_node) { \
|
||||
previous_node = current_node; \
|
||||
if (current_node->left != NULL) { \
|
||||
current_node = current_node->left; \
|
||||
} \
|
||||
} else if (current_node->right != NULL && (current_node->left == NULL || current_node->left == previous_node)) { \
|
||||
previous_node = current_node; \
|
||||
current_node = current_node->right; \
|
||||
} else { \
|
||||
(*out)[current_stack_size] = current_node; \
|
||||
current_stack_size += 1; \
|
||||
\
|
||||
previous_node = current_node; \
|
||||
current_node = current_node->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
bool fledasty_binary_search_tree_##name##_has_key(const FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \
|
||||
if (current_binary_search_tree == NULL) { \
|
||||
return false; \
|
||||
|
|
|
|||
|
|
@ -342,7 +342,14 @@ int main() {
|
|||
|
||||
fledasty_binary_search_tree_int_int_preorder_traversal(&test_search_tree, &in_order_result);
|
||||
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||
printf("Preorder (After Insert): %d\n", in_order_result[i]->key);
|
||||
printf("Pre-order (After Insert): %d\n", in_order_result[i]->key);
|
||||
}
|
||||
|
||||
hallocy_free(in_order_result);
|
||||
|
||||
fledasty_binary_search_tree_int_int_post_order_traversal(&test_search_tree, &in_order_result);
|
||||
for (int i = 0; i < test_search_tree.size; i += 1) {
|
||||
printf("Post-order (After Insert): %d\n", in_order_result[i]->key);
|
||||
}
|
||||
|
||||
hallocy_free(in_order_result);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue