Advertisement
Guest User

ImpossibleAlgoTrans

a guest
Jun 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct stack{
  5.     struct stack* Top;
  6.     void * elem;
  7. };
  8. typedef struct stack* Stack;
  9.  
  10. struct tree{
  11.     int K;
  12.     struct tree* Dx;
  13.     struct tree* Sx;
  14. };
  15. typedef struct tree* Tree;
  16.  
  17.  
  18. Stack pop(Stack S){
  19.     if(S != NULL){
  20.         Stack tmp=S;
  21.         S= S->Top;
  22.         free(tmp);
  23.     }
  24.     return S;
  25. }
  26.  
  27. Stack push(Stack S, void * E){
  28.     Stack TMP=NULL;
  29.     if( S == NULL ){
  30.         TMP=(Stack)malloc(sizeof(struct stack));
  31.         TMP->elem= E;
  32.         TMP->Top=NULL;
  33.     }
  34.     else{
  35.         TMP=(Stack)malloc(sizeof(struct stack));
  36.         TMP->elem= E;
  37.         TMP->Top= S;
  38.        
  39.     }
  40.     return TMP;
  41. }
  42.  
  43. void * top(Stack S){
  44.     if(S != NULL){
  45.         return S->elem;
  46.     }
  47.     return NULL;
  48. }
  49.  
  50. Tree addDx(Tree T, int K){
  51.     if(T == NULL){
  52.         T= (Tree)malloc(sizeof(struct tree));
  53.         T->K=K;
  54.         T->Dx= NULL;
  55.         T->Sx= NULL;
  56.     }
  57.     else{
  58.         T->Dx= addDx(T->Dx, K);
  59.     }
  60.     return T;
  61. }
  62.  
  63. Tree addSx(Tree T, int K){
  64.     if(T == NULL){
  65.         T= (Tree)malloc(sizeof(Tree));
  66.         T->K=K;
  67.         T->Dx= NULL;
  68.         T->Sx= NULL;
  69.     }
  70.     else{
  71.         T->Sx= addSx(T->Sx, K);
  72.     }
  73.     return T;
  74. }
  75.  
  76. void Cancella_Tree(Tree T){
  77.     printf("Sono stata invocata su %d!", T->K);
  78.     return ;
  79. }
  80.  
  81. int AlgoRic(Tree T, int h){
  82.     int ret=-1;
  83.     int s=0;
  84.     int d=0;
  85.     if (T != NULL){
  86.         if(T->Sx != NULL){
  87.             s= AlgoRic(T->Sx, h+1);
  88.         }
  89.         if(T->Dx != NULL){
  90.             d= AlgoRic(T->Dx, h+1);
  91.         }
  92.         if( s == 0 && d == 0){
  93.             ret=h;
  94.         }
  95.         else{
  96.             ret=s+d;
  97.         }
  98.     }
  99.     return ret;
  100. }
  101.  
  102. int AlgoIt(Tree T, int h){
  103.     Stack S_r=NULL, S_t=NULL, S_s=NULL, S_d=NULL, S_ir=NULL;
  104.     Tree currT=T;
  105.     Tree last=T;
  106.     Tree nextT=NULL;
  107.     int ret=-1;
  108.     int currH=h;
  109.     int nextH=0;
  110.     int s=0;
  111.     int d=0;
  112.     int prevs=0;
  113.     int prevd=0;
  114.    
  115.     while( currT != NULL || S_ir != NULL){
  116.         if(currT != NULL){
  117.            
  118.             if( currT->Sx != NULL){
  119.                 S_t=push(S_t, (void*) currT);
  120.                 S_ir=push(S_ir, (void*) 1);
  121.                 nextH=currH+1;
  122.                 nextT=currT->Sx;
  123.                 s=0;
  124.                 d=0;
  125.                 ret=-1;
  126.             }
  127.             else if( currT->Dx != NULL ){
  128.                 S_t=push(S_t, (void*) currT);
  129.                 S_ir=push(S_ir, (void*) 0);
  130.                 nextH=currH+1;
  131.                 nextT=currT->Dx;
  132.                 s=0;
  133.                 d=0;
  134.                 ret=-1;
  135.             }
  136.             else{
  137.                 nextT=NULL;
  138.                 s=0;
  139.                 d=0;
  140.                 ret=-1;
  141.                 nextH=currH+1;//3(NULL)
  142.             }
  143.         }
  144.         else{
  145.             int ir= top(S_ir);
  146.             S_ir=pop(S_ir);
  147.             currT=(Tree)top(S_t);
  148.             currH=currH-1;//2
  149.            
  150.             printf("FIRST|CURR:%d|S:%d|D:%d|H:%d\n",currT->K,s,d, currH);
  151.            
  152.                
  153.             if( s == 0 && d == 0){
  154.                 ret=currH;
  155.             }
  156.             else{
  157.                 ret=s+d;
  158.             }
  159.                 printf("RET:%d\n",ret);
  160.                
  161.             if(ir == 1){  
  162.                 s=ret;
  163.                 if(currT->Dx != NULL){ // scnedo a DX                    
  164.                     S_s=push(S_s,(void *) s);
  165.                     S_ir=push(S_ir, (void*) 0);
  166.                     nextT=currT->Dx;
  167.                     nextH=currH;
  168.                     s=0;
  169.                     d=0;
  170.                     ret=-1;
  171.                 }
  172.                 else{ //Risalgo da SX
  173.                     d= top(S_s);
  174.                     S_t=pop(S_t);
  175.                     nextT=NULL;
  176.                     nextH=currH;
  177.  
  178.                 }
  179.             }
  180.             else{ // Risalgo da DX
  181.                 d=ret;
  182.                 s=top(S_s);
  183.                 S_s=pop(S_s);
  184.                 S_t=pop(S_t);
  185.                 nextT=NULL;
  186.                 nextH=currH;
  187.             }
  188.             printf("SECOND|CURR:%d|S:%d|D:%d|H:%d\n\n",currT->K,s,d, currH);
  189.         }
  190.         currT=nextT;
  191.         currH=nextH;
  192.     }
  193.    
  194.     return ret;
  195. }
  196.  
  197. void visita(Tree T){
  198.     if(T != NULL){
  199.         visita(T->Sx);
  200.         visita(T->Dx);
  201.         printf("%d\n", T->K);
  202.     }
  203. }
  204.  
  205. int main(){
  206.     Tree t= NULL;
  207.     Tree t1= NULL;
  208.     t1= addDx(t1, 1);
  209.     t1= addDx(t1, 3);
  210.     t1= addDx(t1, 6);
  211.     t1= addDx(t1, 5);
  212.     t1= addSx(t1, 2);
  213.     t1= addSx(t1, 4);
  214.     // Secondo albero di test-----------------
  215.     t=addDx(t,1);
  216.     t=addSx(t,2);
  217.     t=addDx(t,3);
  218.     addSx(t->Dx,4);
  219.     int r=0;
  220.     printf("OUTPUT 1:\n");
  221.     r=AlgoRic(t, 1);
  222.     printf("R_RIC:%d\n", r);
  223.     printf("----EOF-----\n\n");
  224.     printf("OUTPUT 2:\n");
  225.     r=AlgoIt(t, 1);
  226.     printf("R_ITR:%d\n", r);
  227.     printf("----EOF-----\n\n");
  228.     visita(t);
  229.    
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement