353 |
|
*/ |
354 |
|
private static <T> void siftDownComparable(int k, T x, Object[] array, |
355 |
|
int n) { |
356 |
< |
Comparable<? super T> key = (Comparable<? super T>)x; |
357 |
< |
int half = n >>> 1; // loop while a non-leaf |
358 |
< |
while (k < half) { |
359 |
< |
int child = (k << 1) + 1; // assume left child is least |
360 |
< |
Object c = array[child]; |
361 |
< |
int right = child + 1; |
362 |
< |
if (right < n && |
363 |
< |
((Comparable<? super T>) c).compareTo((T) array[right]) > 0) |
364 |
< |
c = array[child = right]; |
365 |
< |
if (key.compareTo((T) c) <= 0) |
366 |
< |
break; |
367 |
< |
array[k] = c; |
368 |
< |
k = child; |
356 |
> |
if (n > 0) { |
357 |
> |
Comparable<? super T> key = (Comparable<? super T>)x; |
358 |
> |
int half = n >>> 1; // loop while a non-leaf |
359 |
> |
while (k < half) { |
360 |
> |
int child = (k << 1) + 1; // assume left child is least |
361 |
> |
Object c = array[child]; |
362 |
> |
int right = child + 1; |
363 |
> |
if (right < n && |
364 |
> |
((Comparable<? super T>) c).compareTo((T) array[right]) > 0) |
365 |
> |
c = array[child = right]; |
366 |
> |
if (key.compareTo((T) c) <= 0) |
367 |
> |
break; |
368 |
> |
array[k] = c; |
369 |
> |
k = child; |
370 |
> |
} |
371 |
> |
array[k] = key; |
372 |
|
} |
370 |
– |
array[k] = key; |
373 |
|
} |
374 |
|
|
375 |
|
private static <T> void siftDownUsingComparator(int k, T x, Object[] array, |
376 |
|
int n, |
377 |
|
Comparator<? super T> cmp) { |
378 |
< |
int half = n >>> 1; |
379 |
< |
while (k < half) { |
380 |
< |
int child = (k << 1) + 1; |
381 |
< |
Object c = array[child]; |
382 |
< |
int right = child + 1; |
383 |
< |
if (right < n && cmp.compare((T) c, (T) array[right]) > 0) |
384 |
< |
c = array[child = right]; |
385 |
< |
if (cmp.compare(x, (T) c) <= 0) |
386 |
< |
break; |
387 |
< |
array[k] = c; |
388 |
< |
k = child; |
378 |
> |
if (n > 0) { |
379 |
> |
int half = n >>> 1; |
380 |
> |
while (k < half) { |
381 |
> |
int child = (k << 1) + 1; |
382 |
> |
Object c = array[child]; |
383 |
> |
int right = child + 1; |
384 |
> |
if (right < n && cmp.compare((T) c, (T) array[right]) > 0) |
385 |
> |
c = array[child = right]; |
386 |
> |
if (cmp.compare(x, (T) c) <= 0) |
387 |
> |
break; |
388 |
> |
array[k] = c; |
389 |
> |
k = child; |
390 |
> |
} |
391 |
> |
array[k] = x; |
392 |
|
} |
388 |
– |
array[k] = x; |
393 |
|
} |
394 |
|
|
395 |
|
/** |