1
0
Fork 0

Start of work on red-black trees.

This commit is contained in:
wael 2022-01-30 15:17:35 +02:00
parent 829049ed3f
commit 77b9e2e264
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
2 changed files with 115 additions and 0 deletions

38
nodes/rb_tree_node.h Normal file
View File

@ -0,0 +1,38 @@
/*
* 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/>.
*/
/* This header defines a node in a binary tree. */
#ifndef RED_BLACK_TREE_NODE_H
#define RED_BLACK_TREE_NODE_H
/* This enum is used to define node colors. */
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 {
/* Parent node. */
struct rb_tree_node *parent;
/* Right node. */
struct rb_tree_node *right;
/* Left node. */
struct rb_tree_node *left;
/* Color. */
rb_color_t color;
/* Data. */
void *data;
} rb_tree_node_t;
#endif /* RED_BLACK_TREE_NODE_H */

77
trees/RB_tree/rb_tree.h Normal file
View File

@ -0,0 +1,77 @@
/*
* 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/>.
*/
/* This file defines a simple binary tree and related functions thereof. */
#ifndef RED_BLACK_TREE_H
#define RED_BLACK_TREE_H
#include "../../nodes/rb_tree_node.h"
#include <math.h>
#include <stdlib.h>
#include <errno.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. */
#define RED_BLACK_TREE_FAILURE_CODE 1
/* Return code for comparison, smaller. */
#define RED_BLACK_TREE_COMPARISON_SMALLER_THAN -1
/* Return code for comparison, equal. */
#define RED_BLACK_TREE_COMPARISON_EQUAL_TO 0
/* Return code for comparison, larger. */
#define RED_BLACK_TREE_COMPARISON_LARGER_THAN 1
/* Define tree struct. */
typedef struct rb_tree {
/* Root node. */
rb_tree_node *root;
} rb_tree;
/* 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);
#endif /* RED_BLACK_TREE_H */