Advertisement
Guest User

29-5-2018 alberi ricorsione prova di corr.

a guest
Dec 10th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. //29-05-2018
  2. #include<iostream>
  3. using namespace std;
  4. struct nodoA{int info; nodoA* left,*right; nodoA(int a=0, nodoA*b=0,nodoA*c=0){info=a; left=b; right=c;}};
  5. struct nodo{nodoA* info; nodo* next; nodo(nodoA* a=0, nodo*b=0){info=a; next=b;}};
  6.  
  7. void stampa_l(nodoA *r)
  8. {
  9. if(r)
  10. {
  11. cout<<r->info<<'(';
  12. stampa_l(r->left);
  13. cout<<',';
  14. stampa_l(r->right);
  15. cout<<')';
  16. }
  17. else
  18. cout<< '_';
  19. }
  20. int conta_n(nodoA*r)
  21. {
  22. if(!r) return 0;
  23. else
  24. return conta_n(r->left)+conta_n(r->right)+1;
  25. }
  26.  
  27. nodoA* insert(nodoA*r, int y)
  28. {
  29. if(!r) return new nodoA(y);
  30.  
  31. if(conta_n(r->left)<=conta_n(r->right))
  32. r->left=insert(r->left,y);
  33. else
  34. r->right=insert(r->right,y);
  35. return r;
  36. }
  37.  
  38. nodoA* buildtree(nodoA*r, int dim)
  39. {
  40. if(dim)
  41. {
  42. int z;
  43. cin>>z;
  44. nodoA*x=insert(r,z);
  45. return buildtree(x,dim-1);
  46. }
  47. return r;
  48. }
  49.  
  50.  
  51. void riempi(int*P,int m)
  52. {
  53. if(m)
  54. {
  55. cin>>*P;
  56. riempi(P+1,m-1);
  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.  
  70.  
  71. void concat(nodo*& L, nodo* daAggiungere){
  72.  
  73. if(!L && !daAggiungere){
  74. return;
  75. }
  76.  
  77. if(!L){
  78. L=daAggiungere;
  79. return;
  80. }
  81. if(!daAggiungere){
  82. return;
  83. }
  84.  
  85. if(L->next != 0){
  86. concat(L->next, daAggiungere);
  87. }else{
  88. L->next = daAggiungere;
  89. return;
  90. }
  91. }
  92.  
  93. nodo* match(nodoA*r, int*P, int dimP){
  94. nodo* n, *d, *s;
  95. n=0;
  96. d=0;
  97. s=0;
  98.  
  99. if(!r){ //caso base #1
  100. return 0;
  101. }
  102.  
  103.  
  104. if(*P == r->info){ //match
  105. n = new nodo(r, 0);
  106. P=P+1;
  107. dimP=dimP-1;
  108. }
  109.  
  110. if(r->left==0 && r->right==0){ //caso base #2
  111. if(dimP==0){
  112. return n;
  113. }else{
  114. return 0;
  115. }
  116. }
  117.  
  118. if(dimP>0){
  119. s = match(r->left, P, dimP); //ric #1
  120.  
  121. if(s){
  122. dimP=dimP-1;
  123. P=P+1;
  124. concat(n,s);
  125. }
  126.  
  127.  
  128. d = match(r->right, P, dimP); //ric #2
  129.  
  130. if(d){
  131. dimP=dimP-1;
  132. P=P+1;
  133. concat(n,d);
  134. }
  135.  
  136. if(!s && !d){ //caso base #3
  137. return 0;
  138. }
  139. }
  140.  
  141. return n;
  142. }
  143.  
  144. /*
  145. PROVA DI CORRETTEZZA di match
  146.  
  147. Caso base #1 => POST
  148. r è un albero vuoto => non esistono cammini sui quali matchare e di conseguenza nessuna lista costruita dai nodi matchati è stata costruita => restituisce 0 (ovvero nessun match completo trovato)
  149.  
  150. Caso base #2 => POST
  151. r è una foglia (ovvero i suoi cambi left e right puntano a nullptr) => se la dimP==0 allora ha trovato un match completo su quel cammino e dunque restituisce il nodo(r,0), altrimenti restituisco 0 (ovvero nessun match completo trovato)
  152.  
  153. Caso base #3 => POST
  154. s e d sono contemporaneamente nullptr => la ricerca sul sottoalbero sinistro e destro non ha prodotto match completi sul/sui cammino/cammini => restituisco 0 (ovvero nessun match completo trovato)
  155.  
  156.  
  157. Calcolo:
  158.  
  159. if(*P == r->info == true) ho trovato un match,
  160. creo un nuovo nodo* n(r,0)
  161. decremento la dimensione dell'array P (dimP) e sposto di uno (in avanti) il puntatore all'array P
  162.  
  163. if(dimP>0 == true) il mio array P ha ancora elementi da matchare
  164.  
  165. s = match(r->left, P, dimP);
  166. //passo induttivo, dimostro la PRE_ric #1
  167.  
  168. (1)albero r è un albero ben formato non essendo stato in alcun modo modificato => anche r->left è un albero ben formato
  169. (2)P ha dimP elementi => avendo spostato P e decrementato dimP solo in caso di match => ancche P_ric ha dimP_elementi
  170. (3)dimP>0 in quanto se fosse stata <=0 non avrebbe eseguito la chiamata ricorsiva => anche dimP_ric>0
  171.  
  172. essendo valida la PRE_ric assumo sia valida anche la POST_ric
  173.  
  174. if(s==true) se s non è un puntatore vuoto(ovvero la chiamata ricorsiva ha restituito un nodo non vuoto che dunque ha matchato)
  175. decremento la dimensione dell'array P (dimP) e sposto di uno (in avanti) il puntatore all'array P
  176. concat(n,s) faccio puntare l'ultimo nodo di n ad s
  177.  
  178.  
  179. d = match(r->right, P, dimP);
  180. //passo induttivo, dimostro la PRE_ric #1
  181.  
  182. (1)albero r è un albero ben formato non essendo stato in alcun modo modificato => anche r->right è un albero ben formato
  183. (2)P ha dimP elementi => avendo spostato P e decrementato dimP solo in caso di match => ancche P_ric ha dimP_elementi
  184. (3)dimP>0 in quanto se fosse stata <=0 non avrebbe eseguito la chiamata ricorsiva => anche dimP_ric>0
  185.  
  186. essendo valida la PRE_ric assumo sia valida anche la POST_ric
  187.  
  188. if(s==true) se s non è un puntatore vuoto(ovvero la chiamata ricorsiva ha restituito un nodo non vuoto che dunque ha matchato)
  189. decremento la dimensione dell'array P (dimP) e sposto di uno (in avanti) il puntatore all'array P
  190. concat(n,d) faccio puntare l'ultimo nodo di n ad d
  191.  
  192.  
  193.  
  194. => ha trovato un match completo sul cammino corrispondente => restituisce n, ovvero la lista i cui nodi puntano ai nodi matchati nel cammino
  195.  
  196. => POST
  197. */
  198. main()
  199. {
  200. int n,m;
  201. cout<<"start"<<endl;
  202. cin>> n;
  203. nodoA*R=buildtree(0,n);
  204. stampa_l(R);
  205. cout<<endl;
  206. int P[50];
  207. cin>> m;
  208. riempi(P,m);
  209. nodo*a=match(R,P,m);
  210. if(a)
  211. stampaL(a);
  212. else
  213. cout<<"no match found"<<endl;
  214. cout<<"end"<<endl;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement