-1

This is my traversal method:

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;
            }

This is the output that I'm having:

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

It's seems to be working correctly yet I'm having all these garbage values which I shouldn't have, I couldn't pinpoint the issue.

I suspect there is something wrong with the control structures, is my logic correct or should I've used else if statements ?

  • 1
    You should've implemented proper bounds checking. – Sam Varshavchik May 03 '18 at 16:57
  • 2
    Surely `hipa[(2*i)+1]` must go out of bounds when `i==(maxsize-1)` if `maxsize` refers to the size of `hipa` – Cory Kramer May 03 '18 at 16:57
  • To restate - you should include the declaration of `hipa[]` in your example as well as `maxsize`. It appears you are reading past the end of your array, but we cannot say for certain without that extra code. – Michael Dorgan May 03 '18 at 17:03

2 Answers2

0

I've fixed the issue by changing the for loop parameter from maxsize to heap_size. maxsize was supposed to be the capacity of the heap while heap_size is the current size of the heap. I've added the rest of the class declaration for the curious.

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];
    }
}
0

Even with the "fix" you posted in your answer, you're going to run into the same problem whenever heap_size >= (maxsize/2). You have to check the computed left and right child indexes. Corrected code:

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;
        }
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351