#include #include #include #include using namespace std; #define LEFT(i) (2*i + 1) #define RIGHT(i) (2*i +2) #define PARENT(i) ((i-1)/2) #define MAXDEPTH(n) ((int) (log2(n) + 1)) #define DEPTH(i) ((int) (log2(i+1) + 1)) #define FIRSTLEAF(d) ((int) pow(2, d-1)) //define a heapify function pointer typedef void (*heapifyFunction)(int*, int, int); //methods for the heap void printTree(int ar[], int n); void swap(int ar[], int i, int j); void minHeapify(int *ar, int n, int i); void maxHeapify(int *ar, int n, int i); void minMaxHeapify(int *ar, int n, int i); void heapSort(int ar[], int n, heapifyFunction heapify); void buildHeap(int ar[], int n, heapifyFunction heapify); int main(void) { int *h; int n; //seed the prng srand(time(0)); //get the size cout << "How big of a heap do you want?" ; cin >> n; //create a random heap h = new int[n]; for(int i=0; i=n) l = i; if(r >= n) r = i; //heap property is already met if(ar[i] <= ar[l] && ar[i] <= ar[r]) return; //find out if l is the smallest if(ar[l] < ar[r]) { //left is the smallest, shift i down swap(ar, l, i); minHeapify(ar, n, l); } else { //right is the smallest shift i down swap(ar, r, i); minHeapify(ar, n, r); } } void maxHeapify(int *ar, int n, int i) { int l, r; l = LEFT(i); r = RIGHT(i); //make sure we don' go past the end of the array if(l >=n) l = i; if(r >= n) r = i; //heap property is already met if(ar[i] >= ar[l] && ar[i] >= ar[r]) return; //find out if l is the largest if(ar[l] > ar[r]) { //left is the largest, shift i down swap(ar, l, i); maxHeapify(ar, n, l); } else { //right is the largest shift i down swap(ar, r, i); maxHeapify(ar, n, r); } } void minMaxHeapify(int *ar, int n, int i) { int d = DEPTH(i); int l, r; l = LEFT(i); r = RIGHT(i); //make sure we don't go past the end of the array if(l >=n) l = i; if(r >= n) r = i; if(d % 2) { //odd row //heap property is already met if(ar[i] <= ar[l] && ar[i] <= ar[r]) return; //find out if l is the smallest if(ar[l] < ar[r]) { //left is the smallest, shift i down swap(ar, l, i); minMaxHeapify(ar, n, l); } else { //right is the smallest shift i down swap(ar, r, i); minMaxHeapify(ar, n, r); } } else { //even row //heap property is already met if(ar[i] >= ar[l] && ar[i] >= ar[r]) return; //find out if l is the smallest if(ar[l] > ar[r]) { //left is the smallest, shift i down swap(ar, l, i); minMaxHeapify(ar, n, l); } else { //right is the smallest shift i down swap(ar, r, i); minMaxHeapify(ar, n, r); } } } void buildHeap(int ar[], int n, heapifyFunction heapify) { int d = MAXDEPTH(n); int fl = FIRSTLEAF(d); //start at the bottom of the heap, and build toward the root for(int i = fl; i>=0; i--) { (*heapify)(ar, n, i); } } //sort ar using heaps void heapSort(int ar[], int n, heapifyFunction heapify) { int i; //sweep over the array, heap as we go for(i=0; i