Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #include<fstream>
- struct nodo{int info; nodo* left,*right;nodo(int a=0, nodo* b=0, nodo* c=0){info=a; left=b; right=c;}};
- int conta_nodi(nodo* R)
- {
- if(!R)
- return 0;
- if(!R->left && !R->right)
- return 1;
- return conta_nodi(R->left)+conta_nodi(R->right)+1;
- }
- nodo* insert(nodo*R, nodo* n)
- {
- if(!R)
- return n;
- if(conta_nodi(R->left)<=conta_nodi(R->right))
- R->left=insert(R->left, n);
- else
- R->right=insert(R->right, n);
- return R;
- }
- nodo* build_tree(nodo* R,int n_el)
- {
- if(!n_el)
- return R;
- int x;
- cin>>x;
- nodo* n=new nodo(x);
- nodo* z=insert(R,n);
- return build_tree(z, n_el-1);
- }
- void stampa(nodo *r)
- {
- if(r)
- {
- cout<<r->info<<'(';
- stampa(r->left);
- cout<<',';
- stampa(r->right);
- cout<<')';
- }
- else
- cout<< '_';
- }
- main()
- {
- int n_el;
- cin>>n_el;
- nodo* R=build_tree(0,n_el);
- stampa(R);
- cout<<endl<<"end"<<endl;
- }
Add Comment
Please, Sign In to add comment