-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxHeap.java
104 lines (91 loc) · 1.78 KB
/
MaxHeap.java
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
package codingProblems;
/**
* Find Kth Largest element in an unsorted array using Max-Heap
* 1) Build a Max Heap tree in O(n)
* 2) Use Extract Max k times to get kth maximum element. O(klogn)
* Time complexity: O(n + klogn)
*/
public class MaxHeap
{
int[] a;
int size;
int n;
public int getVal( int pos )
{
return a[pos];
}
public MaxHeap( int size, int[] e )
{
this.size = size;
a = new int[size];
createHeap(e);
}
public int parent( int pos )
{
return (pos-1)/2;
}
public void createHeap( int[] e )
{
for( int val : e ) {
int curr = n++;
a[curr] = val;
while( curr != 0 && a[curr] > a[parent(curr)]) {
int t = a[curr];
a[curr] = a[parent(curr)];
a[parent(curr)] = t;
curr = parent(curr);
}
}
}
public int getMax()
{
int max = a[0];
a[0] = a[n-1];
a[n-1] = 0;
n--;
int pos = 0;
while( !leaf(pos) && ( a[pos] < a[left(pos)]) || a[pos] < a[right(pos)]) {
if( a[left(pos)] > a[right(pos)]) {
int t = a[pos];
a[pos] = a[left(pos)];
a[left(pos)] = t;
pos = left(pos);
} else {
int t = a[pos];
a[pos] = a[right(pos)];
a[right(pos)] = t;
pos = right(pos);
}
}
return max;
}
public int getMax( int k )
{
int max = 0;
for( int i=0; i < k; i++ )
{
max = getMax();
}
return max;
}
public boolean leaf( int pos )
{
return pos >= n/2;
}
public int left( int pos )
{
return (2*pos + 1);
}
public int right( int pos )
{
return (2*pos + 2);
}
public static void main(String[] args)
{
int[] a = {88, 30, 11, 17, 22, 16, 39, 8, 31, 55, 29, 63, 77, 69, 99, 90, 81, 2, 20, 53, 62, 5, 33, 44, 6};
int k = 5;
MaxHeap heap = new MaxHeap(50, a);
int res = heap.getMax( k );
System.out.println(k +"th maximum element from end is : " +res);
}
}