遍历最小堆时获取垃圾值
Getting garbage values when traversing min-heap
这是我的遍历方法:
void heapTraversal(){
for(int i = 0; i<maxsize; i++){
cout << "Current value is : " << hipa[i] << endl;
if(hipa[(2*i)+1]){
cout << "Left child is : " << hipa[(2*i)+1] << endl;
}
else{
cout << "Left child does not exist" << endl;
}
if(hipa[(2*i)+2]){
cout << "Right child is : " << hipa[(2*i)+2] << endl;
}
else{
cout << "Right child does not exist" << endl;
}
这是我得到的输出:
Current value is : 7
Left child is : 9
Right child is : 17
Current value is : 9
Left child is : 15
Right child is : 12
Current value is : 17
Left child is : 25
Right child is : 22
Current value is : 15
Left child is : 1769234787
Right child does not exist
Current value is : 12
Left child does not exist
Right child does not exist
Current value is : 25
Left child does not exist
Right child is : 1852112910
Current value is : 22
Left child is : 1395618676
Right child is : 1701994856
它似乎工作正常,但我有所有这些我不应该有的垃圾值,我无法查明问题所在。
我怀疑控制结构有问题,我的逻辑是否正确,或者我应该使用 else if 语句吗?
我已经通过将 for 循环参数从 maxsize 更改为 heap_size 解决了这个问题。
maxsize 应该是堆的容量,而 heap_size 是堆的当前大小。为了好奇,我添加了 class 声明的其余部分。
class minHeap{
int *hipa;
int maxsize;
int heap_size;
public:
minHeap(int size);
void minHeapbuild(int );
int root(int i){
return (i-1)/2;
}
int left(int i){
return (2*i+1);
}
int right(int i){
return (2*i+2);
}
int extractRoot();
void decreaseKey(int i, int newKey);
void deleteKey(int key);
void addKey(int key);
int getRoot(){
return hipa[0];
}
}
即使您在回答中发布了 "fix",每当 heap_size >= (maxsize/2)
时,您都会 运行 遇到同样的问题。您必须检查计算出的左右子索引。更正后的代码:
void heapTraversal(){
for(int i = 0; i<heap_size; i++){
cout << "Current value is : " << hipa[i] << endl;
int left = (2*i)+1;
if(left < heap_size && hipa[left]){
cout << "Left child is : " << hipa[left] << endl;
}
else{
cout << "Left child does not exist" << endl;
}
int right = left+1;
if(right < heap_size && hipa[right]){
cout << "Right child is : " << hipa[right] << endl;
}
else{
cout << "Right child does not exist" << endl;
}
这是我的遍历方法:
void heapTraversal(){
for(int i = 0; i<maxsize; i++){
cout << "Current value is : " << hipa[i] << endl;
if(hipa[(2*i)+1]){
cout << "Left child is : " << hipa[(2*i)+1] << endl;
}
else{
cout << "Left child does not exist" << endl;
}
if(hipa[(2*i)+2]){
cout << "Right child is : " << hipa[(2*i)+2] << endl;
}
else{
cout << "Right child does not exist" << endl;
}
这是我得到的输出:
Current value is : 7
Left child is : 9
Right child is : 17
Current value is : 9
Left child is : 15
Right child is : 12
Current value is : 17
Left child is : 25
Right child is : 22
Current value is : 15
Left child is : 1769234787
Right child does not exist
Current value is : 12
Left child does not exist
Right child does not exist
Current value is : 25
Left child does not exist
Right child is : 1852112910
Current value is : 22
Left child is : 1395618676
Right child is : 1701994856
它似乎工作正常,但我有所有这些我不应该有的垃圾值,我无法查明问题所在。
我怀疑控制结构有问题,我的逻辑是否正确,或者我应该使用 else if 语句吗?
我已经通过将 for 循环参数从 maxsize 更改为 heap_size 解决了这个问题。 maxsize 应该是堆的容量,而 heap_size 是堆的当前大小。为了好奇,我添加了 class 声明的其余部分。
class minHeap{
int *hipa;
int maxsize;
int heap_size;
public:
minHeap(int size);
void minHeapbuild(int );
int root(int i){
return (i-1)/2;
}
int left(int i){
return (2*i+1);
}
int right(int i){
return (2*i+2);
}
int extractRoot();
void decreaseKey(int i, int newKey);
void deleteKey(int key);
void addKey(int key);
int getRoot(){
return hipa[0];
}
}
即使您在回答中发布了 "fix",每当 heap_size >= (maxsize/2)
时,您都会 运行 遇到同样的问题。您必须检查计算出的左右子索引。更正后的代码:
void heapTraversal(){
for(int i = 0; i<heap_size; i++){
cout << "Current value is : " << hipa[i] << endl;
int left = (2*i)+1;
if(left < heap_size && hipa[left]){
cout << "Left child is : " << hipa[left] << endl;
}
else{
cout << "Left child does not exist" << endl;
}
int right = left+1;
if(right < heap_size && hipa[right]){
cout << "Right child is : " << hipa[right] << endl;
}
else{
cout << "Right child does not exist" << endl;
}