-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminheap.cpp
More file actions
82 lines (57 loc) · 1.22 KB
/
Copy pathminheap.cpp
File metadata and controls
82 lines (57 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
using namespace std;
void heapify(int arr[],int index,int size)
{
int left = 2*index+1;
int right =left+1;
int min = index;
if(left<=size && arr[left]<arr[min])
min = left;
if(right<=size && arr[right]<arr[min])
min =right;
if(index!=min)
{
int temp;
temp=arr[min];
arr[min]=arr[index];
arr[index]=temp;
heapify(arr,min,size);
}
}
void build_heap(int arr[],int size)
{
int i ;
for(int i=size/2;i>=0;i--)
{
heapify(arr,i,size);
}
cout<<"\nHeap after processing is";
for(int j=0;j<=size;j++)
{
cout<<"\n"<<arr[j]<<endl;
}
}
void deletemin(int arr[],int size)
{
int temp;
temp=arr[size];
arr[size]=arr[0];
arr[0]=temp;
cout<<"\nMinimum element poped out of the heap is"<<arr[size]<<endl;
size =size-1;
heapify(arr,0,size);
}
int main()
{
int n;
cout<<"\nEnter the size of the heap";
cin>>n;
int arr[n];
cout<<"\nEnter the heap elements";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
build_heap(arr,n-1);
return 0;
}