|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <climits> |
| 4 | + |
| 5 | +class MaxHeap |
| 6 | +{ |
| 7 | +public: |
| 8 | + MaxHeap(std::vector<int> &v) { |
| 9 | + _size = v.size(); |
| 10 | + _heap = v; |
| 11 | + for (int i = _size / 2 +1 ; i > -1 ; --i) |
| 12 | + SiftDown(i); |
| 13 | + _maxSize = _heap.capacity(); |
| 14 | + } |
| 15 | + ~MaxHeap() {} |
| 16 | + |
| 17 | + int Parent(int i) { return (i - 1) / 2; } |
| 18 | + int LeftChild(int i) { return 2 * i + 1; } |
| 19 | + int RightChild(int i) { return 2 * i + 2; } |
| 20 | + int size() { return _size; } |
| 21 | + int maxSize() { return _maxSize; } |
| 22 | + |
| 23 | + void SiftUp(int i) { |
| 24 | + while (i > 0 && _heap[Parent(i)] < _heap[i]) |
| 25 | + { |
| 26 | + std::swap(_heap[Parent(i)], _heap[i]); |
| 27 | + i = Parent(i); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + void SiftDown(int i) { |
| 32 | + int maxIndex = i; |
| 33 | + int l = LeftChild(i); |
| 34 | + |
| 35 | + if (l < _size && _heap[l] > _heap[maxIndex]) |
| 36 | + maxIndex = l; |
| 37 | + |
| 38 | + int r = RightChild(i); |
| 39 | + if (r < _size && _heap[r] > _heap[maxIndex]) |
| 40 | + maxIndex = r; |
| 41 | + |
| 42 | + if (i != maxIndex) |
| 43 | + { |
| 44 | + std::swap(_heap[i], _heap[maxIndex]); |
| 45 | + SiftDown(maxIndex); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + void Insert(int p) { |
| 50 | + if (_size == _maxSize) { |
| 51 | + std::cout << "Max size!"; |
| 52 | + return; |
| 53 | + } |
| 54 | + ++_size; |
| 55 | + _heap[_size - 1] = p; |
| 56 | + SiftUp(_size -1); |
| 57 | + } |
| 58 | + |
| 59 | + int ExtractMax() { |
| 60 | + int result = _heap[0]; |
| 61 | + _heap[0] = _heap[_size -1]; |
| 62 | + --_size; |
| 63 | + SiftDown(0); |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + void Remove(int i) { |
| 68 | + _heap[i] = INT_MAX; |
| 69 | + SiftUp(i); |
| 70 | + ExtractMax(); |
| 71 | + } |
| 72 | + |
| 73 | + void ChangePriority(int i, int p) { |
| 74 | + int oldp = _heap[i]; |
| 75 | + _heap[i] = p; |
| 76 | + if (p > oldp) |
| 77 | + SiftUp(i); |
| 78 | + else |
| 79 | + SiftDown(i); |
| 80 | + } |
| 81 | + |
| 82 | + std::vector<int> const & heap() { return _heap; } |
| 83 | + |
| 84 | +private: |
| 85 | + std::vector<int> _heap; |
| 86 | + int _size; |
| 87 | + int _maxSize; |
| 88 | + |
| 89 | +}; |
| 90 | + |
| 91 | + |
| 92 | +int main(void) { |
| 93 | + |
| 94 | + std::vector<int> v = {7, 11, 29, 12, 42, 18, 13, 18, 14}; |
| 95 | + for (int i = 0; i < v.size(); ++i) |
| 96 | + std::cout << v[i] << " "; |
| 97 | + std::cout << std::endl; |
| 98 | + |
| 99 | + MaxHeap h(v); |
| 100 | + |
| 101 | + for (int i = 0; i < h.size(); ++i) |
| 102 | + std::cout << h.heap()[i] << " "; |
| 103 | + std::cout << std::endl; |
| 104 | + |
| 105 | + |
| 106 | + std::cout << "\t\t\t" <<h.heap()[0] << std::endl; |
| 107 | + std::cout << "\t\t" << h.heap()[h.LeftChild(0)] << "\t\t" << h.heap()[h.RightChild(0)] << std::endl; |
| 108 | + std::cout << "\t"<< h.heap()[h.LeftChild(h.LeftChild(0))] << "\t" << h.heap()[h.RightChild(h.LeftChild(0))] << "\t\t" << |
| 109 | + h.heap()[h.LeftChild(h.RightChild(0))] << "\t" << h.heap()[h.RightChild(h.RightChild(0))] << std::endl; |
| 110 | + std::cout << h.heap()[h.LeftChild(h.LeftChild(h.LeftChild(0)))] << "\t" << h.heap()[h.RightChild(h.LeftChild(h.LeftChild(0)))] << std::endl<< std::endl; |
| 111 | + |
| 112 | + h.ExtractMax(); |
| 113 | + |
| 114 | +for (int i = 0; i < h.size(); ++i) |
| 115 | + std::cout << h.heap()[i] << " "; |
| 116 | + std::cout << std::endl; |
| 117 | + std::cout << "\t\t\t" <<h.heap()[0] << std::endl; |
| 118 | + std::cout << "\t\t" << h.heap()[h.LeftChild(0)] << "\t\t" << h.heap()[h.RightChild(0)] << std::endl; |
| 119 | + std::cout << "\t"<< h.heap()[h.LeftChild(h.LeftChild(0))] << "\t" << h.heap()[h.RightChild(h.LeftChild(0))] << "\t\t" << |
| 120 | + h.heap()[h.LeftChild(h.RightChild(0))] << "\t" << h.heap()[h.RightChild(h.RightChild(0))] << std::endl; |
| 121 | + std::cout << h.heap()[h.LeftChild(h.LeftChild(h.LeftChild(0)))] << std::endl<< std::endl; |
| 122 | + |
| 123 | + |
| 124 | + h.Insert(58); |
| 125 | + |
| 126 | + for (int i = 0; i < h.size(); ++i) |
| 127 | + std::cout << h.heap()[i] << " "; |
| 128 | + std::cout << std::endl; |
| 129 | + |
| 130 | + std::cout << "\t\t\t" <<h.heap()[0] << std::endl; |
| 131 | + std::cout << "\t\t" << h.heap()[h.LeftChild(0)] << "\t\t" << h.heap()[h.RightChild(0)] << std::endl; |
| 132 | + std::cout << "\t"<< h.heap()[h.LeftChild(h.LeftChild(0))] << "\t" << h.heap()[h.RightChild(h.LeftChild(0))] << "\t\t" << |
| 133 | + h.heap()[h.LeftChild(h.RightChild(0))] << "\t" << h.heap()[h.RightChild(h.RightChild(0))] << std::endl; |
| 134 | + std::cout << h.heap()[h.LeftChild(h.LeftChild(h.LeftChild(0)))] << "\t" << h.heap()[h.RightChild(h.LeftChild(h.LeftChild(0)))] << std::endl<< std::endl; |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments