Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef struct heap{
- int arr[100];
- int heapSize;
- } HEAP;
- void empty(HEAP &h);
- bool isEmpty(HEAP h);
- void insert(HEAP &h, int val);
- void fixHeapRev(HEAP &h);
- void swap(int &a, int &b);
- void printArr(int* arr, int size);
- int maximum(HEAP h);
- void deleteMax(HEAP &h);
- void fixHeap(HEAP &h);
- int maxVal(int a, int b);
- void main()
- {
- HEAP h;
- empty(h);
- insert(h, 5);
- insert(h, 3);
- insert(h, 8);
- insert(h, 4);
- insert(h, 9);
- insert(h, 2);
- insert(h, 1);
- insert(h, 10);
- insert(h, 7);
- insert(h, 6);
- printArr(h.arr, h.heapSize);
- cout << endl << endl;
- system("pause");
- }
- void empty(HEAP &h)
- {
- h.heapSize = 0;
- }
- bool isEmpty(HEAP h)
- {
- if (h.heapSize == 0)
- return true;
- else
- return false;
- }
- void insert(HEAP &h, int val)
- {
- h.arr[h.heapSize] = val;
- h.heapSize++;
- fixHeapRev (h);
- }
- void swap(int &a, int &b)
- {
- int temp = a;
- a = b;
- b = temp;
- }
- void fixHeapRev(HEAP &h)
- {
- int i = h.heapSize - 1;
- int father = (i - 1) / 2;
- while (i > 0 && h.arr[father] < h.arr[i])
- {
- swap(h.arr[father], h.arr[i]);
- i = father;
- father = (i - 1) / 2;
- }
- }
- void printArr(int* arr, int size)
- {
- for (int i = 0; i < size; i++)
- {
- cout << arr[i] << " ";
- }
- }
- int maximum(HEAP h)
- {
- return h.arr[0];
- }
- void deleteMax(HEAP &h)
- {
- h.arr[0] = h.arr[h.heapSize - 1];
- h.heapSize--;
- fixHeap(h);
- }
- void fixHeap(HEAP &h)
- {
- int i = 0;
- bool flag = true;
- while (2 * i + 1 < h.heapSize && flag == true)
- {
- if (2 * i + 2 < h.heapSize)
- {
- if (h.arr[i] > h.arr[2 * i + 1] && h.arr[i] > h.arr[2 * i + 2])
- flag = false;
- else
- {
- }
- }
- }
- }
- int maxVal(int a, int b)
- {
- if (a >= b)
- return a;
- else
- return b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement