Advertisement
Guest User

Untitled

a guest
May 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 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. void riempi(int*P,int m)
  50. {
  51. if(m)
  52. {
  53. cin>>*P;
  54. riempi(P+1,m-1);
  55. }
  56. }
  57. void stampaL(nodo*a)
  58. {
  59. if(a)
  60. {
  61. cout<<a->info->info<<' ';
  62. stampaL(a->next);
  63. }
  64. else
  65. cout<<endl;
  66. }
  67.  
  68. nodo*concat(nodo*L1,nodo*L2)
  69. {
  70. if(!L1)
  71. return L2;
  72. if(!L2)
  73. return L1;
  74. if(!L1->next)
  75. {
  76. L1->next=L2;
  77. return L1;
  78. }
  79. L1->next=concat(L1->next,L2);
  80. return L1;
  81. }
  82.  
  83. //PRE=(albero(r)è ben formato, k>0, 1<=n<=k)
  84. nodo* B(nodoA*r, int k, int&n)
  85. {
  86. if(!r)
  87. return 0;
  88. nodo*right,*left=B(r->left,k,n);//chiamo a sinistra
  89. if(n==1)
  90. {
  91. n=k; //resetto n al valore k iniziale
  92. nodo*tmp=new nodo(r); //creo un nuovo nodo che punta a r
  93. left=concat(left,tmp); //aggiungo il nuovo nodo alla lista
  94. right=B(r->right,k,n); //chiamo a destra
  95. }
  96. else
  97. {
  98. n=n-1; //decremento n
  99. right=B(r->right, k,n); //chiamo a destra
  100. }
  101. return concat(left,right); //concateno la lista che ho trovato a sinistra con quella di destra
  102. }
  103. /*POST=(restituisce la lista concatenata i cui nodi puntano ai nodi dell’albero ordinati
  104. secondo l’ordine infisso,etale che il primo nodo della lista punti al nodo n-esimo di
  105. albero(r)secondo l’ordine infisso, e i successivi nodi puntano ad un nodo di albero(r)
  106. ogni k nodi sempre secondo l’ordine infisso) && ( n, 1<=n<=k, indica che si devono
  107. attraversare n-1 nodi prima di arrivare a quello che va puntato dal prossimo nodo della lista)*/
  108.  
  109. main()
  110. {
  111. int n,m,k;
  112. cout<<"start"<<endl;
  113. cin>> n;
  114. nodoA*R=buildtree(0,n);
  115. stampa_l(R);
  116. cout<<endl;
  117.  
  118. cin>> k;
  119. m=k;
  120. nodo*a=B(R,k,m); // da fare
  121. cout<<"lista:"<<endl;
  122. stampaL(a);
  123. cout<<"n="<<m<<endl;
  124. cout<<"end"<<endl;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement