1
0
Fork 0
C_lib/trees/heaps/max_heap/max_heap.h

123 lines
6.2 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 max heap and related functions thereof. */
#ifndef MAX_HEAP_H
#define MAX_HEAP_H
#include <stdlib.h>
#include "../../../nodes/heap_node.h"
/* Check if using GCC or not. */
#ifndef __GNUC__
#define __typeof__ typeof
#endif /* __GNUC__ */
/* TODO: Check if this breaks compilation with TCC? */
/* Macros. */
#define MAX(a,b) ({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
(void) (&_a == &_b); \
_a < _b ? _a : _b; })
/* Define constants. */
#define TRUE 1
#define FALSE 0
/* Error code used when sanity check fails. */
#define MAX_HEAP_CHECK_ERROR -1
/* Return code for success. */
#define MAX_HEAP_SUCCESS_CODE 0
/* Return code for failure. */
#define MAX_HEAP_FAILURE_CODE 1
/* Return code for comparison, smaller. */
#define MAX_HEAP_COMPARISON_SMALLER_THAN -1
/* Return code for comparison, equal. */
#define MAX_HEAP_COMPARISON_EQUAL_TO 0
/* Return code for comparison, larger. */
#define MAX_HEAP_COMPARISON_LARGER_THAN 1
/* Define the heap struct. */
typedef struct max_heap {
/* Pointer to root. */
heap_node_t *root;
/* Size. */
size_t size;
} max_heap_t;
/* Define all the functions. */
/* This function initializes a new empty heap. */
static inline max_heap_t* create_max_heap();
/* This function initializes a new heap and attempts to store the given data within. */
static inline max_heap_t* initialize_max_heap_node_store(void *data);
/* This helper function is used to recursively fill a sorted array into a new heap. */
max_heap_t* initialize_max_heap_recursive(void **array, int index_start, int index_end, max_heap_t *heap);
/* This function creates a heap for a sorted array. */
max_heap_t* initialize_max_heap(void **array, int length);
/* This function combines two heaps into one - keeps the orignal ones. */
max_heap_t* merge_max_heaps(max_heap_t *first_heap, max_heap_t *second_heap);
/* This function combines two heaps into one - destroys the orignal ones. */
max_heap_t* meld_max_heaps(max_heap_t *first_heap, max_heap_t *second_heap);
/* This function peeks at the maximum. */
static inline heap_node_t* find_max_max_heap(max_heap_t *heap);
/* This function creates a new node with the given data and inserts it into the heap. */
static inline max_heap_t* insert_node_data_max_heap(max_heap_t *heap, void *data, (*comparison_function)(const void*, const void*));
/* This function extracts the max. */
static inline void* extract_max_max_heap(max_heap_t *heap);
/* This function deletes the max. */
static inline void delete_max_max_heap(max_heap_t *heap);
/* This function replaces the root. */
static inline void replace_root_max_heap(max_heap_t *heap, void *data);
/* This function returns the size of the heap. */
static inline size_t size_max_heap(max_heap_t *heap);
/* This function returns whether or not the heap is empty. */
static inline int is_emtpy_max_heap(max_heap_t *heap);
/* This function frees a whole heap - not including data. */
static inline void free_max_heap(max_heap_t *heap);
/* This function frees a whole heap, including data. */
static inline void free_max_heap_data(max_heap_t *heap, void (*free_function)(const void*));
/* This function inserts data to the heap. */
static inline int insert_node_into_max_heap(max_heap_t *heap, heap_node_t *to_insert, int (*comparison_function)(const void*, const void*));
/* This function inserts data to the heap. */
static inline int insert_data_into_max_heap(max_heap_t *heap, void *data, int (*comparison_function)(const void*, const void*));
/* This function searches for data in the heap. */
max_heap_t* search_in_max_heap(max_heap_t *heap, void *data, int (*comparison_function)(const void*, const void*));
/* This function finds the heap's depth (at the deepest leaf). */
static inline size_t find_max_heap_depth(max_heap_t *heap);
/* This function fills a max heap into an allocated array. */
int fill_array_max_heap(max_heap_t *heap, void **array, size_t *index, size_t length);
/* This function creates a sorted array, from a max heap. */
void *sorted_array_from_max_heap(max_heap_t *heap, size_t *length, int *resize_status);
/* Internal functions. */
/* This function increases the key of the current node. */
void increase_key_max_heap(max_heap_t *heap, void *data, int (*comparison_function)(const void*, const void*), void *increase, int (*increase_function)(const void*, const void*));
/* This function deletes an arbitrary node. */
void delete_node_max_heap(max_heap_t *heap, void *data, int (*comparison_function)(const void*, const void*));
/* This function moves a node downwards to its location. */
void sift_down_max_heap(max_heap_t *heap, tree_node_t *node, int (*comparison_function)(const void*, const void*));
/* This function moves a node upwards to its location. */
void sift_up_max_heap(max_heap_t *heap, tree_node_t *node, int (*comparison_function)(const void*, const void*));
/* This function allocates a new node, pointing to the given data within. */
static inline heap_node_t* allocate_heap_node(void *data);
/* This function recursively frees heap nodes without freeing the data thereof. */
static inline void recursive_free_heap_node(heap_node_t *root);
/* This function recursively frees heap nodes while freeing the data thereof. */
static inline void recursive_free_heap_node_data(heap_node_t *root, void (*free_function)(const void*));
/* This function recursively looks for the max depth in the heap. */
static inline size_t calculate_max_heap_depth(heap_node_t *heap, size_t max);
/* This function handles insertion of a single node into a heap - relies on heapification upwards. */
static inline void insert_node_int_max_heap_recursive(heap_node_t *root, heap_node_t *node, int (*comparison_function)(const void*, const void*));
#endif /* MAX_HEAP_H */