使用数组实现最小堆:插入和删除最小值(带有重复项)

时间:2012-04-26 23:21:40

标签: java arrays data-structures heap min-heap

我正在尝试用Java实现Min Heap,但是我遇到插入和删除元素的问题(最后插入,删除root作为min)。它似乎在大多数情况下工作(我使用一个程序来直观地显示堆,并在删除min时打印出新的根,类似的东西)。

我的问题是,由于某种原因,当添加新项目时,root不会使用新项目切换,但我无法弄明白为什么。此外,似乎只有当存在大量重复时才会出现问题,堆似乎不能完全保持有序(父级小于子级)。在大多数情况下,确实如此。只是偶尔没有,对我而言似乎是随机的。

这是通过泛型完成的,基本上遵循大多数算法。我知道的其他所有事情都有效,这绝对是这两种方法的问题。

public void insert(T e)  {
    if (size == capacity)
        increaseSize(); //this works fine

    last = curr; //keeping track of the last index, for heapifying down/bubbling down when removing min
    int parent = curr/2;
    size++; //we added an element, so the size of our data set is larger


    heap[curr] = e; //put value at end of array

    //bubble up
    int temp = curr;

    while (temp > 1 && ((Comparable<T>) heap[temp]).compareTo(heap[parent]) < 0) { //if current element is less than the parent
        //integer division
        parent = temp/2;
        swap(temp, parent); //the swapping method should be correct, but I included it for clarification
        temp = parent; //just moves the index value to follow the element we added as it is bubbled up
    }

    curr++; //next element to be added will be after this one


}

public void swap(int a, int b){
    T temp = heap[a];
    heap[a] = heap[b];
    heap[b] = temp;
}


public T removeMin() {

    //root is always min
    T min = heap[1];

    //keep sure array not empty, or else size will go negative
    if (size > 0)
        size--;

    //put last element as root
    heap[1] = heap[last];
    heap[last] = null;

    //keep sure array not empty, or else last will not be an index
    if (last > 0)
        last--;

    //set for starting at root
    int right = 3;
    int left = 2;
    int curr = 1;
    int smaller = 0;

    //fix heap, heapify down
    while(left < size && right < size){ //we are in array bounds

        if (heap[left] != null && heap[right] != null){ //so no null pointer exceptions
            if (((Comparable<T>)heap[left]).compareTo(heap[right]) < 0) //left is smaller
                smaller = left;
            else if (((Comparable<T>)heap[left]).compareTo(heap[right]) > 0) //right is smaller
                smaller = right;
            else //they are equal
                smaller = left;
        }
        if (heap[left] == null || heap[right] == null)//one child is null
        {
            if (heap[left] == null && heap[right] == null)//both null, stop
                break;
            if (heap[left] == null)//right is not null
                smaller = right;
            else //left is not null
                smaller = left;
        }


        if (((Comparable<T>)heap[curr]).compareTo(heap[smaller]) > 0)//compare smaller or only child
        {
            swap(curr,smaller); //swap with child
            curr = smaller; //so loop can check new children for new placement
        }
        else //if in order, stop
            break;

        right = 2*curr + 1; //set new children
        left = 2*curr;
    }


    return min; //return root
}

任何未在方法中声明的变量都是全局的,我知道有些事情可能是多余的,比如添加的整个当前/最后/临时情况,所以我很抱歉。我试图让所有名字都自我解释,并解释我在removeMin中所做的所有检查。任何帮助都会非常感激,我已经尽可能地了解和调试。我想我在这里根本就缺少一些东西。

1 个答案:

答案 0 :(得分:0)

为了帮助您调试,您应该简化代码。 'last'变量有点奇怪。当你进行循环时,也可以在'insert'中,temp可能会变为0,即

while (temp >= 0 &&......