wheelsmanx

mansour heap

Nov 28th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /*
  2. Heap Data Structure
  3. cps 272
  4. 4-20-2016
  5. */
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class HeapItem
  10. {
  11. private:
  12. int m_key;
  13. public:
  14. HeapItem(int k = 0) : m_key(k) {}
  15. void setKey(int k) { m_key = k; }
  16. int getKey() { return m_key; }
  17. };
  18.  
  19. class Heap
  20. {
  21. private:
  22. HeapItem *m_elements; // pointer to dynamic array
  23. int m_numElements;
  24. int m_heapLength;
  25. public:
  26. Heap(int sz = 10);
  27. ~Heap() { delete[] m_elements; }
  28. bool Enqueue(int k);
  29. HeapItem *Dequeue();
  30. void print();
  31. void ReHeapDown(int root, int bottom);
  32. void ReHeapUp(int root, int bottom);
  33. };
  34.  
  35. Heap::Heap(int sz)
  36. {
  37. m_elements = new HeapItem[sz];
  38. m_numElements = 0;
  39. m_heapLength = sz;
  40. }
  41.  
  42. bool Heap::Enqueue(int k) // Add
  43. {
  44. if (m_numElements < m_heapLength)
  45. {
  46. HeapItem *temp = new HeapItem(k);
  47. m_elements[m_numElements] = *temp;
  48. ReHeapUp(0, m_numElements);
  49. m_numElements++;
  50. }
  51. return false;
  52. }
  53.  
  54. HeapItem *Heap::Dequeue()
  55. {
  56. HeapItem *temp = new HeapItem(m_elements[0].getKey());
  57. m_numElements--;
  58. m_elements[0] = m_elements[m_numElements];
  59. ReHeapDown(0, m_numElements - 1);
  60. if (m_numElements == 0)
  61. return nullptr;
  62. else
  63. return temp;
  64. }
  65.  
  66. void Heap::ReHeapDown(int root, int bottom)
  67. {
  68. int leftChild = root * 2 + 1;
  69. int rightChild = root * 2 + 2;
  70. int maxChild;
  71.  
  72. if (leftChild <= bottom)
  73. {
  74. if (leftChild == bottom)
  75. maxChild = leftChild;
  76. else
  77. {
  78. if (m_elements[leftChild].getKey() <=
  79. m_elements[rightChild].getKey())
  80. maxChild = rightChild;
  81. else
  82. maxChild = leftChild;
  83. }
  84. if (m_elements[root].getKey() <
  85. m_elements[maxChild].getKey())
  86. {
  87. swap(m_elements[root], m_elements[maxChild]);
  88. ReHeapDown(maxChild, bottom);
  89. }
  90. }
  91. }
  92.  
  93. void Heap::ReHeapUp(int root, int bottom)
  94. {
  95. int parent;
  96. if (bottom > root)
  97. {
  98. parent = (bottom - 1) / 2;
  99. if (m_elements[parent].getKey() <
  100. m_elements[bottom].getKey())
  101. {
  102. swap(m_elements[parent], m_elements[bottom]);
  103. ReHeapUp(root, parent);
  104. }
  105. }
  106. }
  107. void Heap::print()
  108. {
  109. for (int i = 0; i < m_numElements; i++)
  110. cout << m_elements[i].getKey() << ", ";
  111. }
  112.  
  113. void main()
  114. {
  115. Heap *grades = new Heap(10);
  116. cout << "Building" << endl;
  117. grades->Enqueue(6);
  118. grades->Enqueue(5);
  119. grades->Enqueue(3);
  120. grades->Enqueue(1);
  121. grades->Enqueue(8);
  122. grades->Enqueue(7);
  123. grades->Enqueue(2);
  124. grades->Enqueue(4);
  125.  
  126. cout << "printing" << endl;
  127. grades->print();
  128.  
  129. cout << "sorting" << endl;
  130. HeapItem *temp;
  131. while ((temp = grades->Dequeue()) != nullptr)
  132. {
  133. cout << "Deleting " << temp->getKey() << endl;
  134. delete temp;
  135. cout << "Elements in the heap " << endl;
  136. grades->print();
  137. cout << endl;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment