feat(search tree): implemented search tree struct and added free function
This commit is contained in:
parent
fb7bbe00bc
commit
20263fe645
2 changed files with 95 additions and 0 deletions
87
Include/Fledasty/Trees/BinarySearchTree.h
Normal file
87
Include/Fledasty/Trees/BinarySearchTree.h
Normal file
|
|
@ -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 <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
#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
|
||||
|
|
@ -29,6 +29,7 @@
|
|||
#include <Fledasty/Core/HashTable.h>
|
||||
#include <Fledasty/Strings/UTF8String.h>
|
||||
#include <Fledasty/Strings/String.h>
|
||||
#include <Fledasty/Trees/BinarySearchTree.h>
|
||||
#include <Fledasty/Algorithms/Hashing.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue