1
+ #include < iostream>
2
+ #include < vector>
3
+
4
+ using namespace std ;
5
+
6
+ class EmptyHeapException : exception{};
7
+
8
+ class MaxHeap
9
+ {
10
+ private:
11
+ vector<int > arr;
12
+
13
+ public:
14
+ int top;
15
+ MaxHeap ();
16
+ MaxHeap (int data);
17
+
18
+ void push (int data);
19
+ void pop () throw(EmptyHeapException);
20
+
21
+ bool empty ();
22
+ int getMaxChildIndex (int , int );
23
+ void printHeap ();
24
+ };
25
+
26
+ MaxHeap::MaxHeap ()
27
+ {
28
+ this ->top = -999 ;
29
+ this ->arr .push_back (-999 );
30
+ }
31
+
32
+ MaxHeap::MaxHeap (int data)
33
+ {
34
+ this ->arr .push_back (0 );
35
+ this ->arr .push_back (data);
36
+ this ->top = data;
37
+ }
38
+
39
+ bool MaxHeap::empty ()
40
+ {
41
+ if (this ->arr .size () == 1 )
42
+ return true ;
43
+ else
44
+ return false ;
45
+ }
46
+
47
+ void MaxHeap::push (int data)
48
+ {
49
+ this ->arr .push_back (data);
50
+ int i, parent;
51
+ i=arr.size ()-1 ;
52
+ parent = i/2 ;
53
+
54
+ while (i>1 && arr[parent] < arr[i]){
55
+
56
+ swap (arr[parent], arr[i]);
57
+ i = parent;
58
+ parent = i/2 ;
59
+ }
60
+
61
+ this ->top = arr[1 ];
62
+ }
63
+
64
+ void MaxHeap::pop () throw(EmptyHeapException)
65
+ {
66
+ int i, parentIndex, leftIndex, rightIndex, maxChildIndex;
67
+
68
+ try {
69
+ if (this ->empty ()){
70
+ throw EmptyHeapException ();
71
+ }
72
+
73
+ this ->arr [1 ] = this ->arr [this ->arr .size ()-1 ];
74
+ this ->arr .pop_back ();
75
+
76
+ parentIndex = 1 ;
77
+ leftIndex = (2 *parentIndex > this ->arr .size ())? 0 : 2 *parentIndex;
78
+ rightIndex = (2 *parentIndex +1 >this ->arr .size ())? 0 : 2 *parentIndex+1 ;
79
+
80
+ maxChildIndex = getMaxChildIndex (leftIndex, rightIndex);
81
+
82
+ while (this ->arr [parentIndex] < this ->arr [maxChildIndex] && (leftIndex!=0 || rightIndex!=0 )){
83
+
84
+ swap (this ->arr [parentIndex], this ->arr [maxChildIndex]);
85
+
86
+ parentIndex = maxChildIndex;
87
+ leftIndex = (2 *parentIndex > this ->arr .size ())? 0 : 2 *parentIndex;
88
+ rightIndex = (2 *parentIndex+1 > this ->arr .size ())? 0 : 2 *parentIndex+1 ;
89
+
90
+ maxChildIndex = getMaxChildIndex (leftIndex, rightIndex);
91
+ }
92
+ this ->top = this ->arr [1 ];
93
+ }
94
+ catch (EmptyHeapException e){
95
+ cout << " \n\t\t /////Deletion cannot be perfomed on empty heap/////" << endl;
96
+ }
97
+ }
98
+
99
+ int MaxHeap::getMaxChildIndex (int left, int right)
100
+ {
101
+ return (this ->arr [left] > this ->arr [right])? left: right;
102
+ }
103
+
104
+ void swap (int *p, int *q)
105
+ {
106
+ int temp = *p;
107
+ *p = *q;
108
+ *q = temp;
109
+ }
110
+
111
+ void MaxHeap::printHeap ()
112
+ {
113
+ cout << endl;
114
+ for (int i=1 ; i<this ->arr .size (); i++){
115
+ cout << this ->arr [i] << " " ;
116
+ }
117
+ cout << endl;
118
+ }
119
+
120
+ int main ()
121
+ {
122
+ MaxHeap myHeap;
123
+ myHeap.push (10 );
124
+ myHeap.push (40 );
125
+ myHeap.push (20 );
126
+ myHeap.push (30 );
127
+ myHeap.printHeap ();
128
+ myHeap.push (50 );
129
+ myHeap.push (32 );
130
+ myHeap.push (70 );
131
+
132
+ myHeap.printHeap ();
133
+
134
+ cout << endl << myHeap.top << endl;
135
+
136
+ myHeap.pop ();
137
+
138
+ cout << myHeap.top << endl;
139
+ myHeap.pop ();
140
+ cout << myHeap.top << endl;
141
+ myHeap.pop ();
142
+ cout << myHeap.top << endl;
143
+ myHeap.pop ();
144
+ cout << myHeap.top << endl;
145
+ myHeap.pop ();
146
+ cout << myHeap.top << endl;
147
+ myHeap.pop ();
148
+ cout << myHeap.top << endl;
149
+ myHeap.pop ();
150
+ myHeap.pop ();
151
+
152
+ return 0 ;
153
+ }
0 commit comments