Bulk of the work on Red-Black trees has been done, needs to be finished and tested still.
This commit is contained in:
parent
cf57df8f27
commit
aaa1b8bb5d
|
@ -23,13 +23,13 @@ enum rb_color_t { BLACK, RED };
|
|||
typedef enum rb_color_t rb_color_t;
|
||||
|
||||
/* This struct defines a red-black tree node. */
|
||||
typedef struct rb_tree_node {
|
||||
typedef struct rb_tree_node_t {
|
||||
/* Parent node. */
|
||||
struct rb_tree_node *parent;
|
||||
struct rb_tree_node_t *parent;
|
||||
/* Right node. */
|
||||
struct rb_tree_node *right;
|
||||
struct rb_tree_node_t *right;
|
||||
/* Left node. */
|
||||
struct rb_tree_node *left;
|
||||
struct rb_tree_node_t *left;
|
||||
/* Color. */
|
||||
rb_color_t color;
|
||||
/* Data. */
|
||||
|
|
|
@ -0,0 +1,296 @@
|
|||
/*
|
||||
* C_lib Copyright (C) 2021 Wael Karram.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* Include guard. */
|
||||
#ifndef RB_TREE_C
|
||||
#define RB_TREE_C
|
||||
#include "rb_tree.h"
|
||||
#endif /* RB_TREE_C */
|
||||
|
||||
/* Function implementations. */
|
||||
/* This function initializes an empty red-black tree. */
|
||||
/* Returns NULL on allocation failure. */
|
||||
static inline rb_tree_t* initialize_rb_tree() {
|
||||
/* Variables. */
|
||||
rb_tree_t* tree = (rb_tree_t*) malloc(sizeof(rb_tree_t));
|
||||
/* Check if the allocation succeeded and set the internal pointer if so. */
|
||||
if (!tree)
|
||||
return tree;
|
||||
tree->root = NULL;
|
||||
return tree;
|
||||
}
|
||||
|
||||
/* This function initializes a red-black tree and stores a value in the root. */
|
||||
/* Returns NULL on allocation failure. */
|
||||
static inline rb_tree_t* initialize_rb_tree_root_store(const void* const data) {
|
||||
/* Variables, delegate. */
|
||||
rb_tree_t* tree = initialize_rb_tree();
|
||||
/* Check for allocation failure. */
|
||||
if (!tree)
|
||||
return tree;
|
||||
tree->root = (rb_tree_node_t*) malloc(sizeof(rb_tree_node_t));
|
||||
/* Check for allocation failure. */
|
||||
if (!(tree->root)) {
|
||||
free(tree);
|
||||
return NULL;
|
||||
}
|
||||
/* Fill the data in, set the color to black and return. */
|
||||
tree->root->data = data;
|
||||
tree->root->color = BLACK;
|
||||
return tree;
|
||||
}
|
||||
|
||||
/* This function attempts to find the parent of a new node. */
|
||||
static rb_tree_t* find_parent(void* data, int (*comparison_function)(const void*, const void*));
|
||||
|
||||
/* This function inserts a new node. */
|
||||
static rb_tree_t* insert_node(void* data, int (*comparison_function)(const void*, const void*));
|
||||
|
||||
/* This function frees a whole Red-Black tree, not including data. */
|
||||
static inline void free_rb_tree(rb_tree_t* tree) {
|
||||
/* Sanity check. */
|
||||
if (!tree)
|
||||
return;
|
||||
/* Delegate. */
|
||||
free_rb_tree_recursive(tree->root);
|
||||
/* Free the struct. */
|
||||
free(tree);
|
||||
}
|
||||
|
||||
/* This function frees a whole Red-Black tree, including data. */
|
||||
/* Silently fails if free_function is NULL. */
|
||||
static inline void free_rb_tree_data(rb_tree_t* tree, void (*free_function)(const void*)) {
|
||||
/* Sanity check. */
|
||||
if (!free_function || !tree)
|
||||
return;
|
||||
|
||||
/* Delegate. */
|
||||
free_rb_tree_data_recursive(tree->root, free_function);
|
||||
/* Free the struct. */
|
||||
free(tree);
|
||||
}
|
||||
|
||||
/* This function handles removal of a node. */
|
||||
static inline void remove_rb_tree_node(rb_tree_t* tree, rb_tree_node_t* node);
|
||||
|
||||
/* This function handles removal of a node and the data thereof. */
|
||||
static inline void remove_rb_tree_node_data(rb_tree_t* tree, rb_tree_node_t* node, void (*free_function)(const void*));
|
||||
|
||||
/* This function finds a node by data search. */
|
||||
/* Returns NULL on error (not finding the node is an error too). */
|
||||
static inline rb_tree_node_t* find_rb_tree_node_data(rb_tree_t *tree, const void* data, int (*comparison_function)(const void*, const void*)) {
|
||||
/* Sanity check. */
|
||||
if (!tree || !(tree->root) || !comparison_fucntion)
|
||||
return NULL;
|
||||
/* Delegate. */
|
||||
return find_rb_tree_node_data_recursive(tree->root, data, comparison_function);
|
||||
}
|
||||
|
||||
|
||||
/* Helper functions. */
|
||||
/* This function recursively frees a red-black tree from the given node downwards, doesn't touch the stored data. */
|
||||
static void free_rb_tree_recursive(rb_tree_node_t* root) {
|
||||
/* Stopping conditions. */
|
||||
if (!root)
|
||||
return;
|
||||
if (!(root->right) && !(root->left)) {
|
||||
/* Check if it has a parent. */
|
||||
if (root->parent)
|
||||
if (CHILD_SIDE(root) == RIGHT)
|
||||
root->parent->right = NULL;
|
||||
else
|
||||
root->parent->left = NULL;
|
||||
/* Free. */
|
||||
free(root);
|
||||
return;
|
||||
}
|
||||
/* Check if the current node has a parent. */
|
||||
if (root->parent)
|
||||
if (CHILD_SIDE(root) == RIGHT)
|
||||
root->parent->right = NULL;
|
||||
else
|
||||
root->parent->left = NULL;
|
||||
/* Recurse. */
|
||||
free_rb_tree_recursive(root->right);
|
||||
free_rb_tree_recursive(root->left);
|
||||
/* Free. */
|
||||
free(root);
|
||||
}
|
||||
|
||||
/* This function recursively frees a red-black tree from the given node downwards, including stored data. */
|
||||
/* Doesn't do any sanity checking on the free_function. */
|
||||
static void free_rb_tree_data_recursive(rb_tree_node_t* root, void (*free_function)(const void*)) {
|
||||
/* Stopping conditions. */
|
||||
if (!root)
|
||||
return;
|
||||
if (!(root->right) && !(root->left)) {
|
||||
/* Check if it has a parent. */
|
||||
if (root->parent)
|
||||
if (CHILD_SIDE(root) == RIGHT)
|
||||
root->parent->right = NULL;
|
||||
else
|
||||
root->parent->left = NULL;
|
||||
/* Free. */
|
||||
free_function(root->data);
|
||||
free(root);
|
||||
return;
|
||||
}
|
||||
/* Check if the current node has a parent. */
|
||||
if (root->parent)
|
||||
if (CHILD_SIDE(root) == RIGHT)
|
||||
root->parent->right = NULL;
|
||||
else
|
||||
root->parent->left = NULL;
|
||||
/* Recurse. */
|
||||
free_rb_tree_data_recursive(root->right);
|
||||
free_rb_tree_data_recursive(root->left);
|
||||
/* Free. */
|
||||
free_function(root->data);
|
||||
free(root);
|
||||
}
|
||||
|
||||
/* This function recursively searches for a node by data. */
|
||||
/* Doesn't do any input validation. */
|
||||
static rb_tree_node_t* find_rb_tree_node_data_recursive(rb_tree_node_t* root, const void* data, int (*comparison_function)(const void*, const void*)) {
|
||||
/* Stopping conditions: NULL or leaf. */
|
||||
if (!root)
|
||||
return root;
|
||||
if (!(root->left) && !(root->right))
|
||||
return (!comparison_function(root, data)) ? root : NULL;
|
||||
|
||||
/* Recursive cases. */
|
||||
/* Check against the current node. */
|
||||
switch (comparison_function(root, data)) {
|
||||
case RED_BLACK_TREE_COMPARISON_SMALLER_THAN:
|
||||
return find_rb_tree_node_data_recursive(root->right, data, comparison_function);
|
||||
case RED_BLACK_TREE_COMPARISON_EQUAL_TO:
|
||||
return root;
|
||||
case RED_BLACK_TREE_COMPARISON_LARGER_THAN:
|
||||
return find_rb_tree_node_data_recursive(root->left, data, comparison_function);
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* This function applies a rotation upon a tree at the given root. */
|
||||
static inline rb_tree_node_t* rotate_direction_root(rb_tree_t* tree, rb_tree_node_t* root, rb_child_side side) {
|
||||
/* Check for NULL. */
|
||||
assert(tree && root);
|
||||
|
||||
/* Local variables. */
|
||||
rb_tree_node_t* grandparent, son, child;
|
||||
/* Set the nodes. */
|
||||
grandparent = root->parent;
|
||||
son = (side == LEFT) ? root->right : root->left;
|
||||
|
||||
/* Check for NULL. */
|
||||
assert(son);
|
||||
|
||||
/* Move around. */
|
||||
child = (side == LEFT) ? son->left : son->right;
|
||||
if (side == LEFT)
|
||||
parent->right = child;
|
||||
else
|
||||
parent->left = child;
|
||||
if (child != NULL)
|
||||
child->parent = parent;
|
||||
if (side == LEFT)
|
||||
son->left = parent;
|
||||
else
|
||||
son->right = parent;
|
||||
parent->parent = son;
|
||||
son->parent = grandparent;
|
||||
if (grandparent) {
|
||||
if (parent == grandparent->right)
|
||||
grandparent->right = son;
|
||||
else
|
||||
gradnparent->left = son;
|
||||
} else
|
||||
tree->root = son;
|
||||
|
||||
/* Return the new root of the subtree. */
|
||||
return son;
|
||||
}
|
||||
|
||||
/* Insert case 1, internal implementation. */
|
||||
/* Parent may be NULL. */
|
||||
static inline void rb_tree_insert_implementation(rb_tree_t* tree, rb_tree_node_t* node, rb_tree_node_t* parent, rb_child_side side) {
|
||||
/* Sanity check. */
|
||||
assert(tree && node);
|
||||
|
||||
/* Local variables. */
|
||||
rb_tree_node_t *grandparent, *uncle, *tmp;
|
||||
|
||||
/* Insert. */
|
||||
node->color = RED;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->parent = parent;
|
||||
/* Check if there is a parent or not. */
|
||||
if (!parent) {
|
||||
tree->root = node;
|
||||
return;
|
||||
} else {
|
||||
if (side == LEFT)
|
||||
parent->left = node;
|
||||
else
|
||||
parent->right = node;1
|
||||
}
|
||||
/* Rebalancing loop. */
|
||||
do {
|
||||
/* First case. */
|
||||
if (parent->color == BLACK)
|
||||
return;
|
||||
if ((grandparent = parent->parent) == NULL)
|
||||
goto case_insert_4; /* Parent is red and is root.*/
|
||||
/* else: Parent is red and grandparent isn't NULL. */
|
||||
/* Get the side of the grandfather where the parent is located. */
|
||||
side = CHILD_SIDE(parent);
|
||||
/* Get the uncle. */
|
||||
uncle = (side == LEFT) ? grandparent->right : grandparent->left;
|
||||
/* Check if uncle is black. */
|
||||
if (!uncle || uncle->color == BLACK)
|
||||
goto case_insert_56; /* Parent is red and uncle is black. */
|
||||
/* Second case. */
|
||||
parent->color = BLACK;
|
||||
uncle->color = BLACK;
|
||||
grandparent->color = RED;
|
||||
node = grandparent; /* Set the current node to the grandparent. */
|
||||
/* Iterate one black level up, I.E.: two tree levels up. */
|
||||
} while ((parent = node->parent) != NULL); /* End of do-while loop. */
|
||||
/* Third case: node is the root and is red. */
|
||||
return;
|
||||
/* Fourth case. */
|
||||
case_insert_4:
|
||||
parent->color = BLACK;
|
||||
return;
|
||||
/* First case, subcases 5 and 6. */
|
||||
/* Parent is red and uncle is black. */
|
||||
case_insert_56:
|
||||
tmp = (side == LEFT) ? parent->right : parent->left; /* Get the child according to the side. */
|
||||
if (node == tmp) {
|
||||
/* Fifth case: parent is red, uncle is black and node is an inner grandchild of grandparent. */
|
||||
rotate_direction(tree, parent, side); /* Parent is never the root. */
|
||||
node = parent; /* The new current node. */
|
||||
parent = (side == LEFT) ? grandparent->left : grandparent->right; /* New parent of node. */
|
||||
/* Fall through to the sixth case. */
|
||||
}
|
||||
/* Sixth case: parent is red, uncle is black and node is an outer grandchild of grandparent. */
|
||||
rotate_direction_root(tree, grandparent, (side == LEFT) ? RIGHT : LEFT); /* Grandparent may be the root. */
|
||||
/* Recolor. */
|
||||
parent->color = BLACK;
|
||||
grandparent->color = RED;
|
||||
}
|
|
@ -21,12 +21,15 @@
|
|||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
/* Include assert.h to allow code generation, but only run with debug flag. */
|
||||
#ifndef DEBUG
|
||||
#define NDEBUG
|
||||
#endif /* DEBUG. */
|
||||
#include <assert.h>
|
||||
|
||||
/* Define constants. */
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define LEFT 0
|
||||
#define RIGHT 1
|
||||
/* Return code for success. */
|
||||
#define RED_BLACK_TREE_SUCCESS_CODE 0
|
||||
/* Return code for failure. */
|
||||
|
@ -39,39 +42,53 @@
|
|||
#define RED_BLACK_TREE_COMPARISON_LARGER_THAN 1
|
||||
|
||||
/* Define tree struct. */
|
||||
typedef struct rb_tree {
|
||||
typedef struct rb_tree_t {
|
||||
/* Root node. */
|
||||
rb_tree_node *root;
|
||||
} rb_tree;
|
||||
rb_tree_node_t *root;
|
||||
} rb_tree_t;
|
||||
|
||||
/* Enum to define right an left sides of the root. */
|
||||
enum rb_child_side { LEFT, RIGHT };
|
||||
typedef enum rb_child_side rb_child_side;
|
||||
|
||||
/* Macro to get the side of the child in relation to the parent thereof. */
|
||||
#define CHILD_SIDE(N) (N == (N->parent)->right ? RIGHT : LEFT)
|
||||
|
||||
/* Define all the functions. */
|
||||
/* This function initializes a single node. */
|
||||
tree_node_t* initialize_binary_search_tree_node();
|
||||
/* This function initializes a single node and attempts to store the given data within. */
|
||||
tree_node_t* initialize_binary_search_tree_node_store(void *data);
|
||||
/* This helper function is used to recursively fill an array into a BST. */
|
||||
tree_node_t* initialize_binary_search_tree_recursive(void **array, int index_start, int index_end);
|
||||
/* This function creates a BST for a sorted array. */
|
||||
tree_node_t* initialize_binary_search_tree(void **array, int length);
|
||||
/* This function links a node to the right. */
|
||||
int link_binary_search_tree_node_right(tree_node_t *parent, tree_node_t *child);
|
||||
/* This function links a node to the left. */
|
||||
int link_binary_search_tree_node_left(tree_node_t *parent, tree_node_t *child);
|
||||
/* This function frees a whole tree - not including data. */
|
||||
void free_binary_search_tree(tree_node_t *root);
|
||||
/* This function frees a whole tree, including data. */
|
||||
void free_binary_search_tree_data(tree_node_t *root, void (*free_function)(const void*));
|
||||
/* This function inserts data to the tree. */
|
||||
int insert_node_into_binary_search_tree(tree_node_t *root, tree_node_t *to_insert, int (*comparison_function)(const void*, const void*));
|
||||
/* This function searches for data in the tree. */
|
||||
tree_node_t* search_in_binary_search_tree(tree_node_t *root, void *data, int (*comparison_function)(const void*, const void*));
|
||||
/* This function finds the tree's depth (at the deepest leaf). */
|
||||
size_t find_binary_search_tree_depth(tree_node_t *root);
|
||||
/* This function fills a binary search tree into an allocated array. */
|
||||
int fill_array_binary_search_tree(tree_node_t *root, void **array, size_t *index, size_t length);
|
||||
/* This function creates a sorted array, from a binary search tree. */
|
||||
void **sorted_array_from_binary_search_tree(tree_node_t *root, size_t *length, int *resize_status);
|
||||
/* Typedefs for rotation functions. */
|
||||
#define rotate_direction(T, N, direction) rotate_direction_root(T, N, direction)
|
||||
#define rotate_left(T, N) rotate_direction_root(T, N, LEFT)
|
||||
#define rotate_right(T, N) rotate_direction_root(T, N, RIGHT)
|
||||
|
||||
/* Function definitions. */
|
||||
/* This function initializes an empty red-black tree. */
|
||||
static inline rb_tree_t* initialize_rb_tree();
|
||||
/* This function initializes a red-black tree and stores a value in the root. */
|
||||
static inline rb_tree_t* initialize_rb_tree_root_store(const void* const data);
|
||||
/* This function attempts to find the parent of a new node. */
|
||||
static rb_tree_t* find_parent(void* data, int (*comparison_function)(const void*, const void*));
|
||||
/* This function inserts a new node. */
|
||||
static rb_tree_t* insert_node(void* data, int (*comparison_function)(const void*, const void*));
|
||||
/* This function frees a whole Red-Black tree, not including data. */
|
||||
static inline void free_rb_tree(rb_tree_t* tree);
|
||||
/* This function frees a whole Red-Black tree, including data. */
|
||||
static inline void free_rb_tree_data(rb_tree_t* tree, void (*free_function)(const void*));
|
||||
/* This function handles removal of a node. */
|
||||
static inline void remove_rb_tree_node(rb_tree_t* tree, rb_tree_node_t* node);
|
||||
/* This function handles removal of a node and the data thereof. */
|
||||
static inline void remove_rb_tree_node_data(rb_tree_t* tree, rb_tree_node_t* node, void (*free_function)(const void*));
|
||||
/* This function finds a node by data search. */
|
||||
static inline rb_tree_node_t* find_rb_tree_node_data(rb_tree_t *tree, const void* data, int (*comparison_function)(const void*, const void*));
|
||||
|
||||
/* Helper functions. */
|
||||
/* This function recursively frees a red-black tree from the given node downwards, doesn't touch the stored data. */
|
||||
static void free_rb_tree_recursive(rb_tree_node_t* root);
|
||||
/* This function recursively frees a red-black tree from the given node downwards, including stored data. */
|
||||
static void free_rb_tree_data_recursive(rb_tree_node_t* root, void (*free_function)(const void*));
|
||||
/* This function recursively searches for a node by data. */
|
||||
static rb_tree_node_t* find_rb_tree_node_data_recursive(rb_tree_node_t* root, const void* data, int (*comparison_function)(const void*, const void*));
|
||||
/* This function applies a rotation upon a tree at the given root. */
|
||||
static inline rb_tree_node_t* rotate_direction_root(rb_tree_t* tree, rb_tree_node_t* root, rb_child_side side);
|
||||
/* Insert, internal implementation. */
|
||||
static inline void rb_tree_insert_implementation(rb_tree_t* tree, rb_tree_node_t* node, rb_tree_node_t* parent, rb_child_side side);
|
||||
|
||||
#endif /* RED_BLACK_TREE_H */
|
||||
|
|
Loading…
Reference in New Issue