Advertisement
Guest User

BuggedCode

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.11 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.         S->Top=NULL;
  39.        
  40.     }
  41.     return TMP;
  42. }
  43.  
  44. void * top(Stack S){
  45.     if(S != NULL){
  46.         return S->elem;
  47.     }
  48.     return NULL;
  49. }
  50.  
  51. Tree addDx(Tree T, int K){
  52.     if(T == NULL){
  53.         T= (Tree)malloc(sizeof(struct tree));
  54.         T->K=K;
  55.         T->Dx= NULL;
  56.         T->Sx= NULL;
  57.     }
  58.     else{
  59.         T->Dx= addDx(T->Dx, K);
  60.     }
  61.     return T;
  62. }
  63.  
  64. Tree addSx(Tree T, int K){
  65.     if(T == NULL){
  66.         T= (Tree)malloc(sizeof(Tree));
  67.         T->K=K;
  68.         T->Dx= NULL;
  69.         T->Sx= NULL;
  70.     }
  71.     else{
  72.         T->Sx= addSx(T->Sx, K);
  73.     }
  74.     return T;
  75. }
  76.  
  77. Tree AlgoRic(Tree T, int L){
  78.     Tree X=NULL;
  79.     if( T != NULL && L >= 0){
  80.         X= (Tree)malloc(sizeof(struct tree));
  81.         X->K= T->K;
  82.         if ( T->Dx != NULL ){
  83.             X->Dx= AlgoRic(T->Dx, L-1);
  84.         }
  85.         if( T->Sx != NULL ){
  86.             X->Sx= AlgoRic(T->Sx, L-1);
  87.         }
  88.         printf("%d(%p) DX:%p SX:%p\n", X->K, X, X->Dx, X->Sx);
  89.         return X;
  90.     }
  91. }
  92.  
  93. Tree AlgoIt(Tree T, int L){
  94.     Stack S_t=NULL, S_ir=NULL, S_x= NULL;
  95.     Tree currT=T;
  96.     Tree nextT=NULL;
  97.     Tree X= NULL;
  98.     Tree ret= NULL;
  99.     int currL=L;
  100.     int nextL=0;
  101.    
  102.     while( currT != NULL || S_ir != NULL){
  103.         if(currT != NULL ){
  104.            // printf("CurrT=%d\n", currT->K);
  105.             X= NULL;
  106.             if( currT != NULL && currL >= 0 ){
  107.                 X=(Tree)malloc(sizeof(struct tree));
  108.                 X->K=currT->K;
  109.                 if( currT->Dx != NULL ){
  110.                     S_ir= push(S_ir,  (void *) 0);
  111.                     S_t= push(S_t, (void*) currT);
  112.                     S_x= push(S_x, (void *) X);
  113.                     nextT=currT->Dx;
  114.                     nextL=currL-1;
  115.                 }
  116.                 else{
  117.                     if(currT->Sx != NULL){
  118.                         S_ir=push(S_ir, (void*) 1);
  119.                         S_t=push(S_t, (void*) currT);
  120.                         S_x=push(S_x, (void *) X);
  121.                         nextT=currT->Sx;
  122.                         nextL=currL-1;
  123.                     }
  124.                     else{
  125.                         ret=X;
  126.                         nextT=NULL;
  127.                         nextL=currL-1;
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         else{
  133.             int ir= (int) ((int *) top(S_ir));
  134.             S_ir=pop(S_ir);
  135.             currT= top(S_t);
  136.             currL= currL+1;
  137.             X= (Tree) top(S_x);
  138.             S_x= pop(S_x);
  139.            
  140.             if(ir == 0){
  141.                 X->Dx= ret;
  142.                 if(currT->Sx != NULL){
  143.                     S_x=push(S_x, (void *) X);
  144.                     nextT=currT->Sx;
  145.                     nextL=currL-1;
  146.                 }
  147.                 else{
  148.                     S_t=pop(S_t);
  149.                     ret=X;
  150.                     nextT=NULL;
  151.                     nextL=currL;
  152.                 }
  153.             }
  154.             else{
  155.                 nextT=NULL;
  156.                 nextL=currL;
  157.                 X->Sx=ret;
  158.                 S_t=pop(S_t);
  159.             }
  160.         }
  161.        
  162.  
  163.         currT=nextT;
  164.         currL=nextL;
  165.     }
  166.     return X;
  167. }
  168.  
  169. int main(){
  170.     Tree t= NULL;
  171.     t= addDx(t, 1);
  172.     t= addDx(t, 3);
  173.     t= addDx(t, 6);
  174.     t= addDx(t, 5);
  175.     t= addSx(t, 2);
  176.     t= addSx(t, 4);
  177.    
  178.     printf("OUTPUT 1:\n");
  179.     AlgoRic(t, 3);
  180.     printf("----EOF-----\n\n");
  181.     printf("OUTPUT 2:\n");
  182.     AlgoIt(t, 3);
  183.     printf("----EOF-----\n\n");
  184.    
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement