Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active July 6, 2019 12:05
Show Gist options
  • Save manojnaidu619/0cdaf844d762fea743c19e839be03caf to your computer and use it in GitHub Desktop.
Save manojnaidu619/0cdaf844d762fea743c19e839be03caf to your computer and use it in GitHub Desktop.
Creation and insertion into Binary max heap
#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