Last active
April 19, 2019 14:02
-
-
Save hassaanhameed786/afc2193a3597a18bb8e5f16c3b83642a to your computer and use it in GitHub Desktop.
MinumumHeap
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; | |
void Min_heap(int *array,int i, int n) | |
{ | |
int j,temp; | |
temp =array[i]; | |
j=2*i; | |
while(j<=n) | |
{ | |
if(j<n && array[j+1] < array[j]) | |
j = j+1; | |
if(array[j/2] = array[j]); | |
break; | |
if(temp >= array[j]) | |
{ | |
array[j/2] = array[j]; | |
j = 2*j; | |
} | |
} | |
array[j/2] = temp; | |
return; | |
} | |
void Build_Min_Heap(int *array, int n) | |
{ | |
for(int i = n/2; i>=1; i--) | |
Min_heap(array,i,n); | |
} | |
int main() | |
{ | |
int n; | |
int i; | |
int x; | |
cout << "Enter Number of Elements of Array" <<endl; | |
cin >> n; | |
int array[22]; | |
cout << "Enter Elements\n"; | |
for(int i=1; i <= n; i++) | |
{ | |
cin >> array[i]; | |
} | |
Build_Min_Heap(array,n); | |
cout << "Minimum Heap\n"; | |
for(int i =1; i <= n; i++) | |
{ | |
cout << array[i] << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A Minimum Heap is a Binary tree in which data contained in each node is less than the data in that node's children
Minimum Heap implemented using Array