Fledasty/Include/Fledasty/Trees/BinarySearchTree.h

291 lines
50 KiB
C

/*
* 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); \
\
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) { \
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 != 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; \
} \
\
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_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; \
} \
\
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]) { \
(*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; \
} \
\
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