繁体   English   中英

用Java调试Max Heap

[英]Debugging Max Heap in Java

不久以前,我正在用C ++编程,并且找到了有关制作最大堆的教程。 这是代码,似乎产生了正确的顺序堆。

#include <iostream>

const int size = 14;
static int A[size] = {1, 2, 5, 10, 2, 11, 12, 7, 23, 22, 34, 54, 12, 22};

int left(int i)
{
    return (2*i)+1;
}

int right(int i)
{
    return (2*i)+2;
}

//a leaf of a value less than the size of the array
//and larger than the center of the array
bool isLeaf(int i)
{
    if(i >= (size/2) && i <= size)
    {
        return true;
    }
    return false;
}

void maxHeapify(int i)
{
    //if it isn't a leaf it may need to be swapped
    if(!isLeaf(i))
    {
        //if the value is less than it's right or left child
        if(A[i] < A[left(i)] || A[i] < A[right(i)])
        {
            //if the left child is smaller
            if(A[left(i)] > A[right(i)])
            {
                //swap the left child with the index
                std::cout << "Swapping " << A[i] << " and " << A[left(i)] << std::endl;
                int a = A[i];
                A[i] = A[left(i)];
                A[left(i)] = a;
                //and run maxHeapify in it. This will push the value to
                //it's lowest point and order all things below the parent
                maxHeapify(left(i));
            }
            else
            {
                //else swap with the right child since it is larger
                std::cout << "Swapping " << A[i] << " and " << A[right(i)] << std::endl;
                int a = A[i];
                A[i] = A[right(i)];
                A[right(i)] = a;
                //run maxheap on that value. This will push the value to it's lowest position
                maxHeapify(right(i));
            }
        }
    }
}


bool buildHeap()
{
    //need to run for each node that is not a leaf
    for(int i = (size/2); i >= 0; i--)
    {
        maxHeapify(i);
    }
    return true;
}


int main()
{
    std::cout << "before: " << std::endl;
    for(int i = 0; i < size; i++)
    {
        std::cout << A[i] << ", ";
    }
    std::cout << std::endl;
    buildHeap();
    std::cout << "After: " << std::endl;
    for(int i = 0; i < size; i++)
    {
        std::cout << A[i] << ",";
    }
    std::cout << std::endl;
    return 0;
}

上面的输出是:
之前:
1,2,5,10,2,11,12,7,23,22,34,54,12,22,
交换12和22
交换11和54
交换2和34
交换10和23
交换5和54
交换5和12
交换2和34
交换2和22
交换1和54
交换1和22
交换1和12

后:
54,34,22,23,22,12,12,7,10,2,2,11,5,1,

目前,我正在学习一个期中考试,并且试图在Java中重现我的代码。 我已经看了一段时间了,但似乎找不到我要去哪里。 这是坏掉的Java:Main.java

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] a = { 1, 2, 5, 10, 2, 11, 12, 7, 23, 22, 34, 54, 12, 22 };
        System.out.println("Begfore");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + ", ");
        }
        Heap h = new Heap(a);
        h.buildHeap();
        h.print();
        System.out.println();
        System.out.println(a.length);
    }
}

Heap.java

public class Heap {

    int[] heap;

    public Heap(int[] a) {
        // heap = new int[a.length];
        heap = a;
    }

    public void buildHeap() {
        for (int i = heap.length / 2; i >= 0; i--) {
            maxHeapify(i);
        }
    }

    public int getLeft(int a) {
        return (2 * a) + 1;
    }

    public int getRight(int a) {
        return (2 * a) + 2;
    }

    private boolean isLeaf(int a) {
        if (a >= ((heap.length) / 2) && a <= heap.length) {
            return true;
        } else {
            return false;
        }
    }

    public void maxHeapify(int a) {
        if (!isLeaf(a)) {
            if (heap[a] < heap[getLeft(a)] || heap[a] < heap[getRight(a)]) {
                if (getLeft(a) <= heap.length && getRight(a) < heap.length) {
                    if (heap[getLeft(a)] > heap[getRight(a)]) {
                        int watcher = heap[a];
                        heap[a] = heap[getLeft(a)];
                        heap[getLeft(a)] = watcher;
                        maxHeapify(getLeft(a));
                    } else {
                        int watcher = heap[a];
                        heap[a] = heap[getRight(a)];
                        heap[getRight(a)] = watcher;
                        maxHeapify(getRight(a));
                    }
                }
            }
        }
    }

    public void print() {
        System.out.println("heap");
        for (int i = 0; i < heap.length; i++) {
            System.out.print(heap[i] + ", ");
        }
    }
}

这不会产生正确的最大有序堆。
Java输出:54、34、12、23、22、12、1、7、10、2、2、11、5、22,

此代码有一些基本问题。 让我解释一下。

首先,在Max Heapify函数中,您无需检查是否为叶子的条件。 这将我带到您的最大heapify函数调用。

始终从索引heap.length / 2-1到0调用heapify函数,请注意您尚未使用-1。 这样可以始终从叶子中保存heapify,因为在二进制堆中,无论max on min,叶子都位于从heap.length / 2到heap.length-1的索引处。

此代码的另一个错误是使用索引的方式。 有两种使用索引的方法。

在这种情况下,都不应使用从0到heap.length -1的实数索引

<=heap.length or >=heap.length

但是您在堆代码中使用了这样的条件。

或者使用伪索引,范围从1到heap.length,在[]中使用它们时,只需减去-1。 这在我的Java代码中完美运行。

希望这可以帮助。 如果需要,我可以给您Java代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM