堆上的 max_heapify 过程

发布于 2024-10-02 17:06:12 字数 1239 浏览 6 评论 0原文

当我运行时,我有这些程序,

#include <iostream>
using namespace std;

int parent(int i ){
    return  i/2;
}

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

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

int a[]={ 27,17,3,16,10,1,5,7,12,4,8,9,10};
int n=sizeof(a)/sizeof(int);

void max_Heapify(int i){
    int largest=0;
    int l=left(i);
    int r=right(i);
    if(l<=n && a[l]>a[i]){
        largest=l;
    }
    else{
        largest=i;
    }
    if(r< n && a[r]>a[largest]){
        largest=r;
    }
    if (largest!=i){
        int t=a[i];
        a[i]=a[largest];
        a[largest]=t;
    }
    max_Heapify(largest);
}

int main(){
     max_Heapify(2);
     for (int i=0;i<n;i++){
         cout<<a[i]<<"  ";
     }    
return 0;
}

它编译得很好,但是在运行停止后,它异常运行,为什么?请帮忙 查看来自 Wikipedia 的代码

Max-Heapify[2](A, i):
 left ← 2i
 right ← 2i + 1
 largest ← i
 if left ≤ heap-length[A] and A[left] > A[i] then:
 largest ← left
 if right ≤ heap-length[A] and A[right] > A[largest] then:
 largest ← right
 if largest ≠ i then:
 swap A[i] ↔ A[largest]
 Max-Heapify(A, largest)

i have these procedure

#include <iostream>
using namespace std;

int parent(int i ){
    return  i/2;
}

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

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

int a[]={ 27,17,3,16,10,1,5,7,12,4,8,9,10};
int n=sizeof(a)/sizeof(int);

void max_Heapify(int i){
    int largest=0;
    int l=left(i);
    int r=right(i);
    if(l<=n && a[l]>a[i]){
        largest=l;
    }
    else{
        largest=i;
    }
    if(r< n && a[r]>a[largest]){
        largest=r;
    }
    if (largest!=i){
        int t=a[i];
        a[i]=a[largest];
        a[largest]=t;
    }
    max_Heapify(largest);
}

int main(){
     max_Heapify(2);
     for (int i=0;i<n;i++){
         cout<<a[i]<<"  ";
     }    
return 0;
}

when i run it compiles fine but after run stops ubnormaly it's running why? please help
look at this code

Max-Heapify[2](A, i):
 left ← 2i
 right ← 2i + 1
 largest ← i
 if left ≤ heap-length[A] and A[left] > A[i] then:
 largest ← left
 if right ≤ heap-length[A] and A[right] > A[largest] then:
 largest ← right
 if largest ≠ i then:
 swap A[i] ↔ A[largest]
 Max-Heapify(A, largest)

From Wikipedia.

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

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

发布评论

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

评论(5

君勿笑 2024-10-09 17:06:12

您将维基百科文章中的伪代码翻译成 C++ 代码,但您不小心更改了逻辑。在维基百科文章中,您会注意到递归仅有条件发生:也就是说,如果最大≠ i

if largest ≠ i then:
    swap A[i] ↔ A[largest]
    Max-Heapify(A, largest)

翻译成 C++,这应该是这样的:

if (largest != i) {
  swap(a[i], a[largest]);
  Max_Heapify(largest);
}

再次注意对 Max_Heapify 的递归调用仅在 largest != i有条件发生。在您的代码中,您无条件地递归调用Max_Heapify,这意味着无论如何您都会继续递归。显然你的程序会因堆栈溢出而崩溃。与迭代不同,您不能无限递归,因为您很快就会耗尽堆栈空间。

You translated the pseudo-code from the Wikipedia article into C++ code, but you accidentally altered the logic. In the Wikipedia article, you'll notice that the recursion only happens conditionally: that is, if largest ≠ i

if largest ≠ i then:
    swap A[i] ↔ A[largest]
    Max-Heapify(A, largest)

Translated into C++, this should read something like:

if (largest != i) {
  swap(a[i], a[largest]);
  Max_Heapify(largest);
}

Again, notice that the recursive call to Max_Heapify only happens conditionally, when largest != i. In your code, you recursively call Max_Heapify unconditionally, meaning that you keep recursing no matter what. So obviously your program is going to crash with a stack overflow. Unlike iteration, you can't recurse infinitely because you quickly run out of stack space.

昔日梦未散 2024-10-09 17:06:12

您没有终止递归的基本情况,所以我想您最终会遇到堆栈溢出。想一想什么时候你可以知道你已经完成了,这样你就可以优雅地退出该功能。

You don't have a base case to terminate the recursion, so I imagine you're eventually getting a stack overflow. Think about when you can tell that you're done so you can gracefully fall out of the function.

掌心的温暖 2024-10-09 17:06:12

也许您不应该总是尾部递归地调用max_Heapify,而是在达到排序堆的底部大小时返回...发生的情况是您的程序堆栈溢出。

另外,请查看std::swap

Perhaps you shouldn't always call max_Heapify tail-recursively, but instead return when you've hit the bottom size of the sorting heap... What happens is that your program runs out of stack.

Also, check out std::swap.

屌丝范 2024-10-09 17:06:12

函数 max_Heapify 总是无限递归。您看到堆栈溢出(小 s,小 o)。

如果您在调试器中单步执行代码,这种情况很快就会显而易见。

The function max_Heapify always recurses infinitely. You are seeing a stack overflow (small s, small o).

If you step through your code in the debugger, this sort of thing will quickly be obvious.

豆芽 2024-10-09 17:06:12

这是我写的版本,你可以看一下。

http://code.google.com/p/clibutils/source /browse/src/c_heap.c

Here is the version I wrote, you can take a look.

http://code.google.com/p/clibutils/source/browse/src/c_heap.c

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