Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #include <vector>
  2.  
  3. using namespace std;
  4.  
  5. #define db(a) cout << #a << " = " << a << endl
  6. #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl;
  7.  
  8. template<typename T> class MyHeap {
  9. protected:
  10. int capacity;
  11. int size;
  12. vector<T> items;
  13.  
  14. virtual void heapifyUp() {}
  15. virtual void heapifyDown() {}
  16.  
  17. bool hasParent(int index) {
  18. return (index - 1) / 2 >= 0;
  19. }
  20. bool hasLeftValue(int index) {
  21. return 2 * index + 1 < size;
  22. }
  23. bool hasRightValue(int index) {
  24. return 2 * index + 2 < size;
  25. }
  26. int getParent(int index) {
  27. return items[(index - 1) / 2];
  28. }
  29. int getCurrent(int index) {
  30. return items[index];
  31. }
  32. int getParentIndex(int index) {
  33. return (index - 1) / 2;
  34. }
  35. void swap(int first, int last) {
  36. T temp = items[first];
  37. items[first] = items[last];
  38. items[last] = temp;
  39. }
  40. int getLeftValue(int index) {
  41. return items[2 * index + 1];
  42. }
  43. int getRightValue(int index) {
  44. return items[2 * index + 2];
  45. }
  46. int getLeftIndex(int index) {
  47. return 2 * index + 1;
  48. }
  49. int getRightIndex(int index) {
  50. return 2 * index + 2;
  51. }
  52. public:
  53. MyHeap() {
  54. capacity = 0;
  55. size = 0;
  56. }
  57. virtual void saludar() = 0;
  58. void add(T item) {
  59. items.push_back(item);
  60. size++;
  61. heapifyUp();
  62. }
  63. T poll() {
  64. T temp = items[0];
  65. items[0] = items[size - 1];
  66. size--;
  67. heapifyDown();
  68. return temp;
  69. }
  70. };
  71.  
  72. template<typename T>
  73. class MaxHeap : public MyHeap<T> {
  74. public:
  75. MaxHeap() : MyHeap<T>() { }
  76. void saludar() {
  77. cout << "implementado" << endl;
  78. }
  79. void heapifyUp() {
  80. int curindex = this->size - 1, parindex;
  81. while (this->hasParent(curindex) && this->getParent(curindex) < this->getCurrent(curindex)) {
  82. parindex = this->getParentIndex(curindex);
  83. this->swap(parindex, curindex);
  84. curindex = parindex;
  85. }
  86. }
  87. void heapifyDown() {
  88. int curindex = 0, maxindex;
  89. while(this->hasLeftValue(curindex)) {
  90. maxindex = this->getLeftValue(curindex) > this->getCurrent(curindex)
  91. ? this->getLeftIndex(curindex) : curindex;
  92. if (this->hasRightValue(curindex) &&
  93. this->getRightValue(curindex) > this->getCurrent(maxindex)) {
  94. maxindex = this->getRightIndex(curindex);
  95. }
  96. if (curindex != maxindex) {
  97. this->swap(curindex, maxindex);
  98. curindex = maxindex;
  99. }
  100. else break;
  101. }
  102. }
  103. };
  104. template<typename T>
  105. class MinHeap : public MyHeap<T> {
  106. public:
  107. MinHeap() : MyHeap<T>() { }
  108. void saludar() {
  109. cout << "implementado en minheap" << endl;
  110. }
  111. void heapifyUp() {
  112. int curindex = this->size - 1, parindex;
  113. while (this->hasParent(curindex) && this->getParent(curindex) > this->getCurrent(curindex)) {
  114. parindex = this->getParentIndex(curindex);
  115. this->swap(parindex, curindex);
  116. curindex = parindex;
  117. }
  118. }
  119. void heapifyDown() {
  120. int curindex = 0, minindex;
  121. while(this->hasLeftValue(curindex)) {
  122. minindex = this->getLeftValue(curindex) < this->getCurrent(curindex)
  123. ? this->getLeftIndex(curindex) : curindex;
  124. if (this->hasRightValue(curindex) &&
  125. this->getRightValue(curindex) < this->getCurrent(minindex)) {
  126. minindex = this->getRightIndex(curindex);
  127. }
  128. if (curindex != minindex) {
  129. this->swap(curindex, minindex);
  130. curindex = minindex;
  131. }
  132. else break;
  133. }
  134. }
  135. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement