Skip to content

Instantly share code, notes, and snippets.

@abrarShariar
Created July 31, 2016 13:55
Show Gist options
  • Select an option

  • Save abrarShariar/9071c1166e43eebe59e73a9a38c73ef2 to your computer and use it in GitHub Desktop.

Select an option

Save abrarShariar/9071c1166e43eebe59e73a9a38c73ef2 to your computer and use it in GitHub Desktop.
MAX HEAP CREATION WITH ARRAY
#include<iostream>
using namespace std;
#define LIMIT 5
int maxHeap[LIMIT];
/*
MAX HEAP CREATION
*/
void insertItem(int,int);
void printMaxHeap();
int main(){
//initialize heap
//int root=0;
int item;
for(int i=0;i<LIMIT;i++){
cout<<"INPUT["<<i<<"]: ";
cin>>item;
insertItem(item,i);
}
printMaxHeap();
}
void printMaxHeap(){
for(int i=0;i<LIMIT;i++){
cout<<maxHeap[i]<<endl;
}
}
void insertItem(int item,int root){
if(root<=0){
maxHeap[root]=item;
return;
}
int parent=(root-1)/2;
if(maxHeap[parent]>=item){
maxHeap[root]=item;
return;
}else{
//recursive insertion
maxHeap[root]=maxHeap[parent];
insertItem(item,parent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment