Advertisement
Guest User

BetterAlgo

a guest
Jun 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 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. Tree AlgoRic(Tree T, int L){
  77.     Tree X=NULL;
  78.     if( T != NULL && L >= 0){
  79.         X= (Tree)malloc(sizeof(struct tree));
  80.         X->K= T->K;
  81.         if ( T->Dx != NULL ){
  82.             X->Dx= AlgoRic(T->Dx, L-1);
  83.         }
  84.         if( T->Sx != NULL ){
  85.             X->Sx= AlgoRic(T->Sx, L-1);
  86.         }
  87.         printf("%d(%p) DX:%p SX:%p\n", X->K, X, X->Dx, X->Sx);
  88.         return X;
  89.     }
  90. }
  91.  
  92. Tree AlgoIt(Tree T, int L){
  93.     Stack S_x=NULL, S_t=NULL;
  94.     Tree currT=T;
  95.     Tree nextT=NULL;
  96.     Tree X= NULL;
  97.     Tree ret= NULL;
  98.     int currL=L;
  99.     int nextL=0;
  100.     int side;
  101.    
  102.     while(currT != NULL || S_x != NULL){
  103.         if(currT != NULL){
  104.             printf("CURR:%d", currT->K);
  105.         }
  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_x=push(S_x,(void*) X);
  111.                     S_t=push(S_t,(void*) currT);
  112.                     nextT= currT->Dx;
  113.                     nextL= currL-1;
  114.                     side=0;
  115.                    
  116.                 }
  117.                 else if( currT->Sx != NULL){
  118.                     S_x=push(S_x,(void*) X);
  119.                     S_t=push(S_t,(void*) currT);
  120.                     nextT= currT->Sx;
  121.                     nextL= currL-1;
  122.                     side=1;
  123.                 }
  124.                 else{
  125.                     ret=X;
  126.                     nextT=NULL;
  127.                     nextL=currL-1;
  128.                 }
  129.         }
  130.         else{
  131.            
  132.             currT=(Tree)top(S_t);
  133.             X=(Tree) top(S_x);
  134.             S_x=pop(S_x);
  135.             currL=currL+1;
  136.            
  137.             if(side == 0){
  138.                 X->Dx= ret;
  139.                 if(currT->Sx != NULL){
  140.                     S_x=push(S_x, (void*) X);
  141.                     nextT=currT->Sx;
  142.                     nextL=currL-1;
  143.                 }
  144.                 else{
  145.                     S_t=pop(S_t);
  146.                     ret=X;
  147.                     nextT=NULL;
  148.                     nextL=currL;
  149.                 }
  150.             }
  151.             else{
  152.                 nextT= NULL;
  153.                 nextL=currL;
  154.                 X->Sx=ret;
  155.                 S_t=pop(S_t);
  156.             }
  157.         }
  158.         currL=nextL;
  159.         currT=nextT;
  160.     }
  161.     return X;
  162. }
  163.  
  164. int main(){
  165.     Tree t= NULL;
  166.     t= addDx(t, 1);
  167.     t= addDx(t, 3);
  168.     t= addDx(t, 6);
  169.     t= addDx(t, 5);
  170.     t= addSx(t, 2);
  171.     t= addSx(t, 4);
  172.    
  173.     printf("OUTPUT 1:\n");
  174.     AlgoRic(t, 3);
  175.     printf("----EOF-----\n\n");
  176.     printf("OUTPUT 2:\n");
  177.     AlgoIt(t, 3);
  178.     printf("----EOF-----\n\n");
  179.    
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement