1
0
Fork 0

More work on max heap.

This commit is contained in:
wael 2022-01-29 20:59:13 +02:00
parent f3c3735e47
commit a25860a4ac
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
2 changed files with 63 additions and 10 deletions

View File

@ -40,12 +40,12 @@ static inline max_heap_t* initialize_max_heap_node_store(void *data) {
/* Attempt to allocate the heap. */
result = create_max_heap();
if (result == NULL)
if (!result)
return NULL;
/* Allocate the node. */
node = allocate_heap_node(data);
if (node == NULL) {
if (!node) {
free(result);
return NULL;
}
@ -58,11 +58,64 @@ static inline max_heap_t* initialize_max_heap_node_store(void *data) {
return result;
}
/* This is the recursive function that implements heap insertion from an array. */
/* Returns failure code on failure, and success code on success. */
int initialize_max_heap_recursive_implementation(void **array, size_t index_start, size_t index_end, heap_node_t *root) {
/* Stopping condition/sanity check. */
if (!root || index_start >= index_end)
return MAX_HEAP_SUCCESS_CODE;
/* Take off the first two. */
if (index_end - index_start >= 1) {
/* Attempt to allocate. */
root->right = (max_heap_t*) malloc(sizeof(max_heap_t));
root->left = (max_heap_t*) malloc(sizeof(max_heap_t));
if (!(root->right) || !(root->left)) {
free(root->right);
free(root->left);
return MAX_HEAP_FAILURE_CODE;
}
/* Fill the data. */
root->right->data = array[index_end];
root->left->data = array[--index_end];
} else {
/* Take off just one. */
root->left = (max_heap_t*) malloc(sizeof(max_heap_t));
if (!(root-left)) {
free(root->left);
return MAX_HEAP_FAILURE_CODE;
}
/* Fill the data. */
root->left->data = array[index_start];
return MAX_HEAP_SUCCESS_CODE;
}
/* Recursive case. */
index_end--;
return initialize_max_heap_recursive_implementation(array, index_start, (size_t)((index_end - index_start) / 2), root->left) && initialize_max_heap_recursive_implementation(array, (size_t)((index_end - index_start) / 2), index_end, root->right);
}
/* This helper function is used to recursively fill a sorted array into a new heap. */
/* Returns NULL on error, doesn't check if heap is allocated or not (it should be). */
max_heap_t* initialize_max_heap_recursive(void **array, size_t index_start, size_t index_end, max_heap_t *heap) {
/* Put the first one in. */
heap->root = (max_heap_t*) malloc(sizeof(max_heap_t));
if (!(heap->root))
return NULL;
heap->root->data = array[index_end];
/* Recurse. */
if(!initialize_max_heap_recursive_implemetation(array, index_start, --index_end, heap->root)) {
free_max_heap(heap);
return NULL;
}
/* Return. */
return heap;
}
/* This function "peeks" at the maximum of the heap. */
/* Returns NULL on errornous input, otherwise returns the max node. */
static inline heap_node_t* find_max_max_heap(max_heap_t *heap) {
/* Check and return. */
return (heap != NULL) ? heap->root : NULL;
return (heap) ? heap->root : NULL;
}
/* Extract the max of the heap. */
@ -90,7 +143,7 @@ static inline void replace_root_max_heap(max_heap_t *heap, void *data) {
heap->root->data = data;
}
/* This function returns the size of the heap, will return -1 on error! */
/* This function returns the size of the heap, will return SIZE_MAX on error! */
static inline size_t size_max_heap(max_heap_t *heap) {
return (heap == NULL) ? MAX_HEAP_CHECK_ERROR : heap->size;
}
@ -132,7 +185,7 @@ static inline void free_max_heap_data(max_heap_t *heap, void (*free_function)(co
}
/* This function finds the heap's depth at the deepest leaf. */
/* Will return -1 if the heap is empty (I.E.: root is NULL). */
/* Will return SIZE_MAX if the heap is empty (I.E.: root is NULL). */
static inline size_t find_max_heap_depth(max_heap_t *heap) {
/* Sanity check. */
if (!heap)
@ -220,8 +273,8 @@ static inline size_t calculate_max_heap_depth(heap_node_t *root, size_t max) {
return max;
/* Recursive calls. */
left = (!root->left) ? (size_t) -1L : calculate_max_heap_depth(root->left);
right = (!root->right) ? (size_t) -1L : calculate_max_heap_depth(root->right);
left = (!root->left) ? (size_t) SIZE_MAX : calculate_max_heap_depth(root->left);
right = (!root->right) ? (size_t) SIZE_MAX : calculate_max_heap_depth(root->right);
/* Return the max. */
return MAX(left, MAX(right, max));

View File

@ -62,8 +62,10 @@ typedef struct max_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 is the recursive function that implements heap insertion from an array. */
int initialize_max_heap_recursive_implementation(void **array, size_t index_start, size_t index_end, heap_node_t *root);
/* 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);
max_heap_t* initialize_max_heap_recursive(void **array, size_t index_start, size_t 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. */
@ -72,8 +74,6 @@ max_heap_t* merge_max_heaps(max_heap_t *first_heap, max_heap_t *second_heap);
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. */