使用二进制堆&使用Remove()产生循环问题比较器-Java

时间:2014-11-13 02:51:31

标签: java loops comparator

我们有一个包含10个项目的二进制堆。我正在努力获得"删除"使用比较器工作的功能。 我遇到了一个无限循环的情况,我不确定为什么。任何事都会有所帮助。感谢。

public E remove() {        
    int current = 1;
    int lastValue = size();

    E element = get(lastValue);
    set(1,element);

    remove(lastValue);

    int left = current * 2;
    int right = current * 2 +1;

    E leftElement= get(left);
    E rightElement= get(right);

    int value1 = comp.compare(element, leftElement);
    int value2 = comp.compare(element, rightElement);

    while(value1 == 1 || value2 == 1){
        int value3 = comp.compare(leftElement, rightElement);
        if (value3 == 1){
            //left > right
            set(current, rightElement);
            set(right, element);
            current = right;

        }
        else{
            set(current, leftElement);
            set(left, element);
            current= left;            
        }

        int left2 = current * 2;
        int right2 = current * 2 + 1;

        leftElement = get(left2);
        rightElement = get(right2);

        value1 = comp.compare(element, leftElement);
        value2 = comp.compare(element, rightElement);
    }

    // You must provide

    return null;
}

0 个答案:

没有答案