Further improvements to max heap, still needs core implementation of adding and deleting data (last outstanding issue).
This commit is contained in:
parent
6a5855e4cd
commit
599b92d77d
|
@ -20,7 +20,7 @@
|
|||
/* Function implementations. */
|
||||
|
||||
/* This function initializes and returns a new EMPTY heap. Returning the pointer thereof on success, or NULL on failure. */
|
||||
inline max_heap_t* create_max_heap() {
|
||||
static inline max_heap_t* create_max_heap() {
|
||||
/* Attempt to allocate and return accordingly. */
|
||||
max_heap_t *result = (max_heap_t*) malloc(sizeof(max_heap_t));
|
||||
if (result == NULL)
|
||||
|
@ -33,7 +33,7 @@ inline max_heap_t* create_max_heap() {
|
|||
}
|
||||
|
||||
/* This function attempts to create a new heap and store the data at the root node. Returns a pointer to the heap on success, otherwise NULL. */
|
||||
max_heap_t* initialize_max_heap_node_store(void *data) {
|
||||
static inline max_heap_t* initialize_max_heap_node_store(void *data) {
|
||||
/* Local variables. */
|
||||
max_heap_t *result;
|
||||
heap_node_t *node;
|
||||
|
@ -65,14 +65,33 @@ static inline heap_node_t* find_max_max_heap(max_heap_t *heap) {
|
|||
return (heap != NULL) ? heap->root : NULL;
|
||||
}
|
||||
|
||||
/* Extract the max of the heap. */
|
||||
static inline void* extract_max_max_heap(max_heap_t *heap) {
|
||||
/* Local variables. */
|
||||
void *data;
|
||||
|
||||
/* Sanity check. */
|
||||
if (!heap || !(heap->root))
|
||||
return NULL;
|
||||
|
||||
/* Get the data pointer. */
|
||||
data = heap->root->data;
|
||||
|
||||
/* Delete the root. */
|
||||
delete_max_max_heap(heap);
|
||||
|
||||
/* Return the data pointer. */
|
||||
return data;
|
||||
}
|
||||
|
||||
/* This function returns the size of the heap, will return -1 on error! */
|
||||
inline size_t size_max_heap(max_heap_t *heap) {
|
||||
static inline size_t size_max_heap(max_heap_t *heap) {
|
||||
return (heap == NULL) ? MAX_HEAP_CHECK_ERROR : heap->size;
|
||||
}
|
||||
|
||||
/* This function returns true or false according to if the heap is empty or not, returns MAX_HEAP_CHCEK_ERROR when the state is undefined. */
|
||||
/* Expect undefined behaviour if size < 0. */
|
||||
int is_empty_max_heap(max_heap_t *heap) {
|
||||
static inline int is_empty_max_heap(max_heap_t *heap) {
|
||||
/* Rely on the size function. */
|
||||
size_t size = size_max_heap(heap);
|
||||
/* Check and return. */
|
||||
|
@ -125,7 +144,7 @@ void sift_up_max_heap(max_heap_t *heap, tree_node_t *node, int (*comparison_func
|
|||
|
||||
/* This function allocates a new heap node, with all pointers to other nodes set to NULL. Returns NULL on failure. */
|
||||
/* Fills it with the given data - I.E.: points to said data. */
|
||||
heap_node_t* allocate_heap_node(void *data) {
|
||||
static inline heap_node_t* allocate_heap_node(void *data) {
|
||||
/* Variables. */
|
||||
heap_node_t *node;
|
||||
|
||||
|
|
|
@ -59,9 +59,9 @@ typedef struct max_heap {
|
|||
|
||||
/* Define all the functions. */
|
||||
/* This function initializes a new empty heap. */
|
||||
inline max_heap_t* create_max_heap();
|
||||
static inline max_heap_t* create_max_heap();
|
||||
/* This function initializes a new heap and attempts to store the given data within. */
|
||||
max_heap_t* initialize_max_heap_node_store(void *data);
|
||||
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. */
|
||||
|
@ -73,17 +73,17 @@ 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. */
|
||||
max_heap_t* insert_node_data_max_heap(max_heap_t *heap, void *data, (*comparison_function)(const void*, const void*));
|
||||
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. */
|
||||
void* extract_max_max_heap(max_heap_t *heap);
|
||||
static inline 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, void *data);
|
||||
/* This function returns the size of the heap. */
|
||||
inline size_t size_max_heap(max_heap_t *heap);
|
||||
static inline 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);
|
||||
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. */
|
||||
|
@ -108,7 +108,7 @@ void sift_down_max_heap(max_heap_t *heap, tree_node_t *node, int (*comparison_fu
|
|||
/* 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. */
|
||||
heap_node_t* allocate_heap_node(void *data);
|
||||
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. */
|
||||
|
|
Loading…
Reference in New Issue