1
1
package me .ramswaroop .arrays ;
2
2
3
- import java . util . Arrays ;
4
- import java . util . NoSuchElementException ;
3
+ import me . ramswaroop . common . MaxHeap ;
4
+ import me . ramswaroop . common . MinHeap ;
5
5
6
6
/**
7
7
* Created by IntelliJ IDEA.
12
12
*/
13
13
public class MedianOfStream {
14
14
15
+ /**
16
+ * @param med
17
+ * @param elem
18
+ * @param maxHeap
19
+ * @param minHeap
20
+ * @return
21
+ */
15
22
public static int getMedianOfStream (int med , int elem , MaxHeap maxHeap , MinHeap minHeap ) {
16
23
17
24
switch (compare (maxHeap .getSize (), minHeap .getSize ())) {
18
- case 0 :
25
+ case 0 : // sizes of maxHeap and minHeap are same
19
26
if (elem < med ) {
20
27
maxHeap .insert (elem );
21
28
med = maxHeap .findMax ();
@@ -24,20 +31,20 @@ public static int getMedianOfStream(int med, int elem, MaxHeap maxHeap, MinHeap
24
31
med = minHeap .findMin ();
25
32
}
26
33
break ;
27
- case 1 :
34
+ case 1 : // size of maxHeap greater than minHeap
28
35
if (elem < med ) {
29
- minHeap .insert (maxHeap .deleteMax ());
36
+ minHeap .insert (maxHeap .extractMax ());
30
37
maxHeap .insert (elem );
31
38
} else {
32
39
minHeap .insert (elem );
33
40
}
34
41
med = (maxHeap .findMax () + minHeap .findMin ()) / 2 ;
35
42
break ;
36
- case -1 :
43
+ case -1 : // size of maxHeap smaller than minHeap
37
44
if (elem < med ) {
38
45
maxHeap .insert (elem );
39
46
} else {
40
- maxHeap .insert (minHeap .deleteMin ());
47
+ maxHeap .insert (minHeap .extractMin ());
41
48
minHeap .insert (elem );
42
49
}
43
50
med = (maxHeap .findMax () + minHeap .findMin ()) / 2 ;
@@ -48,8 +55,10 @@ public static int getMedianOfStream(int med, int elem, MaxHeap maxHeap, MinHeap
48
55
49
56
static void printMedianOfStream (int [] a ) {
50
57
int m = 0 ;
51
- MaxHeap maxHeap = new MaxHeap (12 );
52
- MinHeap minHeap = new MinHeap (12 );
58
+ MaxHeap maxHeap = new MaxHeap (a );
59
+ MinHeap minHeap = new MinHeap (a );
60
+
61
+ // calling in a loop so at to resemble a stream
53
62
for (int i = 0 ; i < a .length ; i ++) {
54
63
m = getMedianOfStream (m , a [i ], maxHeap , minHeap );
55
64
}
@@ -68,250 +77,3 @@ public static void main(String a[]) {
68
77
printMedianOfStream (new int []{5 , 15 , 1 , 3 , 2 , 8 , 7 , 9 , 10 , 6 , 11 , 4 });
69
78
}
70
79
}
71
-
72
- class MinHeap {
73
- /**
74
- * The number of children each node has *
75
- */
76
- private static final int d = 2 ;
77
- private int size ;
78
- private int [] heap ;
79
-
80
- /**
81
- * Constructor *
82
- */
83
- public MinHeap (int capacity ) {
84
- size = 0 ;
85
- heap = new int [capacity + 1 ];
86
- Arrays .fill (heap , -1 );
87
- }
88
-
89
- /**
90
- * Function to check if heap is empty *
91
- */
92
- public boolean isEmpty () {
93
- return size == 0 ;
94
- }
95
-
96
- /**
97
- * Check if heap is full *
98
- */
99
- public boolean isFull () {
100
- return size == heap .length ;
101
- }
102
-
103
- /**
104
- * Clear heap
105
- */
106
- public void makeEmpty () {
107
- size = 0 ;
108
- }
109
-
110
- /**
111
- * Function to get index parent of i *
112
- */
113
- private int parent (int i ) {
114
- return (i - 1 ) / d ;
115
- }
116
-
117
- /**
118
- * Function to get index of k th child of i *
119
- */
120
- private int kthChild (int i , int k ) {
121
- return d * i + k ;
122
- }
123
-
124
- /**
125
- * Function to insert element
126
- */
127
- public void insert (int x ) {
128
- if (isFull ())
129
- throw new NoSuchElementException ("Overflow Exception" );
130
- /** Percolate up **/
131
- heap [size ++] = x ;
132
- heapifyUp (size - 1 );
133
- }
134
-
135
- /**
136
- * Function to find least element *
137
- */
138
- public int findMin () {
139
- if (size == 0 ) return -1 ;
140
- return heap [0 ];
141
- }
142
-
143
- /**
144
- * Function to delete min element *
145
- */
146
- public int deleteMin () {
147
- int keyItem = heap [0 ];
148
- delete (0 );
149
- return keyItem ;
150
- }
151
-
152
- /**
153
- * Function to delete element at an index *
154
- */
155
- public int delete (int ind ) {
156
- if (isEmpty ())
157
- throw new NoSuchElementException ("Underflow Exception" );
158
- int keyItem = heap [ind ];
159
- heap [ind ] = heap [size - 1 ];
160
- size --;
161
- heapifyDown (ind );
162
- return keyItem ;
163
- }
164
-
165
- /**
166
- * Function heapifyUp *
167
- */
168
- private void heapifyUp (int childInd ) {
169
- int tmp = heap [childInd ];
170
- while (childInd > 0 && tmp < heap [parent (childInd )]) {
171
- heap [childInd ] = heap [parent (childInd )];
172
- childInd = parent (childInd );
173
- }
174
- heap [childInd ] = tmp ;
175
- }
176
-
177
- /**
178
- * Function heapifyDown *
179
- */
180
- private void heapifyDown (int ind ) {
181
- int child ;
182
- int tmp = heap [ind ];
183
- while (kthChild (ind , 1 ) < size ) {
184
- child = minChild (ind );
185
- if (heap [child ] < tmp )
186
- heap [ind ] = heap [child ];
187
- else
188
- break ;
189
- ind = child ;
190
- }
191
- heap [ind ] = tmp ;
192
- }
193
-
194
- /**
195
- * Function to get smallest child *
196
- */
197
- private int minChild (int ind ) {
198
- int bestChild = kthChild (ind , 1 );
199
- int k = 2 ;
200
- int pos = kthChild (ind , k );
201
- while ((k <= d ) && (pos < size )) {
202
- if (heap [pos ] < heap [bestChild ])
203
- bestChild = pos ;
204
- pos = kthChild (ind , k ++);
205
- }
206
- return bestChild ;
207
- }
208
-
209
- public int getSize () {
210
- return size ;
211
- }
212
-
213
- /**
214
- * Function to print heap *
215
- */
216
- public void printHeap () {
217
- System .out .print ("\n Heap = " );
218
- for (int i = 0 ; i < size ; i ++)
219
- System .out .print (heap [i ] + " " );
220
- System .out .println ();
221
- }
222
- }
223
-
224
- class MaxHeap {
225
- private int [] heap ;
226
- private int size ;
227
- private int maxsize ;
228
-
229
- private static final int FRONT = 1 ;
230
-
231
- public MaxHeap (int maxsize ) {
232
- this .maxsize = maxsize ;
233
- this .size = 0 ;
234
- heap = new int [this .maxsize + 1 ];
235
- heap [0 ] = Integer .MAX_VALUE ;
236
- }
237
-
238
- public int findMax () {
239
- if (size == 0 ) return -1 ;
240
- return heap [FRONT ];
241
- }
242
-
243
- public int parent (int pos ) {
244
- return pos / 2 ;
245
- }
246
-
247
- private int leftChild (int pos ) {
248
- return (2 * pos );
249
- }
250
-
251
- private int rightChild (int pos ) {
252
- return (2 * pos ) + 1 ;
253
- }
254
-
255
- private boolean isLeaf (int pos ) {
256
- if (pos >= (size / 2 ) && pos <= size ) {
257
- return true ;
258
- }
259
- return false ;
260
- }
261
-
262
- private void swap (int fpos , int spos ) {
263
- int tmp ;
264
- tmp = heap [fpos ];
265
- heap [fpos ] = heap [spos ];
266
- heap [spos ] = tmp ;
267
- }
268
-
269
- private void maxHeapify (int pos ) {
270
- if (!isLeaf (pos )) {
271
- if (heap [pos ] < heap [leftChild (pos )] || heap [pos ] < heap [rightChild (pos )]) {
272
- if (heap [leftChild (pos )] > heap [rightChild (pos )]) {
273
- swap (pos , leftChild (pos ));
274
- maxHeapify (leftChild (pos ));
275
- } else {
276
- swap (pos , rightChild (pos ));
277
- maxHeapify (rightChild (pos ));
278
- }
279
- }
280
- }
281
- }
282
-
283
- public void insert (int element ) {
284
- heap [++size ] = element ;
285
- int current = size ;
286
-
287
- while (heap [current ] > heap [parent (current )]) {
288
- swap (current , parent (current ));
289
- current = parent (current );
290
- }
291
- }
292
-
293
- public int getSize () {
294
- return size ;
295
- }
296
-
297
- public void print () {
298
- for (int i = 1 ; i <= size / 2 ; i ++) {
299
- System .out .print (" PARENT : " + heap [i ] + " LEFT CHILD : " + heap [2 * i ]
300
- + " RIGHT CHILD :" + heap [2 * i + 1 ]);
301
- System .out .println ();
302
- }
303
- }
304
-
305
- public void maxHeap () {
306
- for (int pos = (size / 2 ); pos >= 1 ; pos --) {
307
- maxHeapify (pos );
308
- }
309
- }
310
-
311
- public int deleteMax () {
312
- int popped = heap [FRONT ];
313
- heap [FRONT ] = heap [size --];
314
- maxHeapify (FRONT );
315
- return popped ;
316
- }
317
- }
0 commit comments