Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Created July 6, 2019 12:05
Show Gist options
  • Save manojnaidu619/871301d619b6960702e013a39b50d135 to your computer and use it in GitHub Desktop.
Save manojnaidu619/871301d619b6960702e013a39b50d135 to your computer and use it in GitHub Desktop.
Creation and insertion of Binary min 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){ // changing '<' to '>' becomes max_heap
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(6);
insert(40);
for(int i=1;i<a.size();i++){
cout << a[i] << " ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment