forked from robin-thomas/min-heap
/
minHeap.c
214 lines (179 loc) · 5.17 KB
/
minHeap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
File: minHeap.c
Desc: Program showing various operations on a binary min heap
Author: Robin Thomas <robinthomas2591@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LCHILD(x) 2 * x + 1
#define RCHILD(x) 2 * x + 2
#define PARENT(x) (x - 1) / 2
typedef struct node {
int data ;
} node ;
typedef struct minHeap {
int size ;
node *elem ;
} minHeap ;
/*
Function to initialize the min heap with size = 0
*/
minHeap initMinHeap(int size) {
minHeap hp ;
hp.size = 0 ;
return hp ;
}
/*
Function to swap data within two nodes of the min heap using pointers
*/
void swap(node *n1, node *n2) {
node temp = *n1 ;
*n1 = *n2 ;
*n2 = temp ;
}
/*
Heapify function is used to make sure that the heap property is never violated
In case of deletion of a node, or creating a min heap from an array, heap property
may be violated. In such cases, heapify function can be called to make sure that
heap property is never violated
*/
void heapify(minHeap *hp, int i) {
int smallest = (LCHILD(i) < hp->size && hp->elem[LCHILD(i)].data < hp->elem[i].data) ? LCHILD(i) : i ;
if(RCHILD(i) < hp->size && hp->elem[RCHILD(i)].data < hp->elem[smallest].data) {
smallest = RCHILD(i) ;
}
if(smallest != i) {
swap(&(hp->elem[i]), &(hp->elem[smallest])) ;
heapify(hp, smallest) ;
}
}
/*
Build a Min Heap given an array of numbers
Instead of using insertNode() function n times for total complexity of O(nlogn),
we can use the buildMinHeap() function to build the heap in O(n) time
*/
void buildMinHeap(minHeap *hp, int *arr, int size) {
int i ;
// Insertion into the heap without violating the shape property
for(i = 0; i < size; i++) {
if(hp->size) {
hp->elem = realloc(hp->elem, (hp->size + 1) * sizeof(node)) ;
} else {
hp->elem = malloc(sizeof(node)) ;
}
node nd ;
nd.data = arr[i] ;
hp->elem[(hp->size)++] = nd ;
}
// Making sure that heap property is also satisfied
for(i = (hp->size - 1) / 2; i >= 0; i--) {
heapify(hp, i) ;
}
}
/*
Function to insert a node into the min heap, by allocating space for that node in the
heap and also making sure that the heap property and shape propety are never violated.
*/
void insertNode(minHeap *hp, int data) {
if(hp->size) {
hp->elem = realloc(hp->elem, (hp->size + 1) * sizeof(node)) ;
} else {
hp->elem = malloc(sizeof(node)) ;
}
node nd ;
nd.data = data ;
int i = (hp->size)++ ;
while(i && nd.data < hp->elem[PARENT(i)].data) {
hp->elem[i] = hp->elem[PARENT(i)] ;
i = PARENT(i) ;
}
hp->elem[i] = nd ;
}
/*
Function to delete a node from the min heap
It shall remove the root node, and place the last node in its place
and then call heapify function to make sure that the heap property
is never violated
*/
void deleteNode(minHeap *hp) {
if(hp->size) {
printf("Deleting node %d\n\n", hp->elem[0].data) ;
hp->elem[0] = hp->elem[--(hp->size)] ;
hp->elem = realloc(hp->elem, hp->size * sizeof(node)) ;
heapify(hp, 0) ;
} else {
printf("\nMin Heap is empty!\n") ;
free(hp->elem) ;
}
}
/*
Function to get maximum node from a min heap
The maximum node shall always be one of the leaf nodes. So we shall recursively
move through both left and right child, until we find their maximum nodes, and
compare which is larger. It shall be done recursively until we get the maximum
node
*/
int getMaxNode(minHeap *hp, int i) {
if(LCHILD(i) >= hp->size) {
return hp->elem[i].data ;
}
int l = getMaxNode(hp, LCHILD(i)) ;
int r = getMaxNode(hp, RCHILD(i)) ;
if(l >= r) {
return l ;
} else {
return r ;
}
}
/*
Function to clear the memory allocated for the min heap
*/
void deleteMinHeap(minHeap *hp) {
free(hp->elem) ;
}
/*
Function to display all the nodes in the min heap by doing a inorder traversal
*/
void inorderTraversal(minHeap *hp, int i) {
if(LCHILD(i) < hp->size) {
inorderTraversal(hp, LCHILD(i)) ;
}
printf("%d ", hp->elem[i].data) ;
if(RCHILD(i) < hp->size) {
inorderTraversal(hp, RCHILD(i)) ;
}
}
/*
Function to display all the nodes in the min heap by doing a preorder traversal
*/
void preorderTraversal(minHeap *hp, int i) {
if(LCHILD(i) < hp->size) {
preorderTraversal(hp, LCHILD(i)) ;
}
if(RCHILD(i) < hp->size) {
preorderTraversal(hp, RCHILD(i)) ;
}
printf("%d ", hp->elem[i].data) ;
}
/*
Function to display all the nodes in the min heap by doing a post order traversal
*/
void postorderTraversal(minHeap *hp, int i) {
printf("%d ", hp->elem[i].data) ;
if(LCHILD(i) < hp->size) {
postorderTraversal(hp, LCHILD(i)) ;
}
if(RCHILD(i) < hp->size) {
postorderTraversal(hp, RCHILD(i)) ;
}
}
/*
Function to display all the nodes in the min heap by doing a level order traversal
*/
void levelorderTraversal(minHeap *hp) {
int i ;
for(i = 0; i < hp->size; i++) {
printf("%d ", hp->elem[i].data) ;
}
}