Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int parent (int index);
  5. int leftChild (int index);
  6. int rightChild (int index);
  7. int siftDown (int *heap, int index, int size);
  8. int siftUp (int *heap, int index);
  9. void extractMax(int *heap, int size);
  10. void addElement(int *heap, int currHeapSize, int newElement, const int heapSize);
  11. void deleteElement(int *heap, int currHeapSize, int index);
  12. enum operations {EXTRACT = 1, ADD, DELETE};
  13. int currPyramidSize = 0;
  14. int main () {
  15. int heapSize, numberOfOperations;
  16. int operationType;
  17. int newElement, deleteIndex;
  18. cin >> heapSize;
  19. cin >> numberOfOperations;
  20. int *heap = new int [heapSize];
  21. for (int count = 0; count < numberOfOperations; ++count) {
  22. cin >> operationType;
  23. if (operationType == operations::EXTRACT) {
  24. extractMax(heap, currPyramidSize);
  25. }
  26. else if (operationType == operations::ADD) {
  27. ++currPyramidSize;
  28. cin >> newElement;
  29. addElement(heap, currPyramidSize, newElement, heapSize);
  30. }
  31. else if (operationType == operations ::DELETE) {
  32. cin >> deleteIndex;
  33. deleteElement(heap, currPyramidSize, deleteIndex - 1);
  34. }
  35. }
  36. for (int i = 0; i < currPyramidSize; ++i) {
  37. cout << *(heap + i) << " ";
  38. }
  39. delete []heap;
  40. }
  41. int parent (int index) {
  42. return (index - 1) / 2;
  43. }
  44. int leftChild (int index) {
  45. return 2 * index + 1;
  46. }
  47. int rightChild (int index) {
  48. return 2 * index + 2;
  49. }
  50. int siftDown (int *heap, int index, int size) {
  51. while (leftChild(index) <= size - 1 && (heap[index] < heap[leftChild(index)] ||
  52. (heap[index] < heap[rightChild(index)] && rightChild(index) <= size - 1))) {
  53. int indexMax = (heap[rightChild(index)] > heap[leftChild(index)] && rightChild(index) <= size - 1) ? rightChild(index) : leftChild(index);
  54. swap(heap[index], heap[indexMax]);
  55. index = indexMax;
  56. }
  57. return index + 1;
  58. }
  59. int siftUp (int *heap, int index) {
  60. while (index > 0 && heap[index] > heap[parent(index)]) {
  61. swap(heap[index], heap[parent(index)]);
  62. index = parent(index);
  63. }
  64. return index + 1;
  65. }
  66. void extractMax(int *heap, int size) {
  67. if (size > 1) {
  68. swap(heap[0], heap[size - 1]);
  69. cout << siftDown(heap, 0, size - 1) << " ";
  70. cout << heap[size - 1] << endl;
  71. --currPyramidSize;
  72. }
  73. else if (size == 1) {
  74. cout << 0 << " ";
  75. cout << heap[size - 1] << endl;
  76. --currPyramidSize;
  77. }
  78. else if (size <= 0) {
  79. cout << -1 << endl;
  80. }
  81. }
  82. void addElement(int *heap, int currHeapSize, int newElement, const int heapSize) {
  83. if (currHeapSize <= heapSize) {
  84. heap[currHeapSize - 1] = newElement;
  85. cout << siftUp(heap, currHeapSize - 1) << endl;
  86. }
  87. else if (currHeapSize > heapSize) {
  88. cout << -1 << endl;
  89. --currPyramidSize;
  90. }
  91. }
  92. void deleteElement(int *heap, int currHeapSize, int index) {
  93. if (index >= 0 && index <= currHeapSize - 1) {
  94. cout << heap[index] << endl;
  95. swap(heap[index], heap[currHeapSize - 1]);
  96. siftDown(heap, index , currHeapSize - 1);
  97. siftUp(heap, index);
  98. --currPyramidSize;
  99. }
  100. else if (index < 0 || index > currHeapSize - 1) {
  101. cout << -1 << endl;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement