Guest User

Untitled

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