diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 7e23655..d3a16cc 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); \ @@ -119,33 +119,43 @@ 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; \ } \ \ -value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key) { \ - if (current_hash_table == NULL) { \ - return 0; \ +FledastyError fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key, value_type *out) { \ + if (current_hash_table == NULL || out == NULL) { \ + 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 new file mode 100644 index 0000000..d6cb787 --- /dev/null +++ b/Include/Fledasty/Trees/BinarySearchTree.h @@ -0,0 +1,350 @@ +/* + * 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); \ + \ +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); \ +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; } + +#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; \ + } \ + \ + 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 *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->key = key; \ + new_node->value = value; \ + \ + 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 { \ + previous_node->right = new_node; \ + } \ + \ + current_binary_search_tree->size += 1; \ + return FLEDASTY_ERROR_NONE; \ +} \ + \ +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 || out == 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; \ + } \ + \ + *out = current_node->value; \ + 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->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; \ + } \ + } \ + \ + if (replacement_node != 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; \ +} \ + \ +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; \ + } \ + \ + *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)) { \ + (*out)[current_stack_size] = current_node; \ + current_stack_size += 1; \ + \ + 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; \ +} \ + \ +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; \ +} \ + \ +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; \ + } \ + \ + 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/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 b449818..5fa65e7 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -19,6 +19,7 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ +#include "Fledasty/Utils/Error.h" #include #include #include @@ -29,10 +30,12 @@ #include #include #include +#include #include 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) @@ -52,6 +55,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, compare_integers, integer_less_than_integer) + int main() { FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL }; for (int i = 0; i < 10; i += 1) { @@ -198,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)) { @@ -313,6 +321,55 @@ 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); + + 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); + + 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("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); + + 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); + } + + hallocy_free(in_order_result); + + 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; } \ No newline at end of file