More heap work
This commit is contained in:
parent
d2e629bec6
commit
cc556de1ea
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* 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 header defines a node in a heap. */
|
||||
#ifndef HEAP_NODE_H
|
||||
#define HEAP_NODE_H
|
||||
typedef struct heap_node {
|
||||
/* Parent node. */
|
||||
struct heap_node *parnet;
|
||||
/* Right node. */
|
||||
struct heap_node *right;
|
||||
/* Left node. */
|
||||
struct heap_node *left;
|
||||
/* Data. */
|
||||
void *data;
|
||||
} heap_node_t;
|
||||
#endif /* HEAP_NODE_H */
|
|
@ -15,11 +15,6 @@
|
|||
*/
|
||||
|
||||
/* Includes. */
|
||||
/* Include guard. */
|
||||
#ifndef BINARY_TREE_C_INCLUDE
|
||||
#define BINARY_TREE_C_INCLUDE
|
||||
#include "../../binary_tree/binary_tree.c"
|
||||
#endif /* BINARY_TREE_C_INCLUDE */
|
||||
#include "max_heap.h"
|
||||
|
||||
/* Function implementations. */
|
||||
|
@ -41,23 +36,20 @@ inline max_heap_t* create_max_heap() {
|
|||
max_heap_t* initialize_max_heap_node_store(void *data) {
|
||||
/* Local variables. */
|
||||
max_heap_t *result;
|
||||
tree_node_t *node;
|
||||
heap_node_t *node;
|
||||
|
||||
/* Attempt to allocate the heap. */
|
||||
result = create_max_heap();
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Attempt to allocate the node. */
|
||||
node = (tree_node_t*) malloc(sizeof(tree_node_t));
|
||||
/* Allocate the node. */
|
||||
node = allocate_heap_node(data);
|
||||
if (node == NULL) {
|
||||
free(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Store the data. */
|
||||
node->data = data;
|
||||
|
||||
/* Initialize the heap. */
|
||||
result->root = node;
|
||||
result->size++;
|
||||
|
@ -87,4 +79,30 @@ int is_empty_max_heap(max_heap_t *heap) {
|
|||
}
|
||||
|
||||
/* Internal functions. */
|
||||
/* 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*)) {
|
||||
/* Keep swapping with the parent until it is in the right place. */
|
||||
}
|
||||
|
||||
/* This function allocates a new heap node, will all pointers set to NULL. Returns NULL on failure. */
|
||||
/* Fills it with the given data. */
|
||||
heap_node_t* allocate_heap_node(void *data) {
|
||||
/* Variables. */
|
||||
heap_node_t *node;
|
||||
|
||||
/* Attempt to allocate the node. */
|
||||
node = (heap_node_t*) malloc(sizeof(tree_node_t));
|
||||
if (node == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Store the data. */
|
||||
node->data = data;
|
||||
|
||||
/* Set parent and children to NULL. */
|
||||
node->parent = NULL;
|
||||
node->right = NULL;
|
||||
node->left = NULL;
|
||||
|
||||
/* Return the pointer. */
|
||||
return node;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "../binary_tree/binary_tree.h"
|
||||
#include "../../../nodes/heap_node.h"
|
||||
|
||||
/* Define constants. */
|
||||
#define TRUE 1
|
||||
|
@ -40,8 +40,8 @@
|
|||
|
||||
/* Define the heap struct. */
|
||||
typedef struct max_heap {
|
||||
/* Pointer to internal tree. */
|
||||
tree_node_t* root;
|
||||
/* Pointer to root. */
|
||||
heap_node_t *root;
|
||||
/* Size. */
|
||||
size_t size;
|
||||
} max_heap_t;
|
||||
|
@ -96,4 +96,6 @@ void delete_node_max_heap(max_heap_t *heap, void *data, int (*comparison_functio
|
|||
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. */
|
||||
heap_node_t* allocate_heap_node(void *data);
|
||||
#endif /* MAX_HEAP_H */
|
||||
|
|
Loading…
Reference in New Issue