C++ 堆数组不工作
我正在编写一个程序,从一个普通的数组构建一个堆数组,但它根本不起作用。 我们必须使用这个sudo代码,我用它来编写我的rebuildHeap函数,但我不知道我做错了什么。有人能发现错误吗C++ 堆数组不工作,c++,heap,binary-heap,C++,Heap,Binary Heap,我正在编写一个程序,从一个普通的数组构建一个堆数组,但它根本不起作用。 我们必须使用这个sudo代码,我用它来编写我的rebuildHeap函数,但我不知道我做错了什么。有人能发现错误吗 rebuildHeap is used after the replacement node has taken the place of root rebuildHeap(heap, index, lastIndex) 1 if (index * 2 + 1 <= lastIndex) 1 lef
rebuildHeap is used after the replacement node has taken the place of root
rebuildHeap(heap, index, lastIndex)
1 if (index * 2 + 1 <= lastIndex)
1 leftKey = heap[index*2+1].key
2 if (index * 2 + 2 > lastIndex)
1 largeChildIndex = index * 2 + 1
3 else
1 rightKey = heap[index*2+2].key
2 if (leftKey > rightKey)
1 largeChildIndex = index * 2 + 1
3 else
1 largeChildIndex = index * 2 + 2
4 end if
4 end if
5 if (heap[index].key < heap[largeChildIndex].key)
1 swap (heap[index], heap[largeChildIndex])
2 rebuildHeap(heap, largeChildIndex, lastIndex)
6 end if
2 end if
rebuildHeap在替换节点取代根节点后使用
重建EAP(堆、索引、lastIndex)
1如果(索引*2+1最后索引)
1 largeChildIndex=索引*2+1
3其他
1 rightKey=heap[index*2+2].key
2如果(左键>右键)
1 largeChildIndex=索引*2+1
3其他
1大儿童指数=指数*2+2
4如果结束
4如果结束
5 if(heap[index].key
这是我的密码。。首先我创建一个int数组并存储一些随机值,然后运行createheap函数调用rebuildHeap,直到堆数组完成
已编辑,已删除数组大小。
void rebuildHeap(int heap[], int index, int lastindex) {
int leftkey = 0 ;
int largeChildIndex = 0;
int rightkey = 0;
cout << endl;
if (heap[index*2+1] <= heap[lastindex])
{
leftkey = heap[index*2+1];
cout <<" index : " << index*2+1 << " leftkey " << leftkey << endl;
cout <<" index : " << lastindex << " heap[lastindex] = " << heap[lastindex] << endl;
if ((heap[index * 2+ 2]) > heap[lastindex])
largeChildIndex = (index* 2) +1;
else
{
rightkey = heap[index*2+2];
if (leftkey > rightkey)
largeChildIndex = index * 2 +1;
else
largeChildIndex = index*2+2;
}
}
if (heap[index] < heap[largeChildIndex]) {
swap(heap[index], heap[largeChildIndex]);
rebuildHeap(heap, largeChildIndex, lastindex);
}
}
void swap (int &a, int &b) {
int temp = b;
b = a;
a = temp;
}
int main(int argc, const char * argv[])
{
int a[] = {3, 7, 12, 15, 18, 23, 4, 9, 11, 14, 19, 21};
int length = (sizeof(a)/sizeof(a[0]));
for (int i = length/2-1; i >= 0; i-- ) {
rebuildHeap(a, i, length-1);
cout << " i " << i;
}
for (int i = 0; i < length; i++) {
cout << endl<< a[i] << endl;
}
};
void rebuildHeap(int heap[],int index,int lastindex){
int leftkey=0;
int-largeChildIndex=0;
int rightkey=0;
cout首先,我想强调的是,您对伪代码的翻译是错误的,所以我修复了它
#include <iostream>
using namespace std;
void rebuildHeap(int *heap, int index, int lastindex)
{
int leftkey = 0 ;
int largeChildIndex = 0;
int rightkey = 0;
if ((index*2 + 1) <= lastindex)
{
leftkey = heap[index*2+1];
if ((index*2 + 2) > lastindex)
largeChildIndex = index*2 +1;
else
{
rightkey = heap[index*2+2];
if (leftkey > rightkey)
largeChildIndex = index*2+1;
else
largeChildIndex = index*2+2;
}
}
else
{
return;
}
if (heap[index] < heap[largeChildIndex]) {
swap(heap[index], heap[largeChildIndex]);
rebuildHeap(heap, largeChildIndex, lastindex);
}
}
void swap (int &a, int &b) {
int temp = b;
b = a;
a = temp;
}
int main(int argc, const char * argv[])
{
int a[] = {3, 7, 12, 15, 18, 23, 4, 9, 11, 14, 19, 21};
int length = sizeof(a)/sizeof(a[0]);
//create heap
for (int i = (length/2-1); i >= 0; i --)
{
rebuildHeap(a, i, length-1);
}
//prints the heap array
for (int i = 0; i < length; i++) {
cout << a[i] << " ";
}
return 0;
}
#包括
使用名称空间std;
void rebuildHeap(int*堆、int索引、int lastindex)
{
int leftkey=0;
int-largeChildIndex=0;
int rightkey=0;
如果((索引*2+1)上次索引)
大子女指数=指数*2+1;
其他的
{
rightkey=heap[index*2+2];
如果(左键>右键)
大子女指数=指数*2+1;
其他的
大孩子指数=指数*2+2;
}
}
其他的
{
返回;
}
if(heap[index]=0;i--)
{
重建EAP(a,i,长度-1);
}
//打印堆数组
for(int i=0;i 行int size=(sizeof(&heap));
似乎不正确。size
应该是什么?它将数组的长度存储在那里。数组中的元素数不起作用。您必须将其作为参数传递给函数。sizeof(&heap)
将等于您环境中指针的大小——大多数情况下为4或8。但这是一个递归函数,如何工作?您是否尝试使用伪函数(而不是“sudo”)按原样编码,不引入大小
?这正是我要找的Maxheap,谢谢你,它是完美的。必须再次检查算法才能知道它是如何工作的。如果你选择我的答案作为正确答案并投票,我将不胜感激。干杯:DIf你想看看算法是如何工作的,就这样更改它:…我f(heap[index]