HW04
Source codes for forth assignment
heap.h
Go to the documentation of this file.
1 /**
2  * Interface for creating and working with heap.
3  *
4  * This file declares functions and types for creating heaps and retrieving
5  * items from it.
6  *
7  * @file heap.h
8  * @author Lubomir Sedlar
9  */
10 #ifndef HEAP_H
11 #define HEAP_H
12 
13 #include "graph.h"
14 
15 /**
16  * Heap is an opaque structure.
17  * Do not access its members directly, use provided functions.
18  */
19 typedef struct heap Heap;
20 
21 /**
22  * Create new heap with nodes from given graph.
23  * This function does not copy all node information, only the array of pointers
24  * to nodes. Free the allocated memory with heap_free(). All nodes in the heap
25  * are given new distance of `UINT_MAX` (ie. infinity) and NULL previous node.
26  *
27  * Use heap_decrease_distance() to change distance of a node.
28  *
29  * @param g graph to copy nodes from
30  * @return newly created heap
31  */
33 
34 /**
35  * Test if a heap is empty.
36  *
37  * @param h heap to be tested
38  * @return true if heap is empty, false otherwise
39  */
40 bool heap_is_empty(Heap *h);
41 
42 /**
43  * Remove minimal element from the heap, i.e. node with minimal value of dist,
44  * and return it.
45  *
46  * @param h heap to extract from
47  * @return former minimal element
48  */
50 
51 /**
52  * Decrease distance of a certain node.
53  * The `val` parameter is the new value of that node. It must be less then the
54  * original value. This property is tested with an `assert`, violating it
55  * leads to program crash.
56  *
57  * To track the path in a graph, this function also expects previous node of
58  * the modified node. Previous node means the node from which the modified
59  * node was reached.
60  *
61  * @param h heap to be modified
62  * @param n node that should be changed
63  * @param val new value of the node
64  * @param prev new previous node of modified node
65  */
66 void heap_decrease_distance(Heap *h, Node *n, unsigned int val, Node *prev);
67 
68 /**
69  * Free all memory used by the heap.
70  *
71  * @param h heap to be freed
72  */
73 void heap_free(Heap *h);
74 
75 #endif /* end of include guard: HEAP_H */
void heap_decrease_distance(Heap *h, Node *n, unsigned int val, Node *prev)
Decrease distance of a certain node.
Definition: heap.c:123
bool heap_is_empty(Heap *h)
Test if a heap is empty.
Definition: heap.c:109
Node * heap_extract_min(Heap *h)
Remove minimal element from the heap, i.e.
Definition: heap.c:114
struct node Node
Node is an opaque type.
Definition: graph.h:19
Heap * heap_new_from_graph(Graph *g)
Create new heap with nodes from given graph.
Definition: heap.c:89
void heap_free(Heap *h)
Free all memory used by the heap.
Definition: heap.c:132
Interface for creating graph.
struct heap Heap
Heap is an opaque structure.
Definition: heap.h:19
struct graph Graph
Graph is an opaque type.
Definition: graph.h:25