C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
heap.c
Go to the documentation of this file.
1 
6 #include "heap.h"
7 
8 void heap_insert(t_gen d,t_gen data);
10 void heap_build(t_gen d);
11 void heap_sort(t_gen d);
12 int heap_len(t_gen d);
13 void heap_print(t_gen d);
14 bool heap_empty(t_gen d);
15 bool heap_full(t_gen d);
16 t_gen heap_update_key(t_gen d, t_gen val, int idx);
17 void destroy_heap(t_gen d);
18 
19 
29 t_gen create_heap(char *name, t_gen data, int size, e_heaptype htype, t_dparams *prm)
30 {
31  t_heap *h = get_mem(1, sizeof(t_heap));
32 
33  // Initailze heap Params
34  h->name = name;
35  h->type = htype;
36  h->data = data;
37  h->size = size;
38  h->count = 0;
39 
40  // Initailze heap routines
41  h->insert = heap_insert;
43  h->build = heap_build;
44  h->sort = heap_sort;
45  h->len = heap_len;
47  h->full = heap_full;
48  h->empty = heap_empty;
49  h->print = heap_print;
50  h->destroy = destroy_heap;
51 
52  // Initailze datatype based operations req for prop working of heap
53  h->cmpr = prm->cmpr;
54  h->cmpr_idx = prm->cmpr_idx;
55  h->swap_idx = prm->swap_idx;
56  h->copy_idx = prm->copy_idx;
57  h->get_idx = prm->get_idx;
58  h->print_data = prm->print_data;
59 
60  return (t_gen)h;
61 }
62 
63 
70 {
71  t_heap *h = (t_heap*)d;
72 
73  return (h->count >= h->size);
74 }
75 
82 {
83  t_heap *h = (t_heap*)d;
84 
85  return (h->count == 0);
86 }
87 
95 void heapify_up(t_heap *h, int idx)
96 {
97  int parent;
98  e_cmpr cmp_res;
99  // exit condition depending type of heap
100  cmp_res = (h->type == eMAX_HEAP)? eGREAT : eLESS;
101 
102  for (parent = (idx - 1) / 2; idx; parent = (idx - 1) / 2) {
103  // Heap prop satisfied parent </> child for min/max heap
104  // else swap parent and child and go one up
105  if (h->cmpr_idx(h->data, parent, idx) == cmp_res) {
106  break;
107  } else {
108  h->swap_idx(h->data, parent, idx);
109  }
110  idx = parent;
111  }
112 }
113 
120 void heap_insert(t_gen d,t_gen data)
121 {
122  t_heap *h = (t_heap*)d;
123 
124  if (h->count >= h->size) {
125  LOG_WARN("HEAP", "%s: HEAP_FULL\n",h->name);
126  return;
127  }
128 
129  h->copy_idx(h->data, h->count, data);
130  heapify_up(h, h->count);
131  h->count++;
132 }
133 
141 void heapify(t_heap *h, int idx)
142 {
143  int parent,lchild,rchild;
144  e_cmpr cmp_res;
145 
146  // exit condition depending type of heap
147  cmp_res = (h->type != eMAX_HEAP)? eGREAT : eLESS;
148  lchild = rchild = 0;
149  for (parent = idx; rchild <= h->count; ) {
150  lchild = (2 * idx)+1; rchild = (2 * parent) + 2;
151  // Heap prop satisfied parent </> child for min/max heap
152  // else parent and child and go one up
153  if (lchild < h->count && h->cmpr_idx
154  (h->data, parent, lchild) == cmp_res) {
155  parent = lchild;
156  }
157  if (rchild < h->count && h->cmpr_idx
158  (h->data, parent, rchild) == cmp_res) {
159  parent = rchild;
160  }
161 
162  // if parent has changed swap parent with idx
163  if (idx != parent) {
164  h->swap_idx(h->data, parent, idx);
165  idx = parent;
166  } else {
167  break;
168  }
169  }
170 }
171 
178 {
179  t_heap *h = (t_heap*)d;
180  int idx;
181 
182  h->count = h->size;
183  for(idx = h->size/2; idx >= 0; idx--) {
184  heapify(h, idx);
185  }
186 }
187 
188 
195 {
196  t_heap *h = (t_heap*)d;
197  t_gen data = NULL;
198 
199  if (h->count == 0) {
200  LOG_WARN("HEAP", "%s: HEAP EMPTY\n",h->name);
201  return data;
202  }
203 
204  // Root is swapped with last node in heap
205  // temp store root at last heap location
206  h->count--;
207  h->swap_idx(h->data, 0, h->count);
208 
209  // heapify to preserve heap prop
210  heapify(h, 0);
211 
212  //Ref to previous heap root is returned
213  data = h->get_idx(h->data, h->count);
214 
215  return data;
216 }
217 
224 {
225  t_heap *h = (t_heap*)d;
226  int i,n = (h->count-1);
227 
228  // build heap
229  h->build(h);
230 
231  // Move the current root to last
232  // sorted but in traverse in reverse order
233  for(;h->count;) {
234  // Root to be deleted is swapped with last node in heap
235  h->count--;
236  h->swap_idx(h->data, 0, h->count);
237  // heapify to preserve heap prop
238  heapify(h, 0);
239  }
240 }
241 
251 {
252  t_heap *h = (t_heap*)d;
253  t_gen tmp = NULL;
254  e_cmpr cmp_res;
255 
256  if (idx < 0 || idx >= h->count) {
257  LOG_WARN("HEAP", "%s: idx out of bound\n",h->name);
258  return tmp;
259  }
260 
261  // exit condition depending type of heap
262  cmp_res = (h->type != eMAX_HEAP)? eGREAT : eLESS;
263  tmp = h->get_idx(h->data, idx);
264 
265  // If new val '<'/'>' previous val or min/max heap
266  // then heapify up else heapify down
267  if (h->cmpr(tmp, val) == cmp_res) {
268  h->copy_idx(h->data, idx, val);
269  heapify_up(h, idx);
270  }
271  else {
272  h->copy_idx(h->data, idx, val);
273  heapify(h, idx);
274  }
275 
276  return tmp;
277 }
278 
285 {
286  return ((t_heap*)h)->count;
287 }
288 
295 {
296  t_heap *h = (t_heap*)d;
297 
298  free_mem(h);
299 }
300 
301 /* @brief
302  * Util function to get type of heap in string
303  * @param type - heap Type
304  * @return String of heap type
305 */
307 {
308  switch (type) {
309  case eMIN_HEAP:
310  return "MIN_HEAP";
311  case eMAX_HEAP:
312  return "MAX_HEAP";
313  }
314 
315  return "UNDEFINED";
316 }
323 {
324  t_heap *h = (t_heap*)d;
325  int i;
326 
327  printf("%s:%s {count: %d} {size: %d}\n[ ",h->name,
328  get_heaptype_name(h->type), h->count, h->size);
329  for (i = 0; i < h->size; i ++) {
330  h->print_data(h->get_idx(h->data, i));
331  printf(" ");
332  }
333  printf("]\n");
334 }
t_gen heap_extract_root(t_gen d)
Extract the root from heap
Definition: heap.c:194
t_gen heap_update_key(t_gen d, t_gen val, int idx)
heap update value of given node
Definition: heap.c:250
void heapify(t_heap *h, int idx)
Rearrange a heap to maintain the heap property aka heapyfy_down
Definition: heap.c:141
bool heap_full(t_gen d)
To check if heap full
Definition: heap.c:69
void heap_insert(t_gen d, t_gen data)
Insert an element to heap
Definition: heap.c:120
void heapify_up(t_heap *h, int idx)
Preserve heap property on insert by heapifying from bottom to root
Definition: heap.c:95
bool heap_empty(t_gen d)
To check if heap empty
Definition: heap.c:81
char * get_heaptype_name(e_heaptype type)
Definition: heap.c:306
void heap_sort(t_gen d)
Heap sort the data
Definition: heap.c:223
int heap_len(t_gen d)
heap count
Definition: heap.c:284
void heap_build(t_gen d)
Build a heap given a array
Definition: heap.c:177
void heap_print(t_gen d)
heap_print_info
Definition: heap.c:322
void destroy_heap(t_gen d)
Destroy the instance of the heap
Definition: heap.c:294
t_gen create_heap(char *name, t_gen data, int size, e_heaptype htype, t_dparams *prm)
Create an instance of heap
Definition: heap.c:29
Contains declations of heap types, operations and structure.
e_heaptype
Types of heaps.
Definition: heap.h:9
@ eMIN_HEAP
Minimum Heap
Definition: heap.h:10
@ eMAX_HEAP
Maximum Heap.
Definition: heap.h:11
#define LOG_WARN(mod, fmt, args...)
Definition: logger.h:18
#define free_mem(mem_addr)
Definition: memory_manager.h:9
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
data params struct defn
Definition: common.h:18
f_cmpr cmpr
Routine used for comparing two given elems of said type.
Definition: common.h:23
f_get_idx get_idx
Routine used for getting elem in given array index.
Definition: common.h:32
f_swp_idx swap_idx
Routine used for swapring elems in given array indicies.
Definition: common.h:29
f_cpy_idx copy_idx
Routine used for copying elems in given array indicies.
Definition: common.h:30
f_print print_data
Routine used for printing elem data.
Definition: common.h:33
f_cmp_idx cmpr_idx
Routine used for comparing elems in given array indicies.
Definition: common.h:28
Heap struct defn.
Definition: heap.h:18
f_update update
routine to update a key of given heap node
Definition: heap.h:32
f_len len
routine to get heap len
Definition: heap.h:33
f_cmpr cmpr
Definition: heap.h:40
f_ins insert
routine to insert elements in heap
Definition: heap.h:28
f_get_idx get_idx
Definition: heap.h:44
f_vgen sort
routine to heap sort
Definition: heap.h:31
f_print print
routine to print heap info
Definition: heap.h:36
int size
Max Size of heap.
Definition: heap.h:22
char * name
Stack instance name.
Definition: heap.h:20
f_vgen build
routine to heapify
Definition: heap.h:30
f_destroy destroy
routine to destroy
Definition: heap.h:37
f_empty empty
routine to check if heap empty
Definition: heap.h:35
f_swp_idx swap_idx
Definition: heap.h:43
f_cpy_idx copy_idx
Definition: heap.h:42
f_print print_data
Definition: heap.h:45
f_full full
routine to check if heap full
Definition: heap.h:34
e_heaptype type
Stack Type.
Definition: heap.h:23
int count
Total elems present in heap.
Definition: heap.h:21
f_gen extract
routine to extract min/max root element in heap
Definition: heap.h:29
t_gen * data
Ptr to array based heap.
Definition: heap.h:25
f_cmp_idx cmpr_idx
Definition: heap.h:41
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
e_cmpr
Custom Compare function return type.
Definition: typedefs.h:34
@ eGREAT
Definition: typedefs.h:37
@ eLESS
Definition: typedefs.h:35