private void siftDown(Integer index){ //获取出左边孩子和右边孩子的节点索引 Integer leftIndex = leftChild(index); if (leftIndex > array.getSize()-1) return; Integer rightIndex =rightChild(index); if (rightIndex > array.getSize()-1) return; if (array.get(leftIndex).compareTo(array.get(index)) > 0 ){ array.swap(leftIndex,index); siftDown(leftIndex); }else if (array.get(rightIndex).compareTo(array.get(index))>0){ array.swap(rightIndex,index); siftDown(rightIndex); } }
使用递归更好理解一点。