Advertisement
Guest User

Untitled

a guest
May 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct nodoA{int info; nodoA* left,*right; nodoA(int a=0, nodoA*b=0,nodoA*c=0){info=a; left=b; right=c;}};
  4. struct nodo{nodoA* info; nodo* next; nodo(nodoA* a=0, nodo*b=0){info=a; next=b;}};
  5.  
  6. void stampa_l(nodoA *r)
  7. {
  8. if(r)
  9. {
  10. cout<<r->info<<'(';
  11. stampa_l(r->left);
  12. cout<<',';
  13. stampa_l(r->right);
  14. cout<<')';
  15. }
  16. else
  17. cout<< '_';
  18. }
  19. int conta_n(nodoA*r)
  20. {
  21. if(!r) return 0;
  22. else
  23. return conta_n(r->left)+conta_n(r->right)+1;
  24. }
  25.  
  26. nodoA* insert(nodoA*r, int y)
  27. {
  28. if(!r) return new nodoA(y);
  29.  
  30. if(conta_n(r->left)<=conta_n(r->right))
  31. r->left=insert(r->left,y);
  32. else
  33. r->right=insert(r->right,y);
  34. return r;
  35. }
  36.  
  37. nodoA* buildtree(nodoA*r, int dim)
  38. {
  39. if(dim)
  40. {
  41. int z;
  42. cin>>z;
  43. nodoA*x=insert(r,z);
  44. return buildtree(x,dim-1);
  45. }
  46. return r;
  47. }
  48.  
  49.  
  50. void riempi(int*P,int m)
  51. {
  52. if(m)
  53. {
  54. cin>>*P;
  55. riempi(P+1,m-1);
  56. }
  57. }
  58.  
  59. void stampaL(nodo*a)
  60. {
  61. if(a)
  62. {
  63. cout<<a->info->info<<' ';
  64. stampaL(a->next);
  65. }
  66. else
  67. cout<<endl;
  68. }
  69. nodo* match(nodoA* r, int* P, int dimP){
  70. if(!dimP) return new nodo(r);
  71. if(!r) return 0;
  72. if(r->info==*P) {
  73. if(dimP>1){
  74. nodo*left = match(r->left, P+1, dimP-1);
  75. if(left) return new nodo(r, left);
  76. nodo* right = match(r->right, P+1, dimP-1);
  77. if(right) return new nodo(r, right);
  78. }
  79. else return new nodo(r, 0);
  80. }
  81. nodo*left = match(r->left, P, dimP);
  82. if(left) return left;
  83. nodo* right = match(r->right, P, dimP);
  84. if(right) return right;
  85. return 0;
  86. }
  87. main()
  88. {
  89. int n,m;
  90. cout<<"start"<<endl;
  91. cin>> n;
  92. nodoA*R=buildtree(0,n);
  93. stampa_l(R);
  94. cout<<endl;
  95. int P[50];
  96. cin>> m;
  97. riempi(P,m);
  98. nodo*a=match(R,P,m);
  99. if(a)
  100. stampaL(a);
  101. else
  102. cout<<"no match found"<<endl;
  103. cout<<"end"<<endl;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement