Compare commits
3 commits
main
...
f11-red-bl
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a265f75ae | |||
| b1136f78d8 | |||
| fc3597fca5 |
3 changed files with 103 additions and 3 deletions
|
|
@ -15,7 +15,7 @@
|
||||||
* File: BinarySearchTree.h
|
* File: BinarySearchTree.h
|
||||||
* Description:
|
* Description:
|
||||||
* This file contains the binary search tree structure and the functions for
|
* This file contains the binary search tree structure and the functions for
|
||||||
* modifying it. It includes functions to Insert, remove, search, check if
|
* modifying it. It includes functions to Insert, remove, get, check if
|
||||||
* contains value, check if is empty, find max and min value, traverse the tree
|
* contains value, check if is empty, find max and min value, traverse the tree
|
||||||
* and clear it.
|
* and clear it.
|
||||||
*
|
*
|
||||||
|
|
@ -42,7 +42,7 @@ typedef struct FledastyBinarySearchTreeNode_##name {
|
||||||
} FledastyBinarySearchTreeNode_##name; \
|
} FledastyBinarySearchTreeNode_##name; \
|
||||||
\
|
\
|
||||||
typedef struct { \
|
typedef struct { \
|
||||||
size_t size, depth; \
|
size_t size; \
|
||||||
FledastyBinarySearchTreeNode_##name *top; \
|
FledastyBinarySearchTreeNode_##name *top; \
|
||||||
} FledastyBinarySearchTree_##name; \
|
} FledastyBinarySearchTree_##name; \
|
||||||
\
|
\
|
||||||
|
|
|
||||||
92
Include/Fledasty/Trees/RedBlackTree.h
Normal file
92
Include/Fledasty/Trees/RedBlackTree.h
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* 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: RedBlackTree.h
|
||||||
|
* Description:
|
||||||
|
* This file contains the red black tree structure and the functions for
|
||||||
|
* modifying it. It includes functions to Insert, remove, get, check if
|
||||||
|
* contains value, check if is empty, find max and min value, traverse the tree
|
||||||
|
* and clear it.
|
||||||
|
*
|
||||||
|
* Author: Mineplay
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#ifndef FLEDASTY_RED_BLACK_TREE
|
||||||
|
#define FLEDASTY_RED_BLACK_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_RED_BLACK_TREE_DEFINE(key_type, value_type, name) \
|
||||||
|
typedef struct FledastyRedBlackTreeNode_##name { \
|
||||||
|
bool is_red; \
|
||||||
|
\
|
||||||
|
key_type key; \
|
||||||
|
value_type value; \
|
||||||
|
\
|
||||||
|
struct FledastyRedBlackTreeNode_##name *parent, *left, *right; \
|
||||||
|
} FledastyRedBlackTreeNode_##name; \
|
||||||
|
\
|
||||||
|
typedef struct { \
|
||||||
|
size_t size; \
|
||||||
|
FledastyRedBlackTreeNode_##name *top; \
|
||||||
|
} FledastyRedBlackTree_##name; \
|
||||||
|
\
|
||||||
|
FledastyError fledasty_red_black_tree_##name##_free(FledastyRedBlackTree_##name *current_red_black_tree); \
|
||||||
|
\
|
||||||
|
static inline bool fledasty_red_black_tree_##name##_is_empty(const FledastyRedBlackTree_##name *current_red_black_tree) { return current_red_black_tree == NULL || current_red_black_tree->size == 0; }
|
||||||
|
|
||||||
|
#define FLEDASTY_RED_BLACK_TREE_IMPLEMENT(key_type, value_type, name, compare_key_function, key_less_than_function) \
|
||||||
|
FledastyError fledasty_red_black_tree_##name##_free(FledastyRedBlackTree_##name *current_red_black_tree) { \
|
||||||
|
if (current_red_black_tree != NULL) { \
|
||||||
|
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
FledastyRedBlackTreeNode_##name *current_node = current_red_black_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
|
||||||
10
Tests/Main.c
10
Tests/Main.c
|
|
@ -31,6 +31,7 @@
|
||||||
#include <Fledasty/Strings/UTF8String.h>
|
#include <Fledasty/Strings/UTF8String.h>
|
||||||
#include <Fledasty/Strings/String.h>
|
#include <Fledasty/Strings/String.h>
|
||||||
#include <Fledasty/Trees/BinarySearchTree.h>
|
#include <Fledasty/Trees/BinarySearchTree.h>
|
||||||
|
#include <Fledasty/Trees/RedBlackTree.h>
|
||||||
#include <Fledasty/Algorithms/Hashing.h>
|
#include <Fledasty/Algorithms/Hashing.h>
|
||||||
|
|
||||||
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
||||||
|
|
@ -58,6 +59,9 @@ FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
|
||||||
FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int)
|
FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int)
|
||||||
FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, compare_integers, integer_less_than_integer)
|
FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, compare_integers, integer_less_than_integer)
|
||||||
|
|
||||||
|
FLEDASTY_RED_BLACK_TREE_DEFINE(int, int, int_int)
|
||||||
|
FLEDASTY_RED_BLACK_TREE_IMPLEMENT(int, int, int_int, compare_integers, integer_less_than_integer)
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
||||||
for (int i = 0; i < 10; i += 1) {
|
for (int i = 0; i < 10; i += 1) {
|
||||||
|
|
@ -322,7 +326,7 @@ int main() {
|
||||||
|
|
||||||
fledasty_string_free(&normal_test_string);
|
fledasty_string_free(&normal_test_string);
|
||||||
|
|
||||||
FledastyBinarySearchTree_int_int test_search_tree = { 0, 0, NULL };
|
FledastyBinarySearchTree_int_int test_search_tree = { 0, NULL };
|
||||||
|
|
||||||
fledasty_binary_search_tree_int_int_insert(&test_search_tree, 5, 10);
|
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, 3, 6);
|
||||||
|
|
@ -378,6 +382,10 @@ int main() {
|
||||||
printf("Biggest key is %d with the value %d\n", max_key, max_value);
|
printf("Biggest key is %d with the value %d\n", max_key, max_value);
|
||||||
|
|
||||||
fledasty_binary_search_tree_int_int_free(&test_search_tree);
|
fledasty_binary_search_tree_int_int_free(&test_search_tree);
|
||||||
|
|
||||||
|
FledastyRedBlackTree_int_int test_red_black_tree = { 0, NULL };
|
||||||
|
|
||||||
|
fledasty_red_black_tree_int_int_free(&test_red_black_tree);
|
||||||
printf("Done\n");
|
printf("Done\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue