سؤال

I need help! I am doing a heap that is a array of structures, my reheap down algorithm seems to be wrong, I have tried everything that I know, and this kind of thing works when there is no structure, just the array. So here is the error:

main.cpp: In function 'void reheapDown(hitna**, int, int&)':
main.cpp:88: error: invalid types 'hitna**[std::ios_base& ()(std::ios_base&)]' for array subscript
main.cpp:89: error: invalid conversion from 'std::ios_base& (*)(std::ios_base&)' to 'int'

and the code:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct hitna{
    string ime;
    int broj;
};

void unos(hitna *heap[50], int &last);
void reheapUp(hitna *heap[], int &last);
void exchange(hitna *&x, hitna *&y);
void reheapDown(hitna *heap[],int parent,  int &last);
void obrada(hitna *heap[], int &last);

int main(){

hitna *heap[50]={0};
int last = -1;
int odg;
do{
    cout<<"- - - I Z B O R N I K - - -";
    cout<<"\n\n1)Unos pacijenata\n";
    cout<<"2)Obrada pacijenata\n";
    cout<<"3)Ispis pacijenata\n";
    cout<<"4)Izlaz\n";

    cout<<"\nOdabir: ";
    cin>>odg;

    switch(odg){
        case 1:          
           unos(heap, last);
            break;
        case 2: 
            obrada(heap, last);
        break;
        case 3:
            for (int i=0;i<=last;i++){
                cout<<heap[i]->ime<<" "<<heap[i]->broj<<endl;
            }
        case 4:break;
        default:
            break;

    };
}while(odg!=4);

return 0;
}

void exchange(hitna *&x, hitna *&y){
    hitna *t = x;
    x=y;
    y=t;

}

void unos(hitna *heap[50], int &last){
    last++;
    heap[last]=new hitna;
    cout<<"IME: ";cin>>heap[last]->ime;
    cout<<"BROJ: ";cin>>heap[last]->broj;

    reheapUp(heap, last);


}
void reheapUp(hitna *heap[], int &last){
    int parent=(last-1)/2;
    if (heap[parent]->broj < heap[last]->broj){
        exchange(heap[parent], heap[last]);
        reheapUp(heap, parent);}

}  



void reheapDown(hitna *heap[],int parent, int &last){
int left = 2 * parent + 1;

if(left<=last){
    int child = left;
    if(left+1<=last) int right = child+1;

    if ((heap[right]->broj) > (heap[left]->broj)) //THIS IS WRONG
            {child = right;}                        //THIS ALSO

    if (heap[parent]->broj < heap[child]->broj){
        exchange(heap[parent], heap[child]);
        reheapDown(heap, child, last);

    }
}

}

void obrada(hitna *heap[], int &last){
    if(last<0) {return;}
    exchange(heap[0], heap[last]);
    last--;
    reheapDown(heap, 0, last);
}
هل كانت مفيدة؟

المحلول

Your scope for right is incorrect.

if(left+1<=last) int right = child+1;

The identifier right only exists inside this if-block, so you cannot use it outside. You need to decide what is correct in this case. If that condition left+1<=last is not true, should you still execute the remaining code in the function? If so, you need to declare right outside (at the same scope level as left), and give it an appropriate value.

I expect you intended something like this:

if(left+1 <= last) {
    int child = left;
    int right = left+1;

    if ((heap[right]->broj) > (heap[left]->broj))
        child = right;

    if (heap[parent]->broj < heap[child]->broj) {
        exchange(heap[parent], heap[child]);
        reheapDown(heap, child, last);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top