1
0
C_lib/trees/binary_search_tree/binary_search_tree.h

73 lines
4.1 KiB
C

/*
* 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 BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#include "../binary_tree/binary_tree.h"
/* Define constants. */
#define TRUE 1
#define FALSE 0
/* Return code for success. */
#define BINARY_SEARCH_TREE_SUCCESS_CODE 0
/* Return code for failure. */
#define BINARY_SEARCH_TREE_FAILURE_CODE 1
/* Return code for comparison, smaller. */
#define BINARY_SEARCH_TREE_COMPARISON_SMALLER_THAN -1
/* Return code for comparison, equal. */
#define BINARY_SEARCH_TREE_COMPARISON_EQUAL_TO 0
/* Return code for comparison, larger. */
#define BINARY_SEARCH_TREE_COMPARISON_LARGER_THAN 1
/* Define all the functions. */
/* This function initializes a single node. */
static inline tree_node_t* initialize_binary_search_tree_node();
/* This function initializes a single node and attempts to store the given data within. */
static inline tree_node_t* initialize_binary_search_tree_node_store(void* const data);
/* This helper function is used to recursively fill an array into a BST. */
static inline tree_node_t* initialize_binary_search_tree_recursive(void** const array, size_t index_start, size_t index_end);
/* This function creates a BST for a sorted array. */
static inline tree_node_t* initialize_binary_search_tree(void** const array, size_t length);
/* This function links a node to the right. */
static inline link_binary_search_tree_node_right(tree_node_t *parent, tree_node_t *child);
/* This function links a node to the left. */
static inline link_binary_search_tree_node_left(tree_node_t *parent, tree_node_t *child);
/* This function frees a whole tree - not including data. */
static inline void free_binary_search_tree(tree_node_t *root);
/* This function frees a whole tree, including data. */
static inline void free_binary_search_tree_data(tree_node_t *root, void (*free_function)(const void*));
/* This function inserts data to the tree. */
static inline void 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. */
static inline tree_node_t* search_in_binary_search_tree(const tree_node_t* const root, const void* const data, int (*comparison_function)(const void*, const void*));
/* This function searches for data in the tree. */
static inline tree_node_t* search_in_binary_search_tree_iterative(const tree_node_t* const root, const void* const data, int (*comparison_function)(const void*, const void*));
/* This function finds the tree's depth (at the deepest leaf). */
static inline size_t find_binary_search_tree_depth(const tree_node_t* const root);
/* This function fills a binary search tree into an allocated array. */
static inline void fill_array_binary_search_tree(tree_node_t *root, void **array, size_t *index, const size_t length);
/* This function creates a sorted array, from a binary search tree. */
static inline void **sorted_array_from_binary_search_tree(tree_node_t *root, size_t *length, int *resize_status);
/* This function finds the tree's minimum. */
static inline tree_node_t* find_minimum_binary_search_tree(const tree_node_t* const root, int (*comparison_function)(const void*, const void*));
/* This function finds the tree's maximum. */
static inline tree_node_t* find_maximum_binary_search_tree(const tree_node_t* const root, int (*comparison_function)(const void*, const void*));
#endif /* BINARY_SEARCH_TREE_H */