Advertisement
Guest User

Untitled

a guest
Sep 11th, 2011
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1. // binary.c
  2. // comile with:
  3. //   gcc -o binary binary.c
  4. // and run.
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. typedef struct node node;
  10.  
  11. struct node
  12. {
  13.     node *_leftChild;
  14.     int _value;
  15.     node *_rightChild;
  16.     node *_parent;
  17. };
  18.  
  19. node* nn(node *left, int value, node *right)
  20. {
  21.     node *n = (node*)malloc(sizeof(node));
  22.     n->_leftChild = left;
  23.     n->_rightChild = right;
  24.     n->_value = value;
  25.     n->_parent = 0;
  26.     return n;
  27. }
  28.  
  29. void solidify(node *n)
  30. {
  31.     if(n->_leftChild)
  32.     {
  33.         n->_leftChild->_parent = n;
  34.         solidify(n->_leftChild);
  35.     }
  36.     if(n->_rightChild)
  37.     {
  38.         n->_rightChild->_parent = n;
  39.         solidify(n->_rightChild);
  40.     }
  41. }
  42.  
  43. void printascsingle(node *n)
  44. {
  45.     int direction = 0; //1 - right down ; 2 - left down ; 3 - right up ; 4 - left up
  46.  
  47.     while(n->_leftChild)
  48.         n = n->_leftChild;
  49.    
  50.     while(1)
  51.     {
  52.         if(!n->_parent && direction == 2)
  53.             break;
  54.         else if(direction == 1)
  55.         {
  56.             printf(" %i\n", n->_value);
  57.             if(n->_rightChild)
  58.             {
  59.                 n = n->_rightChild;
  60.                 direction = 3;
  61.             }
  62.             else
  63.             {
  64.                 if(n->_value < n->_parent->_value)
  65.                     direction = 1;
  66.                 else
  67.                     direction = 2;
  68.                 n = n->_parent;                    
  69.             }
  70.         }
  71.         else if(direction == 2)
  72.         {
  73.             if(n->_value <= n->_parent->_value)
  74.                 direction = 1;
  75.             n = n->_parent;
  76.         }
  77.         else
  78.         {
  79.             if(n->_leftChild)
  80.             {
  81.                 n = n->_leftChild;
  82.                 direction = 4;
  83.             }
  84.             else
  85.             {
  86.                 printf(" %i\n", n->_value);
  87.                 if(n->_rightChild)
  88.                 {
  89.                     n = n->_rightChild;
  90.                     direction = 3;
  91.                 }
  92.                 else
  93.                 {
  94.                     if(n->_value < n->_parent->_value)
  95.                         direction = 1;
  96.                     else
  97.                         direction = 2;
  98.                     n = n->_parent;                    
  99.                 }
  100.             }
  101.         }
  102.     }
  103. }
  104.  
  105. void printascdouble(node *n1, node *n2)
  106. {
  107.     int p1direction = 0;
  108.     int p2direction = 0;
  109.  
  110.     node *p1 = n1;
  111.     node *p2 = n2;
  112.    
  113.     node *r;
  114.     int rdirection;
  115.  
  116.     while(p1->_leftChild)
  117.         p1 = p1->_leftChild;
  118.     while(p2->_leftChild)
  119.         p2 = p2->_leftChild;
  120.    
  121.     while(1)
  122.     {
  123.         if((!p1->_parent && (p1direction == 2 || !p1->_rightChild)) || (!p2->_parent && (p2direction == 2 || !p2->_rightChild)))
  124.             break;
  125.            
  126.         if(p1->_value <= p2->_value)
  127.         {
  128.             if(p1direction == 1)
  129.             {
  130.                 printf(" %i\n", p1->_value);
  131.                 if(p1->_rightChild)
  132.                 {
  133.                     p1 = p1->_rightChild;
  134.                     p1direction = 3;
  135.                 }
  136.                 else
  137.                 {
  138.                     if(p1->_value < p1->_parent->_value)
  139.                         p1direction = 1;
  140.                     else
  141.                         p1direction = 2;
  142.                     p1 = p1->_parent;                      
  143.                 }
  144.             }
  145.             else if(p1direction == 2)
  146.             {
  147.                 if(p1->_value < p1->_parent->_value)
  148.                     p1direction = 1;
  149.                 p1 = p1->_parent;
  150.             }
  151.             else
  152.             {
  153.                 if(p1->_leftChild)
  154.                 {
  155.                     p1 = p1->_leftChild;
  156.                     p1direction = 4;
  157.                 }
  158.                 else
  159.                 {
  160.                     printf(" %i\n", p1->_value);
  161.                     if(p1->_rightChild)
  162.                     {
  163.                         p1 = p1->_rightChild;
  164.                         p1direction = 3;
  165.                     }
  166.                     else
  167.                     {
  168.                         if(p1->_value < p1->_parent->_value)
  169.                             p1direction = 1;
  170.                         else
  171.                             p1direction = 2;
  172.                         p1 = p1->_parent;                      
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.         else
  178.         {
  179.             if(p2direction == 1)
  180.             {
  181.                 printf(" %i\n", p2->_value);
  182.                 if(p2->_rightChild)
  183.                 {
  184.                     p2 = p2->_rightChild;
  185.                     p2direction = 3;
  186.                 }
  187.                 else
  188.                 {
  189.                     if(p2->_value < p2->_parent->_value)
  190.                         p2direction = 1;
  191.                     else
  192.                         p2direction = 2;
  193.                     p2 = p2->_parent;                      
  194.                 }
  195.             }
  196.             else if(p2direction == 2)
  197.             {
  198.                 if(p2->_value < p2->_parent->_value)
  199.                     p2direction = 1;
  200.                 p2 = p2->_parent;
  201.             }
  202.             else
  203.             {
  204.                 if(p2->_leftChild)
  205.                 {
  206.                     p2 = p2->_leftChild;
  207.                     p2direction = 4;
  208.                 }
  209.                 else
  210.                 {
  211.                     printf(" %i\n", p2->_value);
  212.                     if(p2->_rightChild)
  213.                     {
  214.                         p2 = p2->_rightChild;
  215.                         p2direction = 3;
  216.                     }
  217.                     else
  218.                     {
  219.                         if(p2->_value < p2->_parent->_value)
  220.                             p2direction = 1;
  221.                         else
  222.                             p2direction = 2;
  223.                         p2 = p2->_parent;                      
  224.                     }
  225.                 }
  226.             }      
  227.         }
  228.     }
  229.    
  230.     if(p1->_parent)
  231.     {
  232.         r = p1;
  233.         rdirection = p1direction;
  234.     }
  235.     else
  236.     {
  237.         r = p2;
  238.         rdirection = p2direction;
  239.     }
  240.    
  241.     while(1)
  242.     {
  243.         if(!r->_parent && (rdirection == 2 || !r->_rightChild))
  244.             break;
  245.         else if(rdirection == 1)
  246.         {
  247.             printf(" %i\n", r->_value);
  248.             if(r->_rightChild)
  249.             {
  250.                 r = r->_rightChild;
  251.                 rdirection = 3;
  252.             }
  253.             else
  254.             {
  255.                 if(r->_value < r->_parent->_value)
  256.                     rdirection = 1;
  257.                 else
  258.                     rdirection = 2;
  259.                 r = r->_parent;                    
  260.             }
  261.         }
  262.         else if(rdirection == 2)
  263.         {
  264.             if(r->_value < r->_parent->_value)
  265.                 rdirection = 1;
  266.             r = r->_parent;
  267.         }
  268.         else
  269.         {
  270.             if(r->_leftChild)
  271.             {
  272.                 r = r->_leftChild;
  273.                 rdirection = 4;
  274.             }
  275.             else
  276.             {
  277.                 printf(" %i\n", r->_value);
  278.                 if(r->_rightChild)
  279.                 {
  280.                     r = r->_rightChild;
  281.                     rdirection = 3;
  282.                 }
  283.                 else
  284.                 {
  285.                     if(r->_value < r->_parent->_value)
  286.                         rdirection = 1;
  287.                     else
  288.                         rdirection = 2;
  289.                     r = r->_parent;                    
  290.                 }
  291.             }
  292.         }
  293.     }  
  294. }
  295.  
  296. int main()
  297. {
  298.     node *tree1 = nn(nn(nn(0,1,0),3,nn(nn(0,4,0),6,nn(0,7,0))),8,nn(0,10,nn(nn(0,13,0),14,0)));
  299.     node *tree2 = nn(nn(nn(0,1,nn(0,2,0)),3,nn(0,12,0)),13,nn(0,14,nn(0,18,0)));
  300.    
  301.     solidify(tree1);
  302.     solidify(tree2);
  303.    
  304.     printascsingle(tree1);
  305.     printascsingle(tree2);
  306.    
  307.     printascdouble(tree1, tree2);
  308.    
  309.     return 0;
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement