Added max heap header
This commit is contained in:
parent
5983740795
commit
8701a00fdf
|
@ -0,0 +1,81 @@
|
|||
/* This file defines a max heap and related functions thereof. */
|
||||
#ifndef MAX_HEAP_H
|
||||
#define MAX_HEAP_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 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 internal tree. */
|
||||
tree_node_t* root;
|
||||
/* Size. */
|
||||
size_t size;
|
||||
} max_heap_t;
|
||||
|
||||
/* Define all the functions. */
|
||||
/* This function initializes a new empty heap. */
|
||||
max_heap_t* create_max_heap_node();
|
||||
/* This function initializes a new heap and attempts to store the given data within. */
|
||||
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);
|
||||
/* 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. */
|
||||
max_heap_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. */
|
||||
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. */
|
||||
void* extract_max_max_heap(max_heap_t *heap);
|
||||
/* This function deletes the max. */
|
||||
void* delete_max_max_heap(max_heap_t *heap);
|
||||
/* This function replaces the root. */
|
||||
void* replace_root_max_heap(max_heap_t *heap);
|
||||
/* This function returns the size of the heap. */
|
||||
size_t size_max_heap(max_heap_t *heap);
|
||||
/* This function returns whether or not the heap is empty. */
|
||||
int is_emtpy_max_heap(max_heap_t *heap);
|
||||
/* This function frees a whole heap - not including data. */
|
||||
void free_max_heap(max_heap_t *heap);
|
||||
/* This function frees a whole heap, including data. */
|
||||
void free_max_heap_data(max_heap_t *heap, void (*free_function)(const void*));
|
||||
/* This function inserts data to the heap. */
|
||||
int insert_node_into_max_heap(max_heap_t *heap, max_heap_t *to_insert, 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). */
|
||||
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*));
|
||||
#endif /* MAX_HEAP_H */
|
Loading…
Reference in New Issue