-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap ideone.cpp
118 lines (117 loc) · 1.87 KB
/
Heap ideone.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<bits/stdc++.h>
using namespace std;
class maxHeap
{
public:
int *harr;
int cap;
int hsize;
maxHeap(int capacity)
{
harr = new int[capacity];
hsize = 0;
cap = capacity;
}
void maxHeapify(int i)
{
int l = left(i);
int r = right(i);
int largest = i;
if(l < hsize && harr[l]> harr[i] )
{
largest = l;
}
if(r < hsize && harr[r]> harr[i] )
{
largest = r;
}
if(largest == i)
return;
swap(harr[largest],harr[i]);
maxHeapify(largest);
}
void swap(int &a,int &b)
{
int temp =a;
a = b;
b = temp;
}
int left(int i)
{
return 2*i + 1;
}
int right(int i)
{
return 2*i+1;
}
int parent(int i)
{
return (i-1)/2;
}
void insertKey(int key)
{
if(hsize == cap)
{
cout<<"heap already filled"<<endl;
return;
}
hsize++;
int i = hsize - 1;
harr[i] = key;
while( i!= 0 && harr[parent(i)]<harr[i])
{
swap(harr[parent(i)],harr[i]);
i = parent(i);
}
}
void increaseKey(int i,int key)
{
harr[i] = key;
while(i!=0 && harr[parent(i)] <parent(i))
{
swap(harr[parent(i)],harr[i]);
i= parent(i);
}
}
int extractMax()
{
if(hsize == 0)
return INT_MIN;
if(hsize == 1)
{
int root = harr[0];
hsize--;
return root;
}
int max = harr[0];
swap(harr[0],harr[hsize-1]);
hsize--;
maxHeapify(0);
return max;
}
void deleteKey(int i)
{
increaseKey(i,INT_MAX);
extractMax();
}
void printHeapArray()
{
for(int i=0;i<hsize;i++)
{
cout<<harr[i]<<" ";
}
}
};
int main()
{
maxHeap m_heap(10);
m_heap.insertKey(11);
m_heap.insertKey(5);
m_heap.insertKey(9);
m_heap.insertKey(3);
m_heap.insertKey(2);
m_heap.insertKey(8);
m_heap.insertKey(7);
m_heap.printHeapArray();
return 0;
}