-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_heap.cpp
More file actions
87 lines (86 loc) · 1.23 KB
/
max_heap.cpp
File metadata and controls
87 lines (86 loc) · 1.23 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
83
84
85
86
87
#include<iostream>
using namespace std;
void swap(int *a,int* b)
{
int temp=*a;
*a=*b;
*b=temp;
}
class MaxHeap{
int capacity; //total capacity of Heap
int heap_size; //current number of elements in max heap
int *harr; //current top element
public:
MaxHeap(int cap)
{
capacity=cap;
heap_size=0;
harr=new int[cap];
}
void insert(int k);
int getMin()
{
return harr[0];
}
int parent(int i)
{
return (i-1)/2;
}
int right_child(int i)
{
return (2*i)+1;
}
int left_child(int i)
{
return (2*i)-1;
}
void Heapify(int i);
};
void MaxHeap::insert(int k)
{
if(heap_size>=capacity)
{
cout<<"OverFlow";
return;
}
heap_size++;
int i=heap_size-1;
harr[i]=k;
while(i!=0 && harr[parent(i)]<harr[i])
{
swap(&harr[parent(i)],&harr[i]);
i=parent(i);
}
}
void MaxHeap::Heapify(int i)
{
int l=left_child(i);
int r=right_child(i);
int large=i;
while(l<heap_size && harr[l]>harr[large])
{
large=l;
}
while(r<heap_size && harr[r]>harr[l])
{
large=r;
}
if(large!=i)
{
swap(&harr[large],&harr[i]);
Heapify(large);
}
}
int main()
{
MaxHeap mh(10);
mh.insert(2);
mh.insert(1);
mh.insert(1);
mh.insert(0);
mh.insert(5);
mh.Heapify(1);
cout<<mh.getMin()<<"\t";
mh.insert(101);
cout<<mh.getMin()<<"\t";
}