Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct nodo{char info; nodo* left,*right; nodo(int a=0, nodo*b=0,nodo*c=0){info=a; left=b; right=c;}};
- //PRE=(albero(r)corretto,k>0 e salto>0).
- int stampaConSalti(nodo* r, int k, int salto){
- int resto1,resto2;
- if(r){
- if(!r->left && !r->right){
- resto1 = k-1;
- if(!resto1){
- cout<<r->info<<" ";
- resto1 = salto;
- }//!resto1
- return resto1-1;
- }//!r->left && !r->right
- if(!r->left )
- resto1 = k - 1;
- else
- resto1 = stampaConSalti(r->left, k , salto);
- if(!resto1){
- cout<<r->info<<" ";
- k = salto;
- resto1 = k;
- }
- if(r->right){
- resto2 = stampaConSalti(r->right,resto1,salto);
- if(!resto2){
- cout<<r->info<<" ";
- k = salto;
- resto2 = k;
- }
- return resto2;
- }
- else
- return resto1-=1;
- }//r
- }//stampaConSalti
- /*POST=(se albero(r)contiene m nodi, allora, la funzione percorrei primi (rispetto all’ordine infisso) salto-1 nodi senza stamparli,
- poi stampail nodo seguente, poi percorrei prossimi k-1 senza stamparli, stampa il seguente e quando ha percorso tutti gli m nodi,
- restituisce l’intero che indica quanti nodi dovrebbe percorrere per fare la prossima stampa)*/
- nodo* parse(char*& A)
- {
- if(*A>='a' && *A<='z')
- {
- char p=*A;
- if(*(A+1) !='(') throw (1);
- A=A+2;
- nodo*l=parse(A);
- if(*A != ',') throw (2);
- A=A+1;
- nodo*r=parse(A);
- if(*A !=')') throw(3);
- A=A+1;
- return new nodo(p,l,r);
- }
- else
- if(*A=='_') {A=A+1;return 0;}
- else
- throw (4);
- }
- void infix_stampa(nodo* n){
- if(n){
- infix_stampa(n->left);
- cout<<n->info<<" ";
- infix_stampa(n->right);
- }
- }//infix_stampa
- main()
- try{
- char A[100],y,*q=A;
- int k;
- cin>>k;
- while(y!='$')
- {
- cin>>y;
- *q=y;
- q++;
- }
- q=A;
- nodo* R=parse(q);
- infix_stampa(R);
- cout<<endl;
- cout<<"start"<<endl;
- int a= stampaConSalti(R,k,k);//da fare
- cout<<endl<<"restituisce "<<a<<endl;
- cout<<"end"<<endl;
- }
- catch(int x)
- {
- switch(x)
- {
- case 1: cout<<"manca ("<<endl; break;
- case 2: cout<<"manca ," <<endl; break;
- case 3: cout<<"manca )"<<endl; break;
- case 4: cout<<"non albero"<<endl; break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement