私は助けが必要です!構造の配列であるヒープを実行しています。再ヒープ ダウン アルゴリズムが間違っているようです。知っていることはすべて試しましたが、この種のことは、構造がなく、配列だけの場合に機能します。だからここにエラーがあります:
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'
そしてコード:
#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);
}