From b2681dcebd294cb4794dc68c9b3a3dd13d31c165 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 2 Sep 2025 15:05:32 -0500 Subject: [PATCH] feat(search tree): implemented post order traversal --- Include/Fledasty/Trees/BinarySearchTree.h | 35 +++++++++++++++++++++++ Tests/Main.c | 9 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 366d628..51057af 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -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; \ diff --git a/Tests/Main.c b/Tests/Main.c index 41cf25a..5fa65e7 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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);