1
1
package com .blaxswan .algorithm .heap ;
2
2
3
- import sun .reflect .generics .reflectiveObjects .NotImplementedException ;
4
-
5
3
import java .util .ArrayList ;
6
4
import java .util .Arrays ;
7
5
import java .util .List ;
@@ -15,14 +13,14 @@ public class BinaryHeapArray<T extends Comparable<T>> implements BinaryHeap<T> {
15
13
16
14
private OrderingType type = OrderingType .MIN ;
17
15
private int size = 0 ;
16
+ @ SuppressWarnings ("unchecked" )
18
17
private T [] array = (T []) new Comparable [MINIMUM_SIZE ];
19
18
20
19
public BinaryHeapArray () {
21
20
22
21
size = 0 ;
23
22
}
24
23
25
-
26
24
public BinaryHeapArray (OrderingType type ) {
27
25
this ();
28
26
this .type = type ;
@@ -47,24 +45,27 @@ public boolean add(T value) {
47
45
48
46
@ Override
49
47
public T remove (T value ) {
50
- if (array .length == 0 ) return null ;
48
+ if (array .length == 0 )
49
+ return null ;
51
50
for (int i = 0 ; i < size ; i ++) {
52
51
T node = array [i ];
53
- if (node .equals (value )) return remove (i );
52
+ if (node .equals (value ))
53
+ return remove (i );
54
54
}
55
55
return null ;
56
56
}
57
57
58
58
private T remove (int index ) {
59
- if (index <0 || index >=size ) return null ;
59
+ if (index < 0 || index >= size )
60
+ return null ;
60
61
61
62
T t = array [index ];
62
63
array [index ] = array [--size ];
63
64
array [size ] = null ;
64
65
65
66
heapDown (index );
66
67
67
- int shrinkSize = array .length >> 1 ;
68
+ int shrinkSize = array .length >> 1 ;
68
69
if (shrinkSize >= MINIMUM_SIZE && size < shrinkSize )
69
70
shrink ();
70
71
@@ -74,7 +75,7 @@ private T remove(int index) {
74
75
private void heapUp (int idx ) {
75
76
int nodeIndex = idx ;
76
77
T value = this .array [nodeIndex ];
77
- if (value == null )
78
+ if (value == null )
78
79
return ;
79
80
80
81
while (nodeIndex >= 0 ) {
@@ -85,8 +86,7 @@ private void heapUp(int idx) {
85
86
T parent = this .array [parentIndex ];
86
87
87
88
if ((type == OrderingType .MIN && value .compareTo (parent ) < 0 )
88
- || (type == OrderingType .MAX && value .compareTo (parent ) > 0 )
89
- ) {
89
+ || (type == OrderingType .MAX && value .compareTo (parent ) > 0 )) {
90
90
// Node is greater/lesser than parent, switch node with parent
91
91
this .array [parentIndex ] = value ;
92
92
this .array [nodeIndex ] = parent ;
@@ -99,7 +99,7 @@ private void heapUp(int idx) {
99
99
100
100
private void heapDown (int index ) {
101
101
T value = this .array [index ];
102
- if (value == null )
102
+ if (value == null )
103
103
return ;
104
104
105
105
int leftIndex = getLeftIndex (index );
@@ -114,18 +114,18 @@ private void heapDown(int index) {
114
114
115
115
T nodeToMove = null ;
116
116
int nodeToMoveIndex = -1 ;
117
- if ((type == OrderingType .MIN && left != null && right != null && value .compareTo (left ) > 0 && value .compareTo (right ) > 0 )
118
- || (type == OrderingType .MAX && left != null && right != null && value .compareTo (left ) < 0 && value .compareTo (right ) < 0 )) {
117
+ if ((type == OrderingType .MIN && left != null && right != null && value .compareTo (left ) > 0
118
+ && value .compareTo (right ) > 0 )
119
+ || (type == OrderingType .MAX && left != null && right != null && value .compareTo (left ) < 0
120
+ && value .compareTo (right ) < 0 )) {
119
121
// Both children are greater/lesser than node
120
- if ((right !=null ) &&
121
- ((type == OrderingType .MIN && (right .compareTo (left ) < 0 )) || ((type == OrderingType .MAX && right .compareTo (left ) > 0 )))
122
- ) {
122
+ if ((right != null ) && ((type == OrderingType .MIN && (right .compareTo (left ) < 0 ))
123
+ || ((type == OrderingType .MAX && right .compareTo (left ) > 0 )))) {
123
124
// Right is greater/lesser than left
124
125
nodeToMove = right ;
125
126
nodeToMoveIndex = rightIndex ;
126
- } else if ((left !=null ) &&
127
- ((type == OrderingType .MIN && left .compareTo (right ) < 0 ) || (type == OrderingType .MAX && left .compareTo (right ) > 0 ))
128
- ) {
127
+ } else if ((left != null ) && ((type == OrderingType .MIN && left .compareTo (right ) < 0 )
128
+ || (type == OrderingType .MAX && left .compareTo (right ) > 0 ))) {
129
129
// Left is greater/lesser than right
130
130
nodeToMove = left ;
131
131
nodeToMoveIndex = leftIndex ;
@@ -135,14 +135,12 @@ private void heapDown(int index) {
135
135
nodeToMoveIndex = rightIndex ;
136
136
}
137
137
} else if ((type == OrderingType .MIN && right != null && value .compareTo (right ) > 0 )
138
- || (type == OrderingType .MAX && right != null && value .compareTo (right ) < 0 )
139
- ) {
138
+ || (type == OrderingType .MAX && right != null && value .compareTo (right ) < 0 )) {
140
139
// Right is greater/lesser than node
141
140
nodeToMove = right ;
142
141
nodeToMoveIndex = rightIndex ;
143
142
} else if ((type == OrderingType .MIN && left != null && value .compareTo (left ) > 0 )
144
- || (type == OrderingType .MAX && left != null && value .compareTo (left ) < 0 )
145
- ) {
143
+ || (type == OrderingType .MAX && left != null && value .compareTo (left ) < 0 )) {
146
144
// Left is greater/lesser than node
147
145
nodeToMove = left ;
148
146
nodeToMoveIndex = leftIndex ;
@@ -176,13 +174,13 @@ private static int getRightIndex(int index) {
176
174
177
175
// Grow the array by double
178
176
private void grow () {
179
- int growSize = size << 1 ;
177
+ int growSize = size << 1 ;
180
178
array = Arrays .copyOf (array , growSize );
181
179
}
182
180
183
181
// Shrink the array by half
184
182
private void shrink () {
185
- int shrinkSize = array .length >> 1 ;
183
+ int shrinkSize = array .length >> 1 ;
186
184
array = Arrays .copyOf (array , shrinkSize );
187
185
}
188
186
@@ -193,10 +191,12 @@ public void clear() {
193
191
194
192
@ Override
195
193
public boolean contains (T value ) {
196
- if (array .length == 0 ) return false ;
194
+ if (array .length == 0 )
195
+ return false ;
197
196
for (int i = 0 ; i < size ; i ++) {
198
197
T t = array [i ];
199
- if (t .equals (value )) return true ;
198
+ if (t .equals (value ))
199
+ return true ;
200
200
}
201
201
return false ;
202
202
}
@@ -214,7 +214,8 @@ private boolean validateNode(int index) {
214
214
int rightIndex = getRightIndex (index );
215
215
216
216
// We shouldn't ever have a right node without a left in a heap
217
- if (rightIndex != Integer .MIN_VALUE && leftIndex == Integer .MIN_VALUE ) return false ;
217
+ if (rightIndex != Integer .MIN_VALUE && leftIndex == Integer .MIN_VALUE )
218
+ return false ;
218
219
219
220
if (leftIndex != Integer .MIN_VALUE && leftIndex < size ) {
220
221
T left = this .array [leftIndex ];
@@ -238,8 +239,10 @@ private boolean validateNode(int index) {
238
239
239
240
@ Override
240
241
public T [] getHeap () {
242
+ @ SuppressWarnings ("unchecked" )
241
243
T [] nodes = (T []) new Comparable [size ];
242
- if (array .length == 0 ) return nodes ;
244
+ if (array .length == 0 )
245
+ return nodes ;
243
246
244
247
for (int i = 0 ; i < size ; i ++) {
245
248
T node = this .array [i ];
@@ -250,7 +253,8 @@ public T[] getHeap() {
250
253
251
254
@ Override
252
255
public T getHeadValue () {
253
- if (array .length == 0 ) return null ;
256
+ if (array .length == 0 )
257
+ return null ;
254
258
return array [0 ];
255
259
}
256
260
@@ -259,11 +263,10 @@ public T removeHead() {
259
263
return remove (getHeadValue ());
260
264
}
261
265
262
-
263
266
@ Override
264
267
public java .util .Collection <T > toCollection () {
265
268
266
- throw new NotImplementedException ();
269
+ throw new UnsupportedOperationException ();
267
270
}
268
271
269
272
@ Override
@@ -283,7 +286,8 @@ static <T extends Comparable<T>> String getString(BinaryHeapArray<T> tree) {
283
286
return getString (tree , 0 , "" , true );
284
287
}
285
288
286
- private static <T extends Comparable <T >> String getString (BinaryHeapArray <T > tree , int index , String prefix , boolean isTail ) {
289
+ private static <T extends Comparable <T >> String getString (BinaryHeapArray <T > tree , int index , String prefix ,
290
+ boolean isTail ) {
287
291
StringBuilder builder = new StringBuilder ();
288
292
289
293
T value = tree .array [index ];
@@ -305,14 +309,12 @@ private static <T extends Comparable<T>> String getString(BinaryHeapArray<T> tre
305
309
builder .append (getString (tree , children .get (i ), prefix + (isTail ? " " : "│ " ), false ));
306
310
}
307
311
if (children .size () >= 1 ) {
308
- builder .append (getString (tree , children .get (children .size () - 1 ), prefix
309
- + (isTail ? " " : "│ " ), true ));
312
+ builder .append (getString (tree , children .get (children .size () - 1 ),
313
+ prefix + (isTail ? " " : "│ " ), true ));
310
314
}
311
315
}
312
316
313
317
return builder .toString ();
314
318
}
315
319
}
316
320
}
317
-
318
-
0 commit comments