Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Bottom UP
  6. void heapify(int* tree, int n, int root);
  7. void buildHeap(int* tree, int n);
  8.  
  9.  
  10. void swap(int* a, int* b)
  11. {
  12. int aux = *a;
  13. *a = *b;
  14. *b = aux;
  15. }
  16.  
  17. // Top Down
  18. void heapIncreaseKey(int *tree, int i, int key)
  19. {
  20. if(key >= tree[i])
  21. {
  22. tree[i] = key;
  23. while((i > 1) && (tree[(i/2) - 1] < tree[i]))
  24. {
  25. swap(&tree[i],&tree[(i/2)-1]);
  26. i = (i / 2 ) - 1;
  27. }
  28. }
  29. }
  30.  
  31. void maxHeapInsert(int *tree, int key, int hSize)
  32. {
  33. hSize += 1;
  34. heapIncreaseKey(tree,hSize,key);
  35. }
  36.  
  37. void buildMaxHeap(int *tree, int n)
  38. {
  39. for(int i = 2 ; i < n ; i++)
  40. maxHeapInsert(tree,tree[i],i);
  41. }
  42.  
  43. // Heap Sort
  44. //void swap(int* a, int* b);
  45. void heapSort(int* tree, int n);
  46.  
  47. // Printuri
  48. void printArray(int *a, int n);
  49. void printHeap(int* tree, int n);
  50.  
  51. int main()
  52. {
  53. int tree[] = { 1 , 3 , 5 , 4 , 6 , 13 , 10 , 9 , 8 , 15 , 17 };
  54. int n = sizeof(tree) / sizeof(tree[0]);
  55. buildHeap(tree, n);
  56. printHeap(tree, n);
  57.  
  58. cout << '\n';
  59.  
  60. int arr[] = { 12 , 11 , 13 , 5 , 7 , 6 };
  61. int n2 = sizeof(arr) / sizeof(arr[0]);
  62. heapSort(arr, n2);
  63. printArray(arr,n2);
  64.  
  65. cout << '\n';
  66.  
  67. int tree2[] = { 1 , 3 , 5 , 4 , 6 , 13 , 10 , 9 , 8 , 15 , 17 };
  68. int n3 = sizeof(tree2) / sizeof(tree2[0]);
  69. buildMaxHeap(tree2,n3,1);
  70. printHeap(tree2, n3);
  71.  
  72. cout << '\n';
  73.  
  74. int a = 10;
  75. int b = 5;
  76. swap(a,b);
  77. cout << a << " " << b;
  78. }
  79.  
  80. void heapify(int* tree, int n, int root)
  81. {
  82. int max = root;
  83. int leftChild = 2 * root + 1;
  84. int rightChild = 2 * root + 2;
  85.  
  86. if (leftChild < n && tree[leftChild] > tree[max])
  87. max = leftChild;
  88.  
  89. if (rightChild < n && tree[rightChild] > tree[max])
  90. max = rightChild;
  91.  
  92. if (max != root)
  93. {
  94. swap(&tree[root], &tree[max]);
  95. heapify(tree, n, max);
  96. }
  97. }
  98.  
  99. void heapSort(int* tree, int n)
  100. {
  101. for (int i = n / 2 - 1; i >= 0; i--)
  102. heapify(tree, n, i);
  103. for (int i = n - 1; i >= 0; i--)
  104. {
  105. swap(tree[0], tree[i]);
  106. heapify(tree, i, 0);
  107. }
  108. }
  109.  
  110. void buildHeap(int* tree, int n)
  111. {
  112. int start = (n / 2) - 1;
  113. for (int i = start; i >= 0; i--)
  114. heapify(tree, n, i);
  115. }
  116.  
  117. void printHeap(int *tree, int n)
  118. {
  119. for(int i = 0 ; i < n ; i++)
  120. cout << tree[i] << " ";
  121. }
  122.  
  123. void printArray(int *a, int n)
  124. {
  125. for(int i = 0 ; i < n ; i++)
  126. cout << a[i] << " ";
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement