From 20263fe64572b8cbb8567a0ab93a726117bd287a Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 14 Jul 2025 12:58:31 -0500 Subject: [PATCH 01/16] feat(search tree): implemented search tree struct and added free function --- Include/Fledasty/Trees/BinarySearchTree.h | 87 +++++++++++++++++++++++ Tests/Main.c | 8 +++ 2 files changed, 95 insertions(+) create mode 100644 Include/Fledasty/Trees/BinarySearchTree.h diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h new file mode 100644 index 0000000..112fbba --- /dev/null +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -0,0 +1,87 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: BinarySearchTree.h + * Description: + * This file contains the binary search tree structure and the functions for + * modifying it. It includes functions to Insert, remove, search, check if + * contains value, check if is empty, find max and min value, traverse the tree + * and clear it. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_BINARY_TREE +#define FLEDASTY_BINARY_TREE + +#include +#include +#include +#include +#include + +#include "../Utils/Error.h" + +#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ +typedef struct FledastyBinarySearchTreeNode_##name { \ + key_type key; \ + value_type value; \ + \ + struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ +} FledastyBinarySearchTreeNode_##name; \ + \ +typedef struct { \ + size_t size, depth; \ + FledastyBinarySearchTreeNode_##name *top; \ +} FledastyBinarySearchTree_##name; \ + \ +FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree); + +#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name) \ +FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \ + if (current_binary_search_tree != NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL) { \ + if (current_node->left != NULL) { \ + current_node = current_node->left; \ + } else if (current_node->right != NULL) { \ + current_node = current_node->right; \ + } else if (current_node->parent != NULL) { \ + current_node = current_node->parent; \ + if (current_node->left != NULL) { \ + if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + current_node->left = NULL; \ + } else { \ + if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + current_node->right = NULL; \ + } \ + } else { \ + hallocy_free(current_node); \ + current_node = NULL; \ + } \ + } \ + \ + return FLEDASTY_ERROR_NONE; \ +} + +#endif diff --git a/Tests/Main.c b/Tests/Main.c index b449818..8a34746 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -29,6 +29,7 @@ #include #include #include +#include #include static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; } @@ -52,6 +53,9 @@ FLEDASTY_LINKED_LIST_IMPLEMENT(int, int, compare_integers) FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int) FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers) +FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int) +FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int) + int main() { FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL }; for (int i = 0; i < 10; i += 1) { @@ -313,6 +317,10 @@ int main() { } fledasty_string_free(&normal_test_string); + + FledastyBinarySearchTree_int_int test_search_tree = { 0, 0, NULL }; + + fledasty_binary_search_tree_int_int_free(&test_search_tree); printf("Done\n"); return 0; } \ No newline at end of file From d6f12fe535dbe32f8c28f77472f73aa760fe254d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 16 Jul 2025 08:06:38 -0500 Subject: [PATCH 02/16] feat(search tree): implemented insert function --- Include/Fledasty/Trees/BinarySearchTree.h | 139 ++++++++++++++-------- Tests/Main.c | 11 +- 2 files changed, 101 insertions(+), 49 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 112fbba..a000c47 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -33,55 +33,98 @@ #include "../Utils/Error.h" -#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ -typedef struct FledastyBinarySearchTreeNode_##name { \ - key_type key; \ - value_type value; \ - \ - struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ -} FledastyBinarySearchTreeNode_##name; \ - \ -typedef struct { \ - size_t size, depth; \ - FledastyBinarySearchTreeNode_##name *top; \ -} FledastyBinarySearchTree_##name; \ - \ -FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree); +#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ +typedef struct FledastyBinarySearchTreeNode_##name { \ + key_type key; \ + value_type value; \ + \ + struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ +} FledastyBinarySearchTreeNode_##name; \ + \ +typedef struct { \ + size_t size, depth; \ + FledastyBinarySearchTreeNode_##name *top; \ +} FledastyBinarySearchTree_##name; \ + \ +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); \ -#define FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(key_type, value_type, name) \ -FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \ - if (current_binary_search_tree != NULL) { \ - return FLEDASTY_ERROR_INVALID_POINTER; \ - } \ - \ - FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ - while (current_node != NULL) { \ - if (current_node->left != NULL) { \ - current_node = current_node->left; \ - } else if (current_node->right != NULL) { \ - current_node = current_node->right; \ - } else if (current_node->parent != NULL) { \ - current_node = current_node->parent; \ - if (current_node->left != NULL) { \ - if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \ - return FLEDASTY_ERROR_FAILED_ALLOCATION; \ - } \ - \ - current_node->left = NULL; \ - } else { \ - if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \ - return FLEDASTY_ERROR_FAILED_ALLOCATION; \ - } \ - \ - current_node->right = NULL; \ - } \ - } else { \ - hallocy_free(current_node); \ - current_node = NULL; \ - } \ - } \ - \ - return FLEDASTY_ERROR_NONE; \ +#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) { \ + if (current_binary_search_tree != NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL) { \ + if (current_node->left != NULL) { \ + current_node = current_node->left; \ + } else if (current_node->right != NULL) { \ + current_node = current_node->right; \ + } else if (current_node->parent != NULL) { \ + current_node = current_node->parent; \ + if (current_node->left != NULL) { \ + if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + current_node->left = NULL; \ + } else { \ + if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + current_node->right = NULL; \ + } \ + } else { \ + hallocy_free(current_node); \ + current_node = NULL; \ + } \ + } \ + \ + return FLEDASTY_ERROR_NONE; \ +} \ + \ +FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value) { \ + if (current_binary_search_tree == NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \ + \ + new_node->left = NULL; \ + new_node->right = NULL; \ + new_node->parent = NULL; \ + \ + new_node->key = key; \ + new_node->value = value; \ + \ + if (current_binary_search_tree->top == NULL) { \ + current_binary_search_tree->top = new_node; \ + } else { \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (new_node->parent == NULL) { \ + if (key_less_than_function(key, current_node->key)) { \ + if (current_node->left == NULL) { \ + new_node->parent = current_node; \ + current_node->left = new_node; \ + } else { \ + current_node = current_node->left; \ + } \ + } else { \ + if (current_node->right == NULL) { \ + new_node->parent = current_node; \ + current_node->right = new_node; \ + } else { \ + current_node = current_node->right; \ + } \ + } \ + } \ + } \ + \ + current_binary_search_tree->size += 1; \ + return FLEDASTY_ERROR_NONE; \ } #endif diff --git a/Tests/Main.c b/Tests/Main.c index 8a34746..810c645 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -34,6 +34,7 @@ static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; } static inline size_t integer_hash_function(const int key) { return key; } +static inline int integer_less_than_integer(const int first_key, const int second_key) { return first_key < second_key; } FLEDASTY_STACK_DEFINE(int, int) FLEDASTY_STACK_IMPLEMENT(int, int) @@ -54,7 +55,7 @@ FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int) FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers) FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int) -FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int) +FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, integer_less_than_integer) int main() { FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL }; @@ -319,6 +320,14 @@ int main() { fledasty_string_free(&normal_test_string); FledastyBinarySearchTree_int_int test_search_tree = { 0, 0, NULL }; + + fledasty_binary_search_tree_int_int_insert(&test_search_tree, 5, 10); + fledasty_binary_search_tree_int_int_insert(&test_search_tree, 3, 6); + fledasty_binary_search_tree_int_int_insert(&test_search_tree, 4, 8); + fledasty_binary_search_tree_int_int_insert(&test_search_tree, 1, 2); + fledasty_binary_search_tree_int_int_insert(&test_search_tree, 8, 16); + 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_free(&test_search_tree); printf("Done\n"); From ef53a1371ae52b5a1ea10f8dc1d8062666031449 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 20 Aug 2025 12:04:15 -0500 Subject: [PATCH 03/16] feat(search tree): implemented in order traversal for search tree --- Include/Fledasty/Trees/BinarySearchTree.h | 233 +++++++++++++--------- Tests/Main.c | 8 + 2 files changed, 150 insertions(+), 91 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index a000c47..320fe24 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -33,98 +33,149 @@ #include "../Utils/Error.h" -#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ -typedef struct FledastyBinarySearchTreeNode_##name { \ - key_type key; \ - value_type value; \ - \ - struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ -} FledastyBinarySearchTreeNode_##name; \ - \ -typedef struct { \ - size_t size, depth; \ - FledastyBinarySearchTreeNode_##name *top; \ -} FledastyBinarySearchTree_##name; \ - \ -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); \ +#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ +typedef struct FledastyBinarySearchTreeNode_##name { \ + key_type key; \ + value_type value; \ + \ + struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ +} FledastyBinarySearchTreeNode_##name; \ + \ +typedef struct { \ + size_t size, depth; \ + FledastyBinarySearchTreeNode_##name *top; \ +} FledastyBinarySearchTree_##name; \ + \ +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); \ + \ +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) \ -FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \ - if (current_binary_search_tree != NULL) { \ - return FLEDASTY_ERROR_INVALID_POINTER; \ - } \ - \ - FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ - while (current_node != NULL) { \ - if (current_node->left != NULL) { \ - current_node = current_node->left; \ - } else if (current_node->right != NULL) { \ - current_node = current_node->right; \ - } else if (current_node->parent != NULL) { \ - current_node = current_node->parent; \ - if (current_node->left != NULL) { \ - if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \ - return FLEDASTY_ERROR_FAILED_ALLOCATION; \ - } \ - \ - current_node->left = NULL; \ - } else { \ - if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \ - return FLEDASTY_ERROR_FAILED_ALLOCATION; \ - } \ - \ - current_node->right = NULL; \ - } \ - } else { \ - hallocy_free(current_node); \ - current_node = NULL; \ - } \ - } \ - \ - return FLEDASTY_ERROR_NONE; \ -} \ - \ -FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value) { \ - if (current_binary_search_tree == NULL) { \ - return FLEDASTY_ERROR_INVALID_POINTER; \ - } \ - \ - FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \ - \ - new_node->left = NULL; \ - new_node->right = NULL; \ - new_node->parent = NULL; \ - \ - new_node->key = key; \ - new_node->value = value; \ - \ - if (current_binary_search_tree->top == NULL) { \ - current_binary_search_tree->top = new_node; \ - } else { \ - FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ - while (new_node->parent == NULL) { \ - if (key_less_than_function(key, current_node->key)) { \ - if (current_node->left == NULL) { \ - new_node->parent = current_node; \ - current_node->left = new_node; \ - } else { \ - current_node = current_node->left; \ - } \ - } else { \ - if (current_node->right == NULL) { \ - new_node->parent = current_node; \ - current_node->right = new_node; \ - } else { \ - current_node = current_node->right; \ - } \ - } \ - } \ - } \ - \ - current_binary_search_tree->size += 1; \ - return FLEDASTY_ERROR_NONE; \ +#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) { \ + if (current_binary_search_tree != NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL) { \ + if (current_node->left != NULL) { \ + current_node = current_node->left; \ + } else if (current_node->right != NULL) { \ + current_node = current_node->right; \ + } else if (current_node->parent != NULL) { \ + current_node = current_node->parent; \ + if (current_node->left != NULL) { \ + if (hallocy_free(current_node->left) != HALLOCY_ERROR_NONE) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + current_node->left = NULL; \ + } else { \ + if (hallocy_free(current_node->right) != HALLOCY_ERROR_NONE) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + current_node->right = NULL; \ + } \ + } else { \ + hallocy_free(current_node); \ + current_node = NULL; \ + } \ + } \ + \ + return FLEDASTY_ERROR_NONE; \ +} \ + \ +FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value) { \ + if (current_binary_search_tree == NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + 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->right = NULL; \ + new_node->parent = NULL; \ + \ + new_node->key = key; \ + new_node->value = value; \ + \ + if (current_binary_search_tree->top == NULL) { \ + current_binary_search_tree->top = new_node; \ + } else { \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (new_node->parent == NULL) { \ + if (key_less_than_function(key, current_node->key)) { \ + if (current_node->left == NULL) { \ + new_node->parent = current_node; \ + current_node->left = new_node; \ + } else { \ + current_node = current_node->left; \ + } \ + } else { \ + if (current_node->right == NULL) { \ + new_node->parent = current_node; \ + current_node->right = new_node; \ + } else { \ + current_node = current_node->right; \ + } \ + } \ + } \ + } \ + \ + current_binary_search_tree->size += 1; \ + 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 diff --git a/Tests/Main.c b/Tests/Main.c index 810c645..da9e419 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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, 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); printf("Done\n"); return 0; From abcdb0d127e5410e59d299d163eaa9f39831d397 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 27 Aug 2025 12:21:25 -0500 Subject: [PATCH 04/16] feat(search tree): implemented delete function --- Include/Fledasty/Trees/BinarySearchTree.h | 64 ++++++++++++++++++++++- Tests/Main.c | 12 +++-- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 320fe24..bc20566 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -49,10 +49,11 @@ typedef struct { 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##_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); -#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, compare_key_function, key_less_than_function) \ FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree) { \ if (current_binary_search_tree != NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ @@ -132,6 +133,67 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr return FLEDASTY_ERROR_NONE; \ } \ \ +FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \ + if (current_binary_search_tree == NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL && !compare_key_function(current_node->key, key)) { \ + if (key_less_than_function(key, current_node->key)) { \ + current_node = current_node->left; \ + } else { \ + current_node = current_node->right; \ + } \ + } \ + \ + if (current_node == NULL) { \ + return FLEDASTY_ERROR_KEY_NOT_FOUND; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *replacement_node = current_node->right; \ + if (current_node->left != NULL) { \ + replacement_node = current_node->left; \ + while (replacement_node->right != NULL) { \ + replacement_node = replacement_node->right; \ + } \ + } \ + \ + if (replacement_node != NULL) { \ + replacement_node->left = current_node->left; \ + if (current_node->left != NULL) { \ + current_node->left->parent = replacement_node; \ + } \ + \ + replacement_node->right = current_node->right; \ + if (current_node->right != NULL) { \ + current_node->right->parent = replacement_node; \ + } \ + \ + if (replacement_node->parent->right == replacement_node) { \ + replacement_node->parent->right = NULL; \ + } else { \ + replacement_node->parent->left = NULL; \ + } \ + replacement_node->parent = current_node->parent; \ + } \ + \ + if (current_node->parent != NULL) { \ + if(current_node->parent->left == current_node) { \ + current_node->parent->left = replacement_node; \ + } else { \ + current_node->parent->right = replacement_node; \ + } \ + } else { \ + current_binary_search_tree->top = replacement_node; \ + } \ + \ + hallocy_free(current_node); \ + current_binary_search_tree->size -= 1; \ + \ + 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; \ diff --git a/Tests/Main.c b/Tests/Main.c index da9e419..d48edc3 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -55,7 +55,7 @@ FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int) FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers) FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int) -FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, integer_less_than_integer) +FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, compare_integers, integer_less_than_integer) int main() { FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL }; @@ -330,10 +330,16 @@ int main() { 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); + printf("In order (After Insert): %d\n", in_order_result[i]->key); } + + hallocy_free(in_order_result); + + printf("Error code: %d\n", 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); + for (int i = 0; i < test_search_tree.size; i += 1) { + printf("In order (After Remove): %d\n", in_order_result[i]->key); } hallocy_free(in_order_result); From 91e07ff6d04bacc4451c5c985266f333690f3e5b Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 28 Aug 2025 05:17:35 -0500 Subject: [PATCH 05/16] feat(search tree): implemented get function for search tree --- Include/Fledasty/Trees/BinarySearchTree.h | 23 +++++++++++++++++++++++ Include/Fledasty/Utils/Error.h | 1 + Tests/Main.c | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index bc20566..c298be7 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -49,6 +49,7 @@ typedef struct { 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); \ +FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(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); @@ -111,6 +112,11 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr } else { \ FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ while (new_node->parent == NULL) { \ + if (compare_key_function(key, current_node->key)) { \ + current_node->value = value; \ + return FLEDASTY_ERROR_VALUE_REPLACED; \ + } \ + \ if (key_less_than_function(key, current_node->key)) { \ if (current_node->left == NULL) { \ new_node->parent = current_node; \ @@ -133,6 +139,23 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr return FLEDASTY_ERROR_NONE; \ } \ \ +FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \ + if (current_binary_search_tree == NULL) { \ + return NULL; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL && !compare_key_function(current_node->key, key)) { \ + if (key_less_than_function(key, current_node->key)) { \ + current_node = current_node->left; \ + } else { \ + current_node = current_node->right; \ + } \ + } \ + \ + return current_node; \ +} \ + \ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \ if (current_binary_search_tree == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h index ce48894..f535aec 100644 --- a/Include/Fledasty/Utils/Error.h +++ b/Include/Fledasty/Utils/Error.h @@ -31,6 +31,7 @@ typedef enum { FLEDASTY_ERROR_VALUE_NOT_FOUND = 4, FLEDASTY_ERROR_KEY_NOT_FOUND = 5, FLEDASTY_ERROR_INVALID_VALUE = 6, + FLEDASTY_ERROR_VALUE_REPLACED = 7 } FledastyError; #endif diff --git a/Tests/Main.c b/Tests/Main.c index d48edc3..8e0fb09 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -343,6 +343,10 @@ int main() { } hallocy_free(in_order_result); + + FledastyBinarySearchTreeNode_int_int *node = fledasty_binary_search_tree_int_int_get(&test_search_tree, 7); + printf("Search tree get result: %d\n", node->value); + fledasty_binary_search_tree_int_int_free(&test_search_tree); printf("Done\n"); return 0; From ecbffdc1125dd4ed4701c124f6af56e4fb621b5c Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 28 Aug 2025 05:39:20 -0500 Subject: [PATCH 06/16] fix(hash table): fixed duplicate inserts for hash table --- Include/Fledasty/Core/HashTable.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 7e23655..29f9dbf 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -119,12 +119,21 @@ FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *curr hallocy_free(previous_table); \ } \ \ - const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \ - \ FledastyHashTablePair_##name pair = { .key = key, .value = value }; \ - fledasty_dynamic_array_fledasty_internal_pair_append(¤t_hash_table->Table[index], pair); \ + const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \ + if (fledasty_dynamic_array_fledasty_internal_pair_has_value(¤t_hash_table->Table[index], pair)) { \ + size_t key_index = 0; \ + while (!compare_key_function(current_hash_table->Table[index].buffer[key_index].key, key)) { \ + key_index += 1; \ + } \ \ + current_hash_table->Table[index].buffer[key_index].value = value; \ + return FLEDASTY_ERROR_VALUE_REPLACED; \ + } \ + \ + fledasty_dynamic_array_fledasty_internal_pair_append(¤t_hash_table->Table[index], pair); \ current_hash_table->size += 1; \ + \ return FLEDASTY_ERROR_NONE; \ } \ \ From 4e57a165fd20a952e68a8055e25a889c1a0baedb Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 29 Aug 2025 11:58:28 -0500 Subject: [PATCH 07/16] refactor(error handling): changed get functions for tree and hash table to improve error handling --- Include/Fledasty/Core/HashTable.h | 13 +++++++------ Include/Fledasty/Trees/BinarySearchTree.h | 13 +++++++++---- Tests/Main.c | 11 +++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 29f9dbf..1c3dfda 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -55,7 +55,7 @@ typedef struct { FledastyError fledasty_hash_table_##name##_free(FledastyHashTable_##name *current_hash_table); \ \ FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *current_hash_table, key_type key, value_type value); \ -value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key); \ +FledastyError fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key, value_type *out); \ FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key); \ \ FledastyError fledasty_hash_table_##name##_clear(FledastyHashTable_##name *current_hash_table); \ @@ -137,24 +137,25 @@ FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *curr return FLEDASTY_ERROR_NONE; \ } \ \ -value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key) { \ +FledastyError fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key, value_type *out) { \ if (current_hash_table == NULL) { \ - return 0; \ + return FLEDASTY_ERROR_INVALID_POINTER; \ } \ \ const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \ if (current_hash_table->Table[index].buffer == NULL) { \ - return 0; \ + return FLEDASTY_ERROR_KEY_NOT_FOUND; \ } \ \ for (size_t list_index = 0; list_index < current_hash_table->Table[index].size; list_index += 1) { \ const FledastyHashTablePair_##name value = current_hash_table->Table[index].buffer[list_index]; \ if (compare_key_function(value.key, key)) { \ - return value.value; \ + *out = value.value; \ + return FLEDASTY_ERROR_NONE; \ } \ } \ \ - return 0; \ + return FLEDASTY_ERROR_KEY_NOT_FOUND; \ } \ \ FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key) { \ diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index c298be7..2bbf315 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -49,7 +49,7 @@ typedef struct { 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); \ -FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \ +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); \ \ FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree); @@ -139,9 +139,9 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr return FLEDASTY_ERROR_NONE; \ } \ \ -FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \ +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) { \ - return NULL; \ + return FLEDASTY_ERROR_INVALID_POINTER; \ } \ \ FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ @@ -153,7 +153,12 @@ FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(Fl } \ } \ \ - return current_node; \ + if (current_node == NULL) { \ + return FLEDASTY_ERROR_KEY_NOT_FOUND; \ + } \ + \ + *out = current_node->value; \ + return FLEDASTY_ERROR_NONE; \ } \ \ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \ diff --git a/Tests/Main.c b/Tests/Main.c index 8e0fb09..0a986e1 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -19,6 +19,7 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ +#include "Fledasty/Utils/Error.h" #include #include #include @@ -203,12 +204,14 @@ int main() { fledasty_hash_table_int_int_insert(&test_hash_table, 2, 5); fledasty_hash_table_int_int_remove(&test_hash_table, 2); - if (fledasty_hash_table_int_int_get(&test_hash_table, 2) != 0) { + int get_out = 0; + if (fledasty_hash_table_int_int_get(&test_hash_table, 2, &get_out) != FLEDASTY_ERROR_KEY_NOT_FOUND) { printf("Value not removed from hash table\n"); } for (int i = 0; i < 10; i += 1) { - printf("Hash table get: %d\n", fledasty_hash_table_int_int_get(&test_hash_table, i * i)); + fledasty_hash_table_int_int_get(&test_hash_table, i * i, &get_out); + printf("Hash table get: %d\n", get_out); } if (fledasty_hash_table_int_int_has_key(&test_hash_table, 4)) { @@ -344,8 +347,8 @@ int main() { hallocy_free(in_order_result); - FledastyBinarySearchTreeNode_int_int *node = fledasty_binary_search_tree_int_int_get(&test_search_tree, 7); - printf("Search tree get result: %d\n", node->value); + fledasty_binary_search_tree_int_int_get(&test_search_tree, 7, &get_out); + printf("Search tree get result: %d\n", get_out); fledasty_binary_search_tree_int_int_free(&test_search_tree); printf("Done\n"); From c8c2d5623dcf62d7a9ce613843da0473dda7f7bc Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 29 Aug 2025 13:26:07 -0500 Subject: [PATCH 08/16] fix(search tree): fixed memory leak issue --- Include/Fledasty/Trees/BinarySearchTree.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 2bbf315..369a691 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -114,6 +114,7 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr while (new_node->parent == NULL) { \ if (compare_key_function(key, current_node->key)) { \ current_node->value = value; \ + hallocy_free(new_node); \ return FLEDASTY_ERROR_VALUE_REPLACED; \ } \ \ From 86acb0560ad0e20e936164a641377630537252b9 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 30 Aug 2025 13:35:09 -0500 Subject: [PATCH 09/16] refactor(search tree): made in order traversal output consistent with rest of the library --- Include/Fledasty/Core/HashTable.h | 2 +- Include/Fledasty/Trees/BinarySearchTree.h | 62 +++++++++++------------ Tests/Main.c | 7 +-- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 1c3dfda..d3a16cc 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -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) { \ - if (current_hash_table == NULL) { \ + if (current_hash_table == NULL || out == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ \ diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 369a691..057a5ca 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -33,26 +33,26 @@ #include "../Utils/Error.h" -#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ -typedef struct FledastyBinarySearchTreeNode_##name { \ - key_type key; \ - value_type value; \ - \ - struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ -} FledastyBinarySearchTreeNode_##name; \ - \ -typedef struct { \ - size_t size, depth; \ - FledastyBinarySearchTreeNode_##name *top; \ -} FledastyBinarySearchTree_##name; \ - \ -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##_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); \ - \ -FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree); +#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ +typedef struct FledastyBinarySearchTreeNode_##name { \ + key_type key; \ + value_type value; \ + \ + struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ +} FledastyBinarySearchTreeNode_##name; \ + \ +typedef struct { \ + size_t size, depth; \ + FledastyBinarySearchTreeNode_##name *top; \ +} FledastyBinarySearchTree_##name; \ + \ +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##_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##_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) \ 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) { \ - if (current_binary_search_tree == NULL) { \ + if (current_binary_search_tree == NULL || out == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ \ @@ -223,20 +223,20 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr 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; \ +FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out) { \ + if (current_binary_search_tree == NULL || out == NULL) { \ + return FLEDASTY_ERROR_INVALID_POINTER; \ } \ \ - FledastyBinarySearchTreeNode_##name **in_order_list = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \ - if (in_order_list == NULL) { \ - return NULL; \ + *out = (FledastyBinarySearchTreeNode_##name**)hallocy_malloc(current_binary_search_tree->size * sizeof(FledastyBinarySearchTreeNode_##name*)); \ + if (*out == NULL) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ } \ \ 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; \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ } \ \ 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; \ } else { \ if (!added_stack[current_index]) { \ - in_order_list[current_stack_size] = current_node; \ + (*out)[current_stack_size] = current_node; \ current_stack_size += 1; \ added_stack[current_index] = true; \ 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) { \ - return NULL; \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ } \ \ - return in_order_list; \ + return FLEDASTY_ERROR_NONE; \ } #endif diff --git a/Tests/Main.c b/Tests/Main.c index 0a986e1..afe33b6 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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, 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) { printf("In order (After Insert): %d\n", in_order_result[i]->key); } hallocy_free(in_order_result); - printf("Error code: %d\n", 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_remove(&test_search_tree, 3); + 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) { printf("In order (After Remove): %d\n", in_order_result[i]->key); } From 1d8550d653cec110403f7cfe7d18723d188f3dd0 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 31 Aug 2025 08:26:14 -0500 Subject: [PATCH 10/16] perf(search tree): improved insertion and replacement performance --- Include/Fledasty/Trees/BinarySearchTree.h | 53 ++++++++++------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 057a5ca..35fa82e 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -95,45 +95,40 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr return FLEDASTY_ERROR_INVALID_POINTER; \ } \ \ - FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \ - if (new_node == NULL) { \ - return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + FledastyBinarySearchTreeNode_##name *previous_node = NULL; \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL) { \ + if (compare_key_function(key, current_node->key)) { \ + current_node->value = value; \ + return FLEDASTY_ERROR_VALUE_REPLACED; \ + } \ + \ + previous_node = current_node; \ + if (key_less_than_function(key, current_node->key)) { \ + current_node = previous_node->left; \ + } else { \ + current_node = previous_node->right; \ + } \ } \ \ + FledastyBinarySearchTreeNode_##name *new_node = (FledastyBinarySearchTreeNode_##name*)hallocy_malloc(sizeof(FledastyBinarySearchTreeNode_##name)); \ + if (new_node == NULL) { \ + return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + } \ + \ + new_node->parent = previous_node; \ new_node->left = NULL; \ new_node->right = NULL; \ - new_node->parent = NULL; \ \ new_node->key = key; \ new_node->value = value; \ \ - if (current_binary_search_tree->top == NULL) { \ + if (previous_node == NULL) { \ current_binary_search_tree->top = new_node; \ + } else if (key_less_than_function(key, previous_node->key)) { \ + previous_node->left = new_node; \ } else { \ - FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ - while (new_node->parent == NULL) { \ - if (compare_key_function(key, current_node->key)) { \ - current_node->value = value; \ - hallocy_free(new_node); \ - return FLEDASTY_ERROR_VALUE_REPLACED; \ - } \ - \ - if (key_less_than_function(key, current_node->key)) { \ - if (current_node->left == NULL) { \ - new_node->parent = current_node; \ - current_node->left = new_node; \ - } else { \ - current_node = current_node->left; \ - } \ - } else { \ - if (current_node->right == NULL) { \ - new_node->parent = current_node; \ - current_node->right = new_node; \ - } else { \ - current_node = current_node->right; \ - } \ - } \ - } \ + previous_node->right = new_node; \ } \ \ current_binary_search_tree->size += 1; \ From dab5f6a9b2aca8eae8e8f5cca5ff924b4fbad1d6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 31 Aug 2025 10:46:08 -0500 Subject: [PATCH 11/16] feat(search tree): added utility functions to search tree --- Include/Fledasty/Trees/BinarySearchTree.h | 64 ++++++++++++++++------- Tests/Main.c | 4 ++ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 35fa82e..6feb826 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -33,26 +33,29 @@ #include "../Utils/Error.h" -#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ -typedef struct FledastyBinarySearchTreeNode_##name { \ - key_type key; \ - value_type value; \ - \ - struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ -} FledastyBinarySearchTreeNode_##name; \ - \ -typedef struct { \ - size_t size, depth; \ - FledastyBinarySearchTreeNode_##name *top; \ -} FledastyBinarySearchTree_##name; \ - \ -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##_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##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree, FledastyBinarySearchTreeNode_##name ***out); +#define FLEDASTY_BINARY_SEARCH_TREE_DEFINE(key_type, value_type, name) \ +typedef struct FledastyBinarySearchTreeNode_##name { \ + key_type key; \ + value_type value; \ + \ + struct FledastyBinarySearchTreeNode_##name *parent, *left, *right; \ +} FledastyBinarySearchTreeNode_##name; \ + \ +typedef struct { \ + size_t size, depth; \ + FledastyBinarySearchTreeNode_##name *top; \ +} FledastyBinarySearchTree_##name; \ + \ +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##_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##_in_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; } #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) { \ @@ -262,6 +265,27 @@ FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBi } \ \ 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; \ + } \ + \ + FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \ + while (current_node != NULL) { \ + if (compare_key_function(current_node->key, key)) { \ + return true; \ + } \ + \ + if (key_less_than_function(key, current_node->key)) { \ + current_node = current_node->left; \ + } else { \ + current_node = current_node->right; \ + } \ + } \ + \ + return false; \ } #endif diff --git a/Tests/Main.c b/Tests/Main.c index afe33b6..a1f9cd1 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -351,6 +351,10 @@ int main() { fledasty_binary_search_tree_int_int_get(&test_search_tree, 7, &get_out); printf("Search tree get result: %d\n", get_out); + if (fledasty_binary_search_tree_int_int_has_key(&test_search_tree, 10)) { + printf("Search tree has key: 10\n"); + } + fledasty_binary_search_tree_int_int_free(&test_search_tree); printf("Done\n"); return 0; From d6e1a5309d16524f7dfcba0dcd6ea6fe77b3f09d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 31 Aug 2025 13:41:51 -0500 Subject: [PATCH 12/16] fix(search tree): fixed removal of nodes with only right sided nodes --- Include/Fledasty/Trees/BinarySearchTree.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 6feb826..c7520de 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -184,9 +184,7 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr while (replacement_node->right != NULL) { \ replacement_node = replacement_node->right; \ } \ - } \ \ - if (replacement_node != NULL) { \ replacement_node->left = current_node->left; \ if (current_node->left != NULL) { \ current_node->left->parent = replacement_node; \ @@ -196,7 +194,9 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr if (current_node->right != NULL) { \ current_node->right->parent = replacement_node; \ } \ + } \ \ + if (replacement_node != NULL) { \ if (replacement_node->parent->right == replacement_node) { \ replacement_node->parent->right = NULL; \ } else { \ From 8c911e8a4cf8e5cd5f1f7868292e4f2d8e2f1c73 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 1 Sep 2025 04:41:06 -0500 Subject: [PATCH 13/16] perf(search tree): improved remove performance by removing usseless if statements --- Include/Fledasty/Trees/BinarySearchTree.h | 13 +++++-------- Tests/Main.c | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index c7520de..1cf5410 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -185,11 +185,13 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr replacement_node = replacement_node->right; \ } \ \ - replacement_node->left = current_node->left; \ - if (current_node->left != NULL) { \ - current_node->left->parent = replacement_node; \ + if (replacement_node->parent->right == replacement_node) { \ + replacement_node->parent->right = replacement_node->left; \ } \ \ + replacement_node->left = current_node->left; \ + current_node->left->parent = replacement_node; \ + \ replacement_node->right = current_node->right; \ if (current_node->right != NULL) { \ current_node->right->parent = replacement_node; \ @@ -197,11 +199,6 @@ FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTr } \ \ if (replacement_node != NULL) { \ - if (replacement_node->parent->right == replacement_node) { \ - replacement_node->parent->right = NULL; \ - } else { \ - replacement_node->parent->left = NULL; \ - } \ replacement_node->parent = current_node->parent; \ } \ \ diff --git a/Tests/Main.c b/Tests/Main.c index a1f9cd1..71edb9c 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -340,7 +340,7 @@ int main() { hallocy_free(in_order_result); - fledasty_binary_search_tree_int_int_remove(&test_search_tree, 3); + 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) { printf("In order (After Remove): %d\n", in_order_result[i]->key); From 36d25840eba0003bf455b2773423388a0b2ef089 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 2 Sep 2025 14:35:40 -0500 Subject: [PATCH 14/16] feat(search tree): implemented preorder traversal --- Include/Fledasty/Trees/BinarySearchTree.h | 61 +++++++++++++++++------ Tests/Main.c | 7 +++ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 1cf5410..366d628 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -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; \ diff --git a/Tests/Main.c b/Tests/Main.c index 71edb9c..41cf25a 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -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) { From b2681dcebd294cb4794dc68c9b3a3dd13d31c165 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 2 Sep 2025 15:05:32 -0500 Subject: [PATCH 15/16] 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); From d93788d6e7b9a4b2874dddd449d85a07aaa7a81c Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 3 Sep 2025 13:18:32 -0500 Subject: [PATCH 16/16] perf(search tree): improved in order traversal performance --- Include/Fledasty/Trees/BinarySearchTree.h | 42 ++++++++++------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/Include/Fledasty/Trees/BinarySearchTree.h b/Include/Fledasty/Trees/BinarySearchTree.h index 51057af..d6cb787 100644 --- a/Include/Fledasty/Trees/BinarySearchTree.h +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -230,35 +230,29 @@ FledastyError fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBi return FLEDASTY_ERROR_FAILED_ALLOCATION; \ } \ \ - 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 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->left != NULL && !added_stack[current_index + 1]) { \ - current_node = current_node->left; \ - current_index += 1; \ - } else if (!added_stack[current_index]) { \ + 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)) { \ (*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; \ - } \ - } \ \ - if (hallocy_free(added_stack) != HALLOCY_ERROR_NONE) { \ - return FLEDASTY_ERROR_FAILED_ALLOCATION; \ + previous_node = current_node; \ + current_node = current_node->right; \ + } else { \ + if (current_node->right != previous_node) { \ + (*out)[current_stack_size] = current_node; \ + current_stack_size += 1; \ + } \ + previous_node = current_node; \ + current_node = current_node->parent; \ + } \ } \ \ return FLEDASTY_ERROR_NONE; \