Question: language is C++ 4) Program Heap Write the code to implement a complete binary heap using an array ( Not a vector ). Code for
language is C++
4) Program Heap
Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap.
Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc
Set array size to 31 possible integers.
Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13
Have a default constructor that initializes the array to zeros..
The data in the heap will be double datatype.
5) Program Template
Convert Program 4 to template, test with integers, double and char
what I got until now (from another help) is this code
#include "stdafx.h" #include #include using namespace std; double getmax(double[], int); void heapsort(double[], int); void max_heapify(double *k, int i, int n) { int j; double temp = k[i]; j = 2 * i; while (j <= n) { if (j < n && k[j + 1] > k[j]) j = j + 1; if (temp > k[j]) break; else if (temp <= k[j]) { k[j / 2] = k[j]; j = 2 * j; } } k[j / 2] = temp; return; } void build_maxheap(double *k, int n) { int i; for (i = n / 2; i >= 1; i--) { max_heapify(k, i, n); } } int main() { int n, i, x; double max; cout << "enter no of elements of array "; cin >> n; double k[20]; for (i = 1; i <= n; i++) { cout << "enter element" << (i) << endl; cin >> k[i]; } build_maxheap(k, n); cout << "Max Heap "; for (i = 1; i <= n; i++) { cout << k[i] << endl; } heapsort(k, n); max = getmax(k, n); cout << " max is " << max; cout << " Sorted Array "; for (i = 1; i <= n; i++) { cout << k[i] << endl; } getch(); } void heapsort(double *k, int n) { int i, temp; for (i = n; i >= 2; i--) { temp = k[i]; k[i] = k[1]; k[1] = temp; max_heapify(k, 1, i - 1); } } double getmax(double *k, int n) { double max; max = 0; for (int i = 1; i <= n; i++) { if (max }
return max; }
please help me fix it and make another code with a templet