Java 中的自下而上堆错误

发布于 2024-10-17 19:56:01 字数 1684 浏览 2 评论 0原文

所以,我尝试在这里实现bottomupheap算法: http://www.apl.jhu .edu/Classes/Notes/Felikson/courses/605202/lectures/L8/L8.html

Algorithm bottomUpHeap(S)
Input: a sequence S storing 2h-1 keys
Output: a heap H storing the keys in S
if S is empty then
    return an empty heap
remove the first key, k, from S
Split S into subsequences S1 and S2, each of size (n-1)/2
H1¬ bottomUpHeap(S1)
H2¬ bottomUpHeap(S2)
Create binary tree H such with k at root, H1 at left subtree and H2 at right subtree
Perform down-heap bubbling from root if necessary
return H

自从我用 java 编程以来已经有一段时间了,我不断收到一些我不知道的错误。我想知道是否有人可以帮助我清理一些算法步骤。

我创建了一个带有数据和左引用指针和右引用指针(或任何 java 调用它们)的堆节点。输入序列是一个被转换为 ArrayList 的数组。这就是我传递给上面函数的内容。

我从 S 中删除第一个键并使用该键创建一个新节点。在我的示例中,我仅使用 Integer 并将键设置为数据引用。

然后我用 S1 = S.sublist(0, S.length/2)S2 = S.sublist(S.length/2, S.length)

这是我到目前为止所拥有的:

ArrayList 作为 S.Tree 传递code> 定义为Tree(data, left, right)。谢谢。

private Tree Heapify(List<Integer> S){

    if (S.isEmpty()){
        Tree emptyHeap = new Tree();
        return emptyHeap;
    }

    int tmpk = S.get(0);
    S.remove(0);

    int halfArr = S.size()/2;

    List<Integer> S1 = S.subList(0, halfArr);
    List<Integer> S2 = S.subList(halfArr, S.size());

    Tree k = new Tree(tmpk, Heapify(S1), Heapify(S2));

    //Downheap.
    return null;
}

从看来,当传递一个空列表时,由于某种原因使用子列表时,它并不认为它是一个空列表。看起来当它尝试对空执行任何操作(例如 S.isEmpty() )时,它会抛出错误。

谢谢!

So, im trying to implement the bottomupheap algorithm here:
http://www.apl.jhu.edu/Classes/Notes/Felikson/courses/605202/lectures/L8/L8.html

Algorithm bottomUpHeap(S)
Input: a sequence S storing 2h-1 keys
Output: a heap H storing the keys in S
if S is empty then
    return an empty heap
remove the first key, k, from S
Split S into subsequences S1 and S2, each of size (n-1)/2
H1¬ bottomUpHeap(S1)
H2¬ bottomUpHeap(S2)
Create binary tree H such with k at root, H1 at left subtree and H2 at right subtree
Perform down-heap bubbling from root if necessary
return H

It's been a while since I programmed in java and I keep getting some errors unknown to me. I was wondering if someone would help me out by clearing up some of the algorithm steps.

I created a Heap node with data and left and right reference pointers (or whatever java calls them). The input sequence is an array which gets converted to ArrayList. Thats what I pass into the function above.

I remove the first key from S and create a new node with that key. In my example, I'm just using Integers and the key is set to the data reference.

I then use
S1 = S.sublist(0, S.length/2)
and
S2 = S.sublist(S.length/2, S.length)

Heres what i have so far:

An ArrayList is passed as S. Tree is defined as Tree(data, left, right). Thanks.

private Tree Heapify(List<Integer> S){

    if (S.isEmpty()){
        Tree emptyHeap = new Tree();
        return emptyHeap;
    }

    int tmpk = S.get(0);
    S.remove(0);

    int halfArr = S.size()/2;

    List<Integer> S1 = S.subList(0, halfArr);
    List<Integer> S2 = S.subList(halfArr, S.size());

    Tree k = new Tree(tmpk, Heapify(S1), Heapify(S2));

    //Downheap.
    return null;
}

From what it seems is that when an empty list is passed it doesn't think its an empty list when using sublist for some reason. It looks like when it tries to do anything on an empty such as S.isEmpty() it throws an error.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-10-24 19:56:01

我以前经历过这个,结论是你必须使用:

S1 = new ArrayList(S.sublist(0, S.length/2));

javadoc,但是 sublist 仅返回给定范围的原始列表的视图。查看源代码来了解它的魔力发生。

如果您仍然希望保留这一点,在我看来完全尴尬的抽象,我建议您使用 s.size() == 0 代替 s.isEmpty() 。哦,约定也有变量在 camelcase 中声明。

I've experience this before, the conclusion is that you have to use:

S1 = new ArrayList(S.sublist(0, S.length/2));

It's a little unclear from the javadoc, however sublist returns only a view of the original list for the given range. Have a look at the source code to see the magic happen.

If you still wish to retain this, in my opinion completely awkward, abstraction I would recommend you to use s.size() == 0 instead of s.isEmpty(). Oh, convention also has it variables are declared in camelcase.

温柔少女心 2024-10-24 19:56:01

迭代列表时无法删除

这样做:

private Tree heapify(List list){

        if (list.isEmpty()){
            return null;
        }

        int tmpk = list.get(0);
//      list.remove(0);
        List s1 = null;
        List s2 = null;
        list = list.subList(1, list.size()); // change the list instead

        int halfArr = list.size()/2;

        s1 = list.subList(0, halfArr);
        s2 = list.subList(halfArr, list.size());

        Tree k = new Tree(tmpk, heapify(s1), heapify(s2));

        return k;
    }

You cannot remove while iterating through the list.

Do this:

private Tree heapify(List list){

        if (list.isEmpty()){
            return null;
        }

        int tmpk = list.get(0);
//      list.remove(0);
        List s1 = null;
        List s2 = null;
        list = list.subList(1, list.size()); // change the list instead

        int halfArr = list.size()/2;

        s1 = list.subList(0, halfArr);
        s2 = list.subList(halfArr, list.size());

        Tree k = new Tree(tmpk, heapify(s1), heapify(s2));

        return k;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文