Mam taką stertę:
#include <iostream>
#include <math.h> //uzywanie log( ) i pow(,) w printb ()
using namespace std;
void wstaw (int x);
void doGory ();
void naDol ();
void printb ();
int pobierz();
int sterta[100];
int l=0; //ilosc elementow
void main()
{
char c='x';
int a;
int i;
while (c!='k')
{cout<<"dodaj d, pobierz p, koniec k, print e, drzewo b \n";
cin>>c;
switch (c) {
case 'd':
cout<<"dodaj liczbe ";
cin>>a;
wstaw (a);
break;
case 'p':
cout<<"------------liczba ze strety----------"<<endl;
// cout<<"l="<<l<<endl;
if (l==0) {cout<<"sterta pusta"<<endl;}
if (l!=0) {cout<<pobierz()<<endl;}
cout<<"--------------------------------------"<<endl;
break;
case 'e':
cout<<"*****************************************"<<endl;
for(i=1; i<=l; i++) cout<<sterta[i]<<" ";
cout<<endl;
cout<<"*****************************************"<<endl;
break;
case 'b':
printb ();
break;
default:
break;
}
}
}
//**********************************************
void wstaw (int x) {
sterta[++l]=x;
doGory(); }
//**********************************************
void doGory () {
int temp=sterta[l];
int n=l;
while ((n!=1)&&(sterta[n/2]<=temp))
{
sterta[n]=sterta[n/2];
n=n/2;
}
sterta[n]=temp; }
//**********************************************
int pobierz() {
if (l>=1) {
int x=sterta[1];
sterta[1]=sterta[l--];
naDol();
return x;
}
cout<<"sterta pusta"<<endl;
return 0; }
//**********************************************
void naDol () {
int i=1;
while (1) {
int p=i; // lewy potomek wezla 'i'=p, prawy=p+l
if (p<l) break;
if ((p+1<=l) && sterta[p] < sterta [p+1]) p++; // prawy potomek istnieje
// przesuwamy sie do nast?pnego elementu
if (sterta [i] >= sterta[p]) break;
int temp=sterta[p]; // zamiana elementow
sterta[i]=sterta[p];
temp=sterta[i];
p=i; } }
//**********************************************
//**********************************************
//**********************************************
void printb ()
{
int ll1, j,i,kk,n,step,n2;
cout<<"*****************************************"<<endl;
//ll1=(log((double)l) / log((double)2))+1;
ll1=(int)((log((double)l) / log((double)2))+1);
//ll1 -liczba urowniej
//cout<<"ll1="<<ll1<<endl;
//ll=((ll1)+1)/2+1;
//n=pow((double)2,(double)ll1);
n=(int)pow((double)2,(double)ll1);
//cout<<"n="<<n<<endl;
n2=n/2;
step=n;
for(i=1; i<=l; i*=2)
{
for (kk=1; kk<=n2; kk++) cout<<" ";
for(j=i; ((j<=l) && (j<2*i)); j++)
{
cout<<sterta[j];
for (kk=1; kk<=step; kk++) cout<<" ";
}
cout<<endl;
n2=n2/2;
step=step/2;
cout<<endl;
}
//cout<<endl;
cout<<"*****************************************"<<endl;
}
I muszę ją przerobić żeby działała odwrotnie - żeby po dodaniu liczb do sterty i ich pobraniu, program wyświetlił je w tej samej kolejności, co pobrał, drzewo też ma tak działać i print e.