Max Heap插入函数实现C++

发布于 2025-02-04 07:15:17 字数 1520 浏览 2 评论 0原文

我正在尝试将钥匙值插入堆中。我正在使用testunit.cpp出错。我得到了这些错误:

断言失败了。 预期:< [(10,100),(7,70),(6,60),(5,50),(2,20),(1,10),(1,10),(3,30),(4,40) ]> 实际:< [(7,70),(5,50),(6,60),(4,40),(2,20),(1,10),(1,10),(3,30),(10,100) ]>

断言失败了。 预期:< [(10,100),(4,40),(5,50),(1,10),(2,20),(3,30),(3,30)]> 实际:< [((5,50),(4,40),(3,30),(1,10),(2,20),(10,100),(10,100)]>

断言失败了。 预期:< [(9,90),(7,70),(8,80),(6,60),(2,20),(1,10),(1,10),(3,30),(5,5, 50)]> 实际:< [(9,90),(7,70),(8,80),(5,50),(2,20),(1,10),(1,10),(3,30),(6,6,, 60)]]>

断言失败了。 预期:< [(6,60),(5,50),(4,40),(1,10),(2,20),(3,30),(3,30)]> 实际:< [(6,60),(5,50),(3,30),(1,10),(2,20),(4,40)]>

我的插入功能是:

void insert(KeyValueType const& keyValue)
    {

        size_t asize = size();
        if (asize == 0)
        {
            table.push_back(keyValue);
        }
        else
        {
            table.push_back(keyValue);
            for (int i = asize / 2 - 1; i >= 0; i--)
            {
                heapify(i);

            }
        }
    } 

我的堆功能是:

void heapify(size_t index)
    {
        auto previous_index = index;
        do
        {
            previous_index = index;
            auto largest = getLargestFromParentAndHisChildren(index);
            if (index != largest)
            {
                std::swap(table[index], table[largest]);
                index = largest;
            }
        } while (index != previous_index);
    }

I'm trying to insert key values into heap. I'm using TestUnit.cpp for errors. I got these errors:

Assert failed.
Expected:<[(10,100),(7,70),(6,60),(5,50),(2,20),(1,10),(3,30),(4,40)]>
Actual:<[(7,70),(5,50),(6,60),(4,40),(2,20),(1,10),(3,30),(10,100)]>

Assert failed.
Expected:<[(10,100),(4,40),(5,50),(1,10),(2,20),(3,30)]>
Actual:<[(5,50),(4,40),(3,30),(1,10),(2,20),(10,100)]>

Assert failed.
Expected:<[(9,90),(7,70),(8,80),(6,60),(2,20),(1,10),(3,30),(5,50)]>
Actual:<[(9,90),(7,70),(8,80),(5,50),(2,20),(1,10),(3,30),(6,60)]>

Assert failed.
Expected:<[(6,60),(5,50),(4,40),(1,10),(2,20),(3,30)]>
Actual:<[(6,60),(5,50),(3,30),(1,10),(2,20),(4,40)]>

My insert function is :

void insert(KeyValueType const& keyValue)
    {

        size_t asize = size();
        if (asize == 0)
        {
            table.push_back(keyValue);
        }
        else
        {
            table.push_back(keyValue);
            for (int i = asize / 2 - 1; i >= 0; i--)
            {
                heapify(i);

            }
        }
    } 

and my heapify function is :

void heapify(size_t index)
    {
        auto previous_index = index;
        do
        {
            previous_index = index;
            auto largest = getLargestFromParentAndHisChildren(index);
            if (index != largest)
            {
                std::swap(table[index], table[largest]);
                index = largest;
            }
        } while (index != previous_index);
    }

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

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

发布评论

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

评论(1

聽兲甴掵 2025-02-11 07:15:18

您的循环以错误的价值开始。它应该是:

for (int i = (asize - 1) / 2; i >= 0; i--)

不是您的问题,而是:

  • 调用heapify并不是最快的弹出价值的方法,因为该功能还需要检查同胞值,这是不必要的。
  • i -将使此循环o(n),这不是您想要的复杂性。此操作应为o(logn),因此,在每次迭代中,i应该从子女跳到父母。
  • 此外,当实现上一点点时,当不再发生交换时,该循环可能会退出。

Your loop starts at the wrong value. It should be:

for (int i = (asize - 1) / 2; i >= 0; i--)

Not your question, but:

  • calling heapify is not the fastest way to bubble a value up, since that function needs to check also the sibling value, which is unnecessary.
  • i-- will make this loop O(n), which is not the complexity you would want for a heap. This operation should be O(logn), and so i should jump from child to parent, in each iteration.
  • Moreover, when the previous point is implemented, this loop could exit when no more swap occurs.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文