feat(search tree): implemented preorder traversal

This commit is contained in:
Mineplay 2025-09-02 14:35:40 -05:00
parent 8c911e8a4c
commit 36d25840eb
2 changed files with 54 additions and 14 deletions

View file

@ -53,6 +53,7 @@ FledastyError fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_
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); \
FledastyError fledasty_binary_search_tree_##name##_preorder_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; }
@ -239,21 +240,19 @@ FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBi
if (current_node->left != NULL && !added_stack[current_index + 1]) { \
current_node = current_node->left; \
current_index += 1; \
} else { \
if (!added_stack[current_index]) { \
(*out)[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; \
} else if (!added_stack[current_index]) { \
(*out)[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; \
} \
} \
\
@ -264,6 +263,40 @@ FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBi
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_binary_search_tree_##name##_preorder_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) { \
(*out)[current_stack_size] = current_node; \
current_stack_size += 1; \
\
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 { \
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; \

View file

@ -340,6 +340,13 @@ int main() {
hallocy_free(in_order_result);
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);
}
hallocy_free(in_order_result);
fledasty_binary_search_tree_int_int_remove(&test_search_tree, 5);
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) {