Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. /**
  2. * @file HeapAPI.c
  3. * @author Justin Brooks
  4. * @email jbrook09@mail.uoguelph.ca
  5. * @SN 0953640
  6. * @date July 2017
  7. * @brief File containing the function for the heap API
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "HeapAPI.h"
  13.  
  14.  
  15. Node *createHeapNode(void *data)
  16. {
  17. if (data == NULL) return NULL;
  18.  
  19. Node * node = malloc(sizeof(Node));
  20. node->data = data;
  21. node->pos = -1;
  22.  
  23. return node;
  24. }
  25.  
  26. Heap *createHeap(size_t initialSize, HEAP_TYPE htype, void (*destroyDataFP)(void *data),void (*printNodeFP)(void *toBePrinted),int (*compareFP)(const void *first, const void *second))
  27. {
  28. Heap *heap = malloc(sizeof(Heap));
  29.  
  30. heap->heap = calloc(initialSize, sizeof(Node *));
  31. heap->size = initialSize;
  32.  
  33. heap->nextPosition = 0;
  34.  
  35. heap->type = htype;
  36.  
  37. heap->destroyData = destroyDataFP;
  38.  
  39. heap->printNode = printNodeFP;
  40.  
  41. heap->compare = compareFP;
  42.  
  43. return heap;
  44. }
  45.  
  46. Node *getParent(Heap *heap, Node *node)
  47. {
  48. int pos = node->pos;
  49. int parentPos = PARENT(pos);
  50.  
  51. if (parentPos > -1 && parentPos < heap->size) {
  52. return heap->heap[parentPos];
  53. }
  54.  
  55. return NULL;
  56. }
  57.  
  58. Node *getLeftChild(Heap *heap, Node *node)
  59. {
  60. int pos = node->pos;
  61. int parentPos = LEFT(pos);
  62.  
  63. if (parentPos > 0 && parentPos < heap->size) {
  64. return heap->heap[parentPos];
  65. }
  66.  
  67. return NULL;
  68. }
  69.  
  70. Node *getRightChild(Heap *heap, Node *node)
  71. {
  72. int pos = node->pos;
  73. int parentPos = RIGHT(pos);
  74.  
  75. if (parentPos > 0 && parentPos < heap->size) {
  76. return heap->heap[parentPos];
  77. }
  78.  
  79. return NULL;
  80. }
  81.  
  82. void insertHeapNode(Heap *heap, void *data)
  83. {
  84. if (data == NULL) return;
  85. if (heap == NULL) return;
  86.  
  87. int nextPosition = heap->nextPosition;
  88.  
  89. if (nextPosition < heap->size) {
  90.  
  91. Node *node = createHeapNode(data);
  92. node->pos = nextPosition;
  93.  
  94. heap->heap[nextPosition] = node;
  95. heap->nextPosition++;
  96.  
  97. reheapify(heap, node);
  98. }
  99. }
  100.  
  101. void swap(Node *a, Node *b)
  102. {
  103. if (a == NULL || b == NULL) return;
  104.  
  105. void *tempData = a->data;
  106. a->data = b->data;
  107. b->data = tempData;
  108. }
  109.  
  110. void changeHeapType(Heap *heap)
  111. {
  112. if (heap == NULL) return;
  113.  
  114. if (heap->type == MAX_HEAP) {
  115. heap->type = MIN_HEAP;
  116. } else {
  117. heap->type = MAX_HEAP;
  118. }
  119.  
  120. sortHeap(heap);
  121. }
  122.  
  123. void sortHeap(Heap *heap)
  124. {
  125.  
  126. if (heap == NULL || heap->nextPosition == 0) return;
  127.  
  128. int i;
  129. int size = heap->size;
  130.  
  131. for (i = PARENT(size); i >= 0; --i) {
  132. Node *tempNode = heap->heap[i];
  133. if (tempNode != NULL) {
  134. heapify(heap, size, tempNode);
  135. }
  136. }
  137. for(i = size - 1; i >= 0; --i) {
  138. if (heap->heap[i] != NULL) {
  139. swap(heap->heap[0], heap->heap[i]);
  140. heapify(heap, i, heap->heap[0]);
  141. }
  142. }
  143. }
  144.  
  145. void heapify(Heap *heap, int size, Node *root)
  146. {
  147. int (*compareFP)(const void *first, const void *second) = heap->compare;
  148.  
  149. Node *left = getLeftChild(heap, root);
  150. Node *right = getRightChild(heap, root);
  151.  
  152. if (heap->type == MIN_HEAP) {
  153.  
  154. Node *largest = root;
  155.  
  156. if (left != NULL && left->data != NULL) {
  157. if (left->pos < size && compareFP(left->data, largest->data) > 0) {
  158. largest = left;
  159. }
  160. }
  161.  
  162. if (right != NULL && right->data != NULL) {
  163. if (right->pos < size && compareFP(right->data, largest->data) > 0) {
  164. largest = right;
  165. }
  166. }
  167.  
  168. if (largest != root) {
  169. swap(root, largest);
  170. heapify(heap, size, largest);
  171. }
  172.  
  173. } else {
  174.  
  175. Node *smallest = root;
  176.  
  177. if (left != NULL && left->data != NULL) {
  178. if (left->pos < size && compareFP(smallest->data, left->data) > 0) {
  179. smallest = left;
  180. }
  181. }
  182.  
  183. if (right != NULL && right->data != NULL) {
  184. if (right->pos < size && compareFP(smallest->data, right->data) > 0) {
  185. smallest = right;
  186. }
  187. }
  188.  
  189. if (smallest != root) {
  190. swap(root, smallest);
  191. heapify(heap, size, smallest);
  192. }
  193. }
  194. }
  195.  
  196. void deleteHeap(Heap *heap)
  197. {
  198. int i, size = heap->size;
  199. void (*destroyDataFP)(void *data) = heap->destroyData;
  200.  
  201. for(i = 0; i < size; i++) {
  202. Node *node = heap->heap[i];
  203. if (destroyDataFP != NULL) destroyDataFP(node->data);
  204. free(node);
  205. }
  206.  
  207. free(heap->heap);
  208. free(heap);
  209. }
  210.  
  211. void deleteMinOrMax(Heap *heap)
  212. {
  213. if (heap == NULL) return;
  214.  
  215. heap->nextPosition --;
  216.  
  217. void (*destroyDataFP)(void *data) = heap->destroyData;
  218. int (*compareFP)(const void *first, const void *second) = heap->compare;
  219.  
  220. if (heap->nextPosition == 0) {
  221.  
  222. if (destroyDataFP != NULL) destroyDataFP(heap->heap[0]->data);
  223. free(heap->heap[0]);
  224. heap->heap[heap->nextPosition] = NULL;
  225. return;
  226. }
  227.  
  228. Node *first = heap->heap[0];
  229. Node *last = heap->heap[heap->nextPosition];
  230.  
  231. swap(first, last);
  232.  
  233. if (destroyDataFP != NULL) destroyDataFP(last->data);
  234.  
  235. free(last);
  236. heap->heap[heap->nextPosition] = NULL;
  237.  
  238. if (heap->type == MIN_HEAP) {
  239. for(;;) {
  240.  
  241. Node *leftChild = getLeftChild(heap, first);
  242. Node *rightChild = getRightChild(heap, first);
  243.  
  244. if (leftChild != NULL
  245. && rightChild != NULL
  246. && compareFP(first->data, leftChild->data) > 0
  247. && compareFP(first->data, rightChild->data) > 0) {
  248.  
  249. if (compareFP(rightChild->data, leftChild->data) > 0) {
  250.  
  251. swap(leftChild, first);
  252. first = leftChild;
  253.  
  254. } else {
  255. swap(rightChild, first);
  256. first = rightChild;
  257. }
  258.  
  259.  
  260. } else if (leftChild != NULL && compareFP(first->data, leftChild->data) > 0) {
  261.  
  262. swap(leftChild, first);
  263. first = leftChild;
  264.  
  265. } else if (rightChild != NULL && compareFP(first->data, rightChild->data) > 0) {
  266.  
  267. swap(rightChild, first);
  268. first = rightChild;
  269.  
  270. } else {
  271. break;
  272. }
  273. }
  274. } else {
  275. for(;;) {
  276.  
  277. Node *leftChild = getLeftChild(heap, first);
  278. Node *rightChild = getRightChild(heap, first);
  279.  
  280.  
  281.  
  282. if (leftChild != NULL
  283. && rightChild != NULL
  284. && compareFP(first->data, leftChild->data) < 0
  285. && compareFP(first->data, rightChild->data) < 0) {
  286.  
  287. if (compareFP(rightChild->data, leftChild->data) < 0) {
  288. swap(leftChild, first);
  289. first = leftChild;
  290. } else {
  291. swap(rightChild, first);
  292. first = rightChild;
  293. }
  294.  
  295. } else if (leftChild != NULL && compareFP(first->data, leftChild->data) < 0) {
  296.  
  297. swap(leftChild, first);
  298. first = leftChild;
  299.  
  300. } else if (rightChild != NULL && compareFP(first->data, rightChild->data) < 0) {
  301.  
  302. swap(rightChild, first);
  303. first = rightChild;
  304.  
  305. } else {
  306. break;
  307. }
  308. }
  309. }
  310.  
  311. }
  312.  
  313. void *getMinOrMax(Heap *heap)
  314. {
  315. if (heap == NULL) return NULL;
  316. if (heap->size == 0) return NULL;
  317. if (heap->nextPosition == 0) return NULL;
  318.  
  319. Node *node = heap->heap[0];
  320. if (node == NULL) return NULL;
  321.  
  322. void (*printNodeFP)(void *toBePrinted) = heap->printNode;
  323.  
  324. if (heap->printNode != NULL) printNodeFP(node->data);
  325.  
  326. return node->data;
  327. }
  328.  
  329. void reheapify(Heap *heap, Node *node)
  330. {
  331.  
  332. if (heap == NULL) return;
  333. if (node == NULL) return;
  334.  
  335. int (*compareFP)(const void *first, const void *second) = heap->compare;
  336.  
  337. Node *parent = getParent(heap, node);
  338.  
  339. if (heap->type == MIN_HEAP) {
  340.  
  341. while (compareFP(node->data, parent->data) < 0) {
  342.  
  343. if (parent->pos == node->pos) break;
  344.  
  345. swap(node, parent);
  346.  
  347. node = parent;
  348. parent = getParent(heap, node);
  349. }
  350.  
  351. } else {
  352.  
  353. while (compareFP(node->data, parent->data) > 0) {
  354.  
  355. if (parent->pos == node->pos) break;
  356.  
  357. swap(node, parent);
  358.  
  359. node = parent;
  360. parent = getParent(heap, node);
  361. }
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement