Java 尝试使用max heap的逻辑实现min heap

Java 尝试使用max heap的逻辑实现min heap,java,min-heap,Java,Min Heap,我正在尝试实现最小堆,其中包括插入、删除和堆排序等方法。我正在使用最大堆的实现,并尝试将其转换为最小堆。但是,我有一些小问题。这是一个非常直接的方法,但我缺少一些我无法获得的东西 这是我正在使用的Helper Max堆实现: public void trickleDown(int index) { int largerChild; Node top = heapArray[index]; // save root while(index < currentSize/2) // while

我正在尝试实现最小堆,其中包括插入、删除和堆排序等方法。我正在使用最大堆的实现,并尝试将其转换为最小堆。但是,我有一些小问题。这是一个非常直接的方法,但我缺少一些我无法获得的东西

这是我正在使用的Helper Max堆实现:

public void trickleDown(int index)
{
int largerChild;
Node top = heapArray[index]; // save root
while(index < currentSize/2) // while node has at
{ // least one child,
int leftChild = 2*index+1;
int rightChild = leftChild+1;
// find larger child
if( rightChild < currentSize && // (rightChild exists?)
heapArray[leftChild].getKey() <
heapArray[rightChild].getKey() )
largerChild = rightChild;
else
largerChild = leftChild;
// top >= largerChild?
if(top.getKey() >= heapArray[largerChild].getKey())
break;
// shift child up
heapArray[index] = heapArray[largerChild];
index = largerChild; // go down
} // end while
heapArray[index] = top; // index <- root
} // end trickleDown()

/////// My Implementation


 /** Removes the top element of the heap and returns it.
 * 
 * Complexity: O(log n)
 * @return Top (min/max) element of the heap.
 * @throws IllegalStateException if the heap is empty.
 */
 T remove() {

    if (size == 0) {
        throw new IllegalStateException();
    }

    Comparable root = data[0];
    data[0] = data[size-1];
    size--;
    trickleDown(0);
    return  (T) root;


  }



private void trickleDown(int i) {

 int largerChild;
 Comparable top = data[i]; // save root
 while(i > size/2 ) // not on bottom row{
 int leftChild = left(i);
 int rightChild = right(i);
 if(rightChild > size && data[left(i)].compareTo(data[right(i)]) < 0 )
 largerChild = leftChild;
 else
 largerChild = rightChild;

 if(data[i].compareTo(data[right(i)]) <= 0 )
 break;

 data[i] = data[largerChild];
 i = largerChild; 
 } 
 data[i] = top; 
}
public void trickleDown(整数索引)
{
国际大儿童;
Node top=heapArray[index];//保存根目录
while(index=大孩子?
如果(top.getKey()>=heapArray[largerChild].getKey())
打破
//把孩子抬起来
heapArray[index]=heapArray[largerChild];
index=largerChild;//向下
}//结束时
heapArray[index]=top;//索引大小/2)//不在底行{
int leftChild=左(i);
int rightChild=right(i);
如果(右子项>大小和数据[左(i)]。比较(数据[右(i)])<0
大孩子=左孩子;
其他的
大孩子=右孩子;
if(数据[i]。与(数据[right(i)])0;--i)比较{
资产质量(h.size(),i);
整数x=(整数)h.remove();
assertEquals(x,新整数(10-i));//项目按正确顺序删除
检查堆料机(h);
}
testRemove失败:应为,但为


我很确定代码很简单,我尝试过将所有内容从max改为min,但只是遗漏了一些东西,我很难弄清楚。

你有最小堆的工作实现吗?在这种情况下,使用比较器而不是调用
compareTo
,可能更容易/更通用。然后你可以可以保持相同的算法,只需切换比较器。这将为您提供正确的算法,而无需执行任何新的操作。不,这就是为什么我要这样做。我可以通过切换比较器从最大值实现最小值吗?我做到了,但逻辑仅此而已。希望您能提供帮助。谢谢。
 void checkHeapOrder(MinHeap h) {
    assertTrue(h != null);

    for(int i = 1; i < h.size() / 2; ++i) 
        assertTrue("Heap order property is broken at element at position " 
        + i,
                   h.data[i].compareTo(h.data[i*2]) < 0 && 
                   h.data[i].compareTo(h.data[i*2 + 1]) < 0);
   }


@Test
public void testRemove() {
    System.out.println("remove");
    MinHeap h = new MinHeap(10);

    boolean throws_exception = false;
    try {
        h.remove();
    } catch (IllegalStateException e) {
        throws_exception = true;
    } catch (Throwable e) {

    }
    assertTrue("remove throws an exception when empty", throws_exception);

    // Permutation of 0...9
    int[] input = { 0, 5, 9, 2, 3, 1, 6, 8, 7, 4 };

    for(int i : input)                    
        h.insert(i);  

    assertTrue(h.isFull());

    for(int i = 10; i > 0; --i) {
        assertEquals(h.size(), i);
        Integer x = (Integer)h.remove();
        assertEquals(x, new Integer(10-i)); // Items are removed in correct order
        checkHeapOrder(h);
    }