Last active
July 6, 2019 12:05
-
-
Save manojnaidu619/0cdaf844d762fea743c19e839be03caf to your computer and use it in GitHub Desktop.
Creation and insertion into Binary max heap
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> | |
#include <vector> | |
using namespace std; | |
vector <int> a = {0}; | |
void insert(int ele){ | |
int temp; | |
a.push_back(ele); | |
long last = a.size()-1; | |
while(a[last] > a[last/2] && last > 1){ | |
temp = a[last]; | |
a[last] = a[last/2]; | |
a[last/2] = temp; | |
last = last/2; | |
} | |
} | |
int main(){ | |
insert(30); | |
insert(20); | |
insert(15); | |
insert(5); | |
insert(10); | |
insert(12); | |
insert(6); | |
insert(40); | |
for(int i=1;i<a.size();i++){ | |
cout << a[i] << " "; // Use Treevis command line tool to visualize the tree | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment