#include <iostream> #include <cstdlib> using namespace std; void swap(int array[] , int i , int j) { array[i] = array[i]+array[j]; array[j] = array[i]-array[j]; array[i] = array[i]-array[j]; } int Parent(int i) { if(i>1) { return i/2; } else { return 0; } } void MaxHeapify(int array[] , int i , int hSize) { int l = 2*i; int r = 2*i+1; int largest; while(l <= hSize) { if(array[l] > array[i]) { largest = l; } else { largest = i; } if(r<=hSize && array[r]>array[largest])//It's r<=hSize other than r<hSize { largest = r; } if(largest != i) { swap(array , i ,largest); i = largest; l = 2*i; r = 2*i+1; } else { break; } } } int HeapMaximum(int array[]) { return array[1]; } int HeapExtractMax(int array[] , int hSize) { if(hSize < 1) { cout << "underflow"; exit(0); } int max = array[1]; array[1] = array[hSize]; hSize--; MaxHeapify(array , 1 , hSize); return max; } void HeapIncreaseKey(int array[] , int i , int key) { if(key < array[i]) { cout << "new key is smaller than current key"; exit(0); } array[i] = key; if(Parent(i) == true) { while(i>1 && array[Parent(i)]<array[i]) { swap(array , i , Parent(i)); i = Parent(i); } } } void HeapDelete(int array[] , int i , int& hSize) { array[i] = array[hSize]; hSize--; int key = array[i]; if(key <= array[Parent(i)]) { MaxHeapify(array , i ,hSize); } else { while(i>1 && array[Parent(i)]<key) { array[i] = array[Parent(i)]; i = Parent(i); } } } int main() { return 0; }