File tree 1 file changed +2
-4
lines changed
1 file changed +2
-4
lines changed Original file line number Diff line number Diff line change @@ -70,10 +70,8 @@ private void swap(int first, int second) {
70
70
private void heapSubtree (int rootIndex , int lastChild ) {
71
71
int leftIndex = rootIndex * 2 + 1 ;
72
72
int rightIndex = rootIndex * 2 + 2 ;
73
- boolean hasLeftChild = leftIndex <= lastChild ;
74
- boolean hasRightChild = rightIndex <= lastChild ;
75
73
int root = this .heap [rootIndex ];
76
- if (hasRightChild ) {
74
+ if (rightIndex <= lastChild ) { // if has right and left children
77
75
int left = this .heap [leftIndex ];
78
76
int right = this .heap [rightIndex ];
79
77
if (left < right && left < root ) {
@@ -83,7 +81,7 @@ private void heapSubtree(int rootIndex, int lastChild) {
83
81
this .swap (rightIndex , rootIndex );
84
82
this .heapSubtree (rightIndex , lastChild );
85
83
}
86
- } else if (hasLeftChild ) { // if no right, but has left
84
+ } else if (leftIndex <= lastChild ) { // if no right child , but has left child
87
85
int left = this .heap [leftIndex ];
88
86
if (left < root ) {
89
87
this .swap (leftIndex , rootIndex );
You can’t perform that action at this time.
0 commit comments