Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. typedef struct heap{
  6. int arr[100];
  7. int heapSize;
  8. } HEAP;
  9.  
  10.  
  11. void empty(HEAP &h);
  12. bool isEmpty(HEAP h);
  13. void insert(HEAP &h, int val);
  14. void fixHeapRev(HEAP &h);
  15. void swap(int &a, int &b);
  16. void printArr(int* arr, int size);
  17. int maximum(HEAP h);
  18. void deleteMax(HEAP &h);
  19. void fixHeap(HEAP &h);
  20. int maxVal(int a, int b);
  21.  
  22.  
  23. void main()
  24. {
  25. HEAP h;
  26. empty(h);
  27. insert(h, 5);
  28. insert(h, 3);
  29. insert(h, 8);
  30. insert(h, 4);
  31. insert(h, 9);
  32. insert(h, 2);
  33. insert(h, 1);
  34. insert(h, 10);
  35. insert(h, 7);
  36. insert(h, 6);
  37.  
  38. printArr(h.arr, h.heapSize);
  39.  
  40. cout << endl << endl;
  41. system("pause");
  42. }
  43.  
  44. void empty(HEAP &h)
  45. {
  46. h.heapSize = 0;
  47. }
  48.  
  49. bool isEmpty(HEAP h)
  50. {
  51. if (h.heapSize == 0)
  52. return true;
  53. else
  54. return false;
  55. }
  56.  
  57. void insert(HEAP &h, int val)
  58. {
  59. h.arr[h.heapSize] = val;
  60. h.heapSize++;
  61. fixHeapRev (h);
  62. }
  63.  
  64. void swap(int &a, int &b)
  65. {
  66. int temp = a;
  67. a = b;
  68. b = temp;
  69. }
  70.  
  71. void fixHeapRev(HEAP &h)
  72. {
  73. int i = h.heapSize - 1;
  74. int father = (i - 1) / 2;
  75. while (i > 0 && h.arr[father] < h.arr[i])
  76. {
  77. swap(h.arr[father], h.arr[i]);
  78. i = father;
  79. father = (i - 1) / 2;
  80. }
  81. }
  82.  
  83. void printArr(int* arr, int size)
  84. {
  85. for (int i = 0; i < size; i++)
  86. {
  87. cout << arr[i] << " ";
  88. }
  89. }
  90.  
  91. int maximum(HEAP h)
  92. {
  93. return h.arr[0];
  94. }
  95.  
  96. void deleteMax(HEAP &h)
  97. {
  98. h.arr[0] = h.arr[h.heapSize - 1];
  99. h.heapSize--;
  100. fixHeap(h);
  101. }
  102.  
  103. void fixHeap(HEAP &h)
  104. {
  105. int i = 0;
  106. bool flag = true;
  107. while (2 * i + 1 < h.heapSize && flag == true)
  108. {
  109. if (2 * i + 2 < h.heapSize)
  110. {
  111. if (h.arr[i] > h.arr[2 * i + 1] && h.arr[i] > h.arr[2 * i + 2])
  112. flag = false;
  113. else
  114. {
  115.  
  116. }
  117. }
  118. }
  119. }
  120.  
  121. int maxVal(int a, int b)
  122. {
  123. if (a >= b)
  124. return a;
  125. else
  126. return b;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement