1
1
package problems .leetcode ;
2
2
3
3
import java .util .Arrays ;
4
+ import java .util .Collections ;
5
+ import java .util .List ;
4
6
5
7
public class MaxHeap <T extends Comparable <T >> {
6
8
7
9
private T [] heap ;
8
10
private int size ;
9
11
private int maxSize ;
10
12
11
- // Constructor to initialize an
12
- // empty max heap with given maximum
13
- // capacity
14
13
public MaxHeap (int maxSize ) {
15
- // This keyword refers to current instance itself
16
- this .maxSize = maxSize ;
17
- this .size = 0 ;
18
- this .heap = (T []) new Comparable [this .maxSize ];
14
+ this (maxSize , Collections .emptyList ());
19
15
}
20
16
21
- private int parent (int pos ) {
22
- return ( pos - 1 ) / 2 ;
17
+ public MaxHeap (int maxSize , T ... vals ) {
18
+ this ( maxSize , Arrays . asList ( vals )) ;
23
19
}
24
20
25
- private int leftChild (int pos ) {
26
- return (2 * pos ) + 1 ;
21
+ // heapify runtime: O(N)
22
+ public MaxHeap (int maxSize , List <T > vals ) {
23
+ this .size = vals .size ();
24
+ this .maxSize = Math .max (maxSize , this .size );
25
+ this .heap = (T []) new Comparable [this .maxSize + 1 ];
26
+ // max heapify
27
+ for (int i = 0 ; i < vals .size (); i ++) {
28
+ this .heap [i + 1 ] = vals .get (i );
29
+ }
30
+ // heap[size/2..1] are leaves, which are heap themselves.
31
+ for (int i = size / 2 ; i > 0 ; i --) {
32
+ maxHeapify (i );
33
+ }
34
+ }
35
+
36
+ private int parent (int i ) {
37
+ return i / 2 ;
38
+ }
39
+
40
+ private int leftChild (int i ) {
41
+ return 2 * i ;
27
42
}
28
43
29
- private int rightChild (int pos ) {
30
- return ( 2 * pos ) + 2 ;
44
+ private int rightChild (int i ) {
45
+ return 2 * i + 1 ;
31
46
}
32
47
33
48
private void swap (int l , int r ) {
@@ -37,14 +52,19 @@ private void swap(int l, int r) {
37
52
heap [r ] = tmp ;
38
53
}
39
54
55
+ private boolean isGreater (int l , int r ) {
56
+ return heap [l ].compareTo (heap [r ]) > 0 ;
57
+ }
58
+
59
+ // runtime: O(logN)
40
60
private void maxHeapify (int i ) {
41
61
int l = leftChild (i );
42
62
int r = rightChild (i );
43
63
int largest = i ;
44
- if (l < size && heap [ l ]. compareTo ( heap [ i ]) > 0 ) {
64
+ if (l <= size && isGreater ( l , i ) ) {
45
65
largest = l ;
46
66
}
47
- if (r < size && heap [ r ]. compareTo ( heap [ largest ]) > 0 ) {
67
+ if (r <= size && isGreater ( r , largest ) ) {
48
68
largest = r ;
49
69
}
50
70
if (largest != i ) {
@@ -57,27 +77,37 @@ public void add(T element) {
57
77
if (size == maxSize ) {
58
78
throw new IllegalStateException ("heap full!" );
59
79
}
80
+ heap [++size ] = element ;
81
+ heapUp (size );
82
+ }
60
83
61
- heap [size ] = element ;
62
- int current = size ;
63
- while (heap [current ].compareTo (heap [parent (current )]) > 0 ) {
64
- swap (current , parent (current ));
65
- current = parent (current );
84
+ private void heapUp (int i ) {
85
+ while (i > 1 && isGreater (i , parent (i ))) {
86
+ swap (i , parent (i ));
87
+ i = parent (i );
66
88
}
67
- size ++;
68
89
}
69
90
70
91
public T remove () {
71
92
if (size == 0 ) {
72
93
throw new IllegalStateException ("empty heap!" );
73
94
}
74
- T popped = heap [0 ];
75
- heap [0 ] = heap [-- size ];
76
- heap [size ] = null ;
77
- maxHeapify (0 );
95
+ T popped = heap [1 ];
96
+ heap [1 ] = heap [size ];
97
+ heap [size -- ] = null ;
98
+ maxHeapify (1 );
78
99
return popped ;
79
100
}
80
101
102
+ public void increase (int i , T val ) {
103
+ if (i < 1 || i > size ) {
104
+ throw new IndexOutOfBoundsException (i );
105
+ }
106
+
107
+ heap [i ] = val ;
108
+ heapUp (i );
109
+ }
110
+
81
111
@ Override
82
112
public String toString () {
83
113
return Arrays .toString (heap );
@@ -104,5 +134,14 @@ public static void main(String[] args) {
104
134
105
135
System .out .println (heap .remove ());
106
136
System .out .println (heap );
137
+
138
+ heap = new MaxHeap <>(10 , List .of (15 , 5 , 10 , 20 ));
139
+ System .out .println (heap );
140
+
141
+ heap .increase (3 , 25 );
142
+ System .out .println (heap );
143
+ heap .increase (4 , 10 );
144
+ System .out .println (heap );
107
145
}
146
+
108
147
}
0 commit comments