Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 348ccec

Browse files
author
Ram swaroop
committed
MaxHeap and MinHeap code changes + added new methods
1 parent 91c92ac commit 348ccec

File tree

7 files changed

+211
-306
lines changed

7 files changed

+211
-306
lines changed

src/me/ramswaroop/arrays/KLargestElements.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ public static int[] getKLargestElements(int[] a, int k) {
3636

3737
int[] kElements = Arrays.copyOfRange(a, 0, k);
3838

39-
MinHeap.buildMinHeap(kElements);
39+
MinHeap minHeap = new MinHeap(kElements);
40+
minHeap.buildMinHeap();
4041

4142
for (int i = k; i < a.length; i++) {
42-
if (a[i] > kElements[0]) {
43-
kElements[0] = a[i];
44-
MinHeap.buildMinHeap(kElements);
43+
if (a[i] > minHeap.findMin()) {
44+
minHeap.extractMin();
45+
minHeap.insert(a[i]);
4546
}
4647
}
4748

48-
return kElements;
49+
return minHeap.getHeap();
4950
}
5051

5152
public static void main(String a[]) {

src/me/ramswaroop/arrays/KthLargestElement.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ public static int getKthLargestElementNaive(int[] a, int k) {
4040
* @return
4141
*/
4242
public static int getKthLargestElement(int[] a, int k) {
43+
MaxHeap maxHeap = new MaxHeap(a);
44+
maxHeap.buildMaxHeap();
4345
while (true) {
44-
MaxHeap.buildMaxHeap(a);
4546
if (k == 1) break;
46-
47-
swap(a, 0, a.length - 1);
48-
a = Arrays.copyOfRange(a, 0, a.length - 1);
47+
48+
maxHeap.extractMax();
4949
k--;
5050
}
5151

52-
return a[0];
52+
return maxHeap.findMax();
5353
}
5454

5555
private static void swap(int[] a, int firstIndex, int secondIndex) {

src/me/ramswaroop/arrays/MaxInAllSubArrays.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public static int[] maxInAllSubArraysOfSizeKNaive(int[] a, int k) {
3232
for (int i = 0; i <= a.length - k; i++) {
3333
kElements = Arrays.copyOfRange(a, i, i + k);
3434
/**
35-
* maxHeapify() can't be used because to call maxHeapify() on i, left(i) and right (i) should
35+
* maxHeapify() can't be used because to call maxHeapify() on i because left(i) and right (i) should
3636
* already satisfy the max heap property which isn't true in this case.
3737
*/
38-
MaxHeap.buildMaxHeap(kElements);
39-
maxElements[i] = kElements[0];
38+
MaxHeap maxHeap = new MaxHeap(kElements);
39+
maxHeap.buildMaxHeap();
40+
maxElements[i] = maxHeap.findMax();
4041
}
4142

4243
return maxElements;
Lines changed: 18 additions & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package me.ramswaroop.arrays;
22

3-
import java.util.Arrays;
4-
import java.util.NoSuchElementException;
3+
import me.ramswaroop.common.MaxHeap;
4+
import me.ramswaroop.common.MinHeap;
55

66
/**
77
* Created by IntelliJ IDEA.
@@ -12,10 +12,17 @@
1212
*/
1313
public class MedianOfStream {
1414

15+
/**
16+
* @param med
17+
* @param elem
18+
* @param maxHeap
19+
* @param minHeap
20+
* @return
21+
*/
1522
public static int getMedianOfStream(int med, int elem, MaxHeap maxHeap, MinHeap minHeap) {
1623

1724
switch (compare(maxHeap.getSize(), minHeap.getSize())) {
18-
case 0:
25+
case 0: // sizes of maxHeap and minHeap are same
1926
if (elem < med) {
2027
maxHeap.insert(elem);
2128
med = maxHeap.findMax();
@@ -24,20 +31,20 @@ public static int getMedianOfStream(int med, int elem, MaxHeap maxHeap, MinHeap
2431
med = minHeap.findMin();
2532
}
2633
break;
27-
case 1:
34+
case 1: // size of maxHeap greater than minHeap
2835
if (elem < med) {
29-
minHeap.insert(maxHeap.deleteMax());
36+
minHeap.insert(maxHeap.extractMax());
3037
maxHeap.insert(elem);
3138
} else {
3239
minHeap.insert(elem);
3340
}
3441
med = (maxHeap.findMax() + minHeap.findMin()) / 2;
3542
break;
36-
case -1:
43+
case -1: // size of maxHeap smaller than minHeap
3744
if (elem < med) {
3845
maxHeap.insert(elem);
3946
} else {
40-
maxHeap.insert(minHeap.deleteMin());
47+
maxHeap.insert(minHeap.extractMin());
4148
minHeap.insert(elem);
4249
}
4350
med = (maxHeap.findMax() + minHeap.findMin()) / 2;
@@ -48,8 +55,10 @@ public static int getMedianOfStream(int med, int elem, MaxHeap maxHeap, MinHeap
4855

4956
static void printMedianOfStream(int[] a) {
5057
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
5362
for (int i = 0; i < a.length; i++) {
5463
m = getMedianOfStream(m, a[i], maxHeap, minHeap);
5564
}
@@ -68,250 +77,3 @@ public static void main(String a[]) {
6877
printMedianOfStream(new int[]{5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4});
6978
}
7079
}
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("\nHeap = ");
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

Comments
 (0)