Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- template <class T>
- class Heap
- {
- private:
- T* arr;
- int size;
- int maxSize;
- void FilterDown(int cur)
- {
- int curChild = 2 * cur + 1;
- while (cur < size)
- {
- curChild = ((curChild + 1 < size) && arr[curChild] < arr[curChild + 1]) ? curChild + 1 : curChild;
- if (arr[curChild] <= arr[cur]) break;
- swap(arr[cur], arr[curChild]);
- cur = curChild;
- curChild = 2 * cur + 1;
- }
- }
- void FilterUp(int cur)
- {
- int parent = (cur - 1) / 2;
- while (cur && arr[cur] >= arr[parent])
- {
- swap(arr[cur], arr[parent]);
- cur = parent;
- parent = (cur - 1) / 2;
- }
- }
- void HeapBuild()
- {
- int cur = (size - 2) / 2;
- while (cur >= 0)
- {
- FilterDown(cur);
- cur--;
- }
- }
- public:
- Heap(int _maxSize = 256)
- {
- maxSize = _maxSize;
- size = 0;
- arr = new T[maxSize];
- if (arr == 0) throw "MemoryAllocError";
- }
- Heap(int _size, int _maxSize = 256)
- {
- size = _size;
- maxSize = _maxSize;
- arr = new T[maxSize];
- if (arr == 0) throw "MemoryAllocError";
- }
- Heap(T* _arr, int _size, int _maxSize = 256)
- {
- if (_arr == nullptr) throw "Nullptr";
- size = _size;
- maxSize = _maxSize;
- arr = new T[maxSize];
- if (arr == 0) throw "MemoryAllocError";
- for (int i = 0; i < size; ++i)
- Heap<T>::arr[i] = _arr[i];
- HeapBuild();
- }
- Heap(const Heap<T>& H)
- {
- if (H.arr == nullptr) throw "Nullptr";
- size = H.size;
- maxSize = H.maxSize;
- arr = new T[maxSize];
- if(arr == 0) throw "MemoryAllocError";
- for (int i = 0; i < size; ++i)
- this->arr[i] = H.arr[i];
- HeapBuild();
- }
- void push(const T& key)
- {
- if (!size)
- {
- arr = new T[maxSize];
- if (arr == 0) throw "MemoryAllocError";
- }
- if (size == maxSize)
- {
- T* arrBuff = arr;
- arr = new T[maxSize * 2];
- if (arr == 0) throw "MemoryAllocError";
- for (int i = 0; i < size; ++i)
- arr[i] = arrBuff[i];
- delete[] arrBuff;
- }
- size++;
- arr[size - 1] = key;
- FilterUp(size - 1);
- }
- void pop()
- {
- arr[0] = arr[size - 1];
- size--;
- FilterDown(0);
- }
- T top() const
- {
- return arr[0];
- }
- bool empty() const
- {
- return (size == 0);
- }
- void print() const
- {
- for (int i = 0; i < size; ++i)
- cout << arr[i] << " ";
- cout << '\n';
- }
- ~Heap()
- {
- delete[] arr;
- }
- };
- int main()
- {
- int arr[4] = { 15, 10, 40, 30 };
- Heap<int> H(arr, 4);
- Heap<int> A;
- cout << "H is empty?" << (H.empty() ? " YES\n" : " NO\n");
- cout << "A is empty?" << (A.empty() ? " YES\n" : " NO\n");
- A.push(5);
- A.push(10);
- A.push(50);
- A.push(11);
- A.push(8);
- A.push(52);
- A.push(55);
- A.push(25);
- A.push(22);
- A.push(20);
- H.print();
- A.print();
- cout << "MAX element: " << A.top() << '\n'; A.pop();
- cout << "MAX element: " << A.top() << '\n'; A.pop();
- cout << "MAX element: " << A.top() << '\n'; A.pop();
- A.print();
- Heap<int> B(A);
- cout << "B: "; B.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement