Created
July 6, 2019 12:05
-
-
Save manojnaidu619/871301d619b6960702e013a39b50d135 to your computer and use it in GitHub Desktop.
Creation and insertion of Binary min 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){ // 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