Created
July 31, 2016 13:55
-
-
Save abrarShariar/9071c1166e43eebe59e73a9a38c73ef2 to your computer and use it in GitHub Desktop.
MAX HEAP CREATION WITH ARRAY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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