@@ -130,7 +130,8 @@ public boolean add(T value) {
130130 return true ;
131131 }
132132
133- protected void heapUp (int nodeIndex ) {
133+ protected void heapUp (int idx ) {
134+ int nodeIndex = idx ;
134135 T value = this .array [nodeIndex ];
135136 while (nodeIndex >= 0 ) {
136137 int parentIndex = getParentIndex (nodeIndex );
@@ -293,12 +294,14 @@ protected void heapDown(int index) {
293294 if ((type == Type .MIN && left != null && right != null && value .compareTo (left ) > 0 && value .compareTo (right ) > 0 )
294295 || (type == Type .MAX && left != null && right != null && value .compareTo (left ) < 0 && value .compareTo (right ) < 0 )) {
295296 // Both children are greater/lesser than node
296- if ((type == Type .MIN && right .compareTo (left ) < 0 ) || (type == Type .MAX && right .compareTo (left ) > 0 )) {
297+ if ((right !=null ) &&
298+ ((type == Type .MIN && (right .compareTo (left ) < 0 )) || ((type == Type .MAX && right .compareTo (left ) > 0 )))
299+ ) {
297300 // Right is greater/lesser than left
298301 nodeToMove = right ;
299302 nodeToMoveIndex = rightIndex ;
300- } else if ((type == Type . MIN && left . compareTo ( right ) < 0 )
301- || (type == Type .MAX && left .compareTo (right ) > 0 )
303+ } else if ((left != null ) &&
304+ (( type == Type . MIN && left . compareTo ( right ) < 0 ) || (type == Type .MAX && left .compareTo (right ) > 0 ) )
302305 ) {
303306 // Left is greater/lesser than right
304307 nodeToMove = left ;
@@ -431,7 +434,8 @@ public int size() {
431434 * of the Node to get directions for.
432435 * @return Integer array representing the directions to the index.
433436 */
434- private int [] getDirections (int index ) {
437+ private static int [] getDirections (int idx ) {
438+ int index = idx ;
435439 int directionsSize = (int ) (Math .log10 (index + 1 ) / Math .log10 (2 )) - 1 ;
436440 int [] directions = null ;
437441 if (directionsSize > 0 ) {
@@ -585,7 +589,6 @@ private Node<T> getNode(Node<T> startingNode, T value) {
585589 } else if (startingNode != null && !startingNode .value .equals (value )) {
586590 Node <T > left = startingNode .left ;
587591 Node <T > right = startingNode .right ;
588- Type type = this .type ;
589592 if (left != null
590593 && ((type ==Type .MIN && left .value .compareTo (value )<=0 )||(type ==Type .MAX && left .value .compareTo (value )>=0 ))
591594 ) {
@@ -639,16 +642,18 @@ public T remove(T value) {
639642 /**
640643 * Heap up the heap from this node.
641644 *
642- * @param node
645+ * @param nodeToHeapUp
643646 * to heap up.
644647 */
645- protected void heapUp (Node <T > node ) {
648+ protected void heapUp (Node <T > nodeToHeapUp ) {
649+ Node <T > node = nodeToHeapUp ;
646650 while (node != null ) {
647651 Node <T > heapNode = node ;
648652 Node <T > parent = heapNode .parent ;
649653
650- if ((type == Type .MIN && parent != null && node .value .compareTo (parent .value ) < 0 )
651- || (type == Type .MAX && parent != null && node .value .compareTo (parent .value ) > 0 )) {
654+ if ((parent != null ) &&
655+ ((type == Type .MIN && node .value .compareTo (parent .value ) < 0 ) || (type == Type .MAX && node .value .compareTo (parent .value ) > 0 ))
656+ ) {
652657 // Node is less than parent, switch node with parent
653658 Node <T > grandParent = parent .parent ;
654659 Node <T > parentLeft = parent .left ;
@@ -692,12 +697,13 @@ protected void heapUp(Node<T> node) {
692697 /**
693698 * Heap down the heap from this node.
694699 *
695- * @param node
700+ * @param nodeToHeapDown
696701 * to heap down.
697702 */
698- protected void heapDown (Node <T > node ) {
699- if (node ==null ) return ;
703+ protected void heapDown (Node <T > nodeToHeapDown ) {
704+ if (nodeToHeapDown ==null ) return ;
700705
706+ Node <T > node = nodeToHeapDown ;
701707 Node <T > heapNode = node ;
702708 Node <T > left = heapNode .left ;
703709 Node <T > right = heapNode .right ;
@@ -709,8 +715,10 @@ protected void heapDown(Node<T> node) {
709715
710716 Node <T > nodeToMove = null ;
711717
712- if ((type == Type .MIN && left != null && right != null && node .value .compareTo (left .value ) > 0 && node .value .compareTo (right .value ) > 0 )
713- || (type == Type .MAX && left != null && right != null && node .value .compareTo (left .value ) < 0 && node .value .compareTo (right .value ) < 0 )) {
718+ if ((left != null && right != null ) &&
719+ ((type == Type .MIN && node .value .compareTo (left .value ) > 0 && node .value .compareTo (right .value ) > 0 )
720+ || (type == Type .MAX && node .value .compareTo (left .value ) < 0 && node .value .compareTo (right .value ) < 0 ))
721+ ) {
714722 // Both children are greater/lesser than node
715723 if ((type == Type .MIN && right .value .compareTo (left .value ) < 0 ) || (type == Type .MAX && right .value .compareTo (left .value ) > 0 )) {
716724 // Right is greater/lesser than left
@@ -825,12 +833,13 @@ private boolean validateNode(Node<T> node) {
825833 *
826834 * @param node
827835 * to populate.
828- * @param index
836+ * @param idx
829837 * of node in array.
830838 * @param array
831839 * where the node lives.
832840 */
833- private void getNodeValue (Node <T > node , int index , T [] array ) {
841+ private void getNodeValue (Node <T > node , int idx , T [] array ) {
842+ int index = idx ;
834843 array [index ] = node .value ;
835844 index = (index * 2 ) + 1 ;
836845
@@ -969,20 +978,23 @@ public JavaCompatibleBinaryHeapArray(BinaryHeapArray<T> heap) {
969978 /**
970979 * {@inheritDoc}
971980 */
981+ @ Override
972982 public boolean add (T value ) {
973983 return heap .add (value );
974984 }
975985
976986 /**
977987 * {@inheritDoc}
978988 */
989+ @ Override
979990 public boolean remove (Object value ) {
980991 return (heap .remove ((T )value )!=null );
981992 }
982993
983994 /**
984995 * {@inheritDoc}
985996 */
997+ @ Override
986998 public boolean contains (Object value ) {
987999 return heap .contains ((T )value );
9881000 }
@@ -1065,13 +1077,15 @@ public boolean add(T value) {
10651077 /**
10661078 * {@inheritDoc}
10671079 */
1080+ @ Override
10681081 public boolean remove (Object value ) {
10691082 return (heap .remove ((T )value )!=null );
10701083 }
10711084
10721085 /**
10731086 * {@inheritDoc}
10741087 */
1088+ @ Override
10751089 public boolean contains (Object value ) {
10761090 return heap .contains ((T )value );
10771091 }
0 commit comments