堆上的 max_heapify 过程
当我运行时,我有这些程序,
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将维基百科文章中的伪代码翻译成 C++ 代码,但您不小心更改了逻辑。在维基百科文章中,您会注意到递归仅有条件发生:也就是说,
如果最大≠ i
翻译成 C++,这应该是这样的:
再次注意对
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
Translated into C++, this should read something like:
Again, notice that the recursive call to
Max_Heapify
only happens conditionally, whenlargest != i
. In your code, you recursively callMax_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.您没有终止递归的基本情况,所以我想您最终会遇到堆栈溢出。想一想什么时候你可以知道你已经完成了,这样你就可以优雅地退出该功能。
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.
也许您不应该总是尾部递归地调用
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
.函数
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.
这是我写的版本,你可以看一下。
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