다음은 더 큰 값을 버블 링하지 않는 코드입니다. 어느 한 가지 도움을 줄 수 있습니까? 문제는 카운트 ++에서 다소 있습니다.힙에 노드를 삽입하는 동안 거품을 사용하는 방법?
#include<iostream>
using namespace std;
class heap{
public:
int count;
heap(int c)
{
count=c;
}
int Arr[10];
void insert(int num);
void deletemax();
void print();
};
void heap::insert(int num){
if(count==10){
cout<<"Heap full\n";
exit(1);
}
else{
Arr[count]=num;
count++; //The real problem arises here that the compiler adds 1 to count and when the code moves ahead it sets position var to count++ value and tries to compare a value at Arr[POS] with its parent whereas there is no value at this place set uptill.
}
int POS=count;
while(Arr[POS]>Arr[(POS-1)/2]){
int temp;
temp=Arr[POS];
Arr[(POS-1)/2]=temp;
POS=(POS-1)/2;
}
}
void heap::print(){
for(int i=0; i<10; i++){
cout<<Arr[i]<<endl;
}
}
int main(){
heap h(0);
int a;
int b=0;
while(b<10){
cout<<"Insert node in heap\n";
cin>>a;
h.insert(a);
b++;
}
h.print();
return 0;
}
위로 올린 서식을 수정하십시오. –
당신이있는 동안 질문을 시도하십시오. –
나는 내 눈을 아프기까지 이걸 보지 않는다. 서식을 수정하십시오. 다른 한편으로 이것은 실제 질문이없는 코드의 큰 벽처럼 보이므로 어쨌든 나는 보지 않을 것입니다. –