Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Heap {
- static const int SIZE = 100;
- int* h;
- int HeapSize;
- public:
- Heap() {
- h = new int[SIZE];
- HeapSize = 0;
- }
- void addelem(int n) {
- int i = HeapSize, parent = (i - 1)/2;
- h[i] = n;
- HeapSize++;
- while (i > 0 && parent > 0) {
- if (h[i] > h[parent])
- swap(h[i], h[parent]);
- i = parent;
- parent = (i - 1) / 2;
- }
- }
- void outHeap() {
- int i = 0, k = 1;
- while (i < HeapSize) {
- while (i < k && i < HeapSize) {
- cout << h[i] << ' ';
- i++;
- }
- cout << endl;
- k *= 2 + 1;
- }
- }
- void heapify(int i) {
- int left = 2 * i + 1;
- int right = 2 * i + 2;
- if (left < HeapSize && h[i] < h[left]) {
- swap(h[i], h[left]);
- heapify(left);
- }
- if (right < HeapSize && h[i] < h[right]) {
- swap(h[i], h[left]);
- heapify(right);
- }
- }
- int getmax() {
- int x = h[0];
- h[0] = h[--HeapSize];
- heapify(0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement