Guest User

2_18_5_2016

a guest
Jan 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. #include<fstream>
  4. struct nodo{int info; nodo* left,*right;nodo(int a=0, nodo* b=0, nodo* c=0){info=a; left=b; right=c;}};
  5.  
  6. int conta_nodi(nodo* R)
  7. {
  8. if(!R)
  9. return 0;
  10. if(!R->left && !R->right)
  11. return 1;
  12. return conta_nodi(R->left)+conta_nodi(R->right)+1;
  13. }
  14.  
  15. nodo* insert(nodo*R, nodo* n)
  16. {
  17. if(!R)
  18. return n;
  19. if(conta_nodi(R->left)<=conta_nodi(R->right))
  20. R->left=insert(R->left, n);
  21. else
  22. R->right=insert(R->right, n);
  23. return R;
  24. }
  25.  
  26. nodo* build_tree(nodo* R,int n_el)
  27. {
  28. if(!n_el)
  29. return R;
  30. int x;
  31. cin>>x;
  32. nodo* n=new nodo(x);
  33. nodo* z=insert(R,n);
  34. return build_tree(z, n_el-1);
  35. }
  36.  
  37. void stampa(nodo *r)
  38. {
  39. if(r)
  40. {
  41. cout<<r->info<<'(';
  42. stampa(r->left);
  43. cout<<',';
  44. stampa(r->right);
  45. cout<<')';
  46. }
  47. else
  48. cout<< '_';
  49. }
  50. main()
  51. {
  52. int n_el;
  53. cin>>n_el;
  54.  
  55. nodo* R=build_tree(0,n_el);
  56. stampa(R);
  57. cout<<endl<<"end"<<endl;
  58. }
Add Comment
Please, Sign In to add comment