home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / database / avltree / avlprnt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-09  |  2.4 KB  |  105 lines

  1. #include <stdio.h>
  2. #include "avl.h"
  3.  
  4. #define  VERT     Cset[0]
  5. #define  GAMMA    Cset[1]
  6. #define  ELL      Cset[2]
  7. #define  T_LEFT   Cset[3]
  8. #define  T_UP     Cset[4]
  9. #define  T_DOWN   Cset[5]
  10.  
  11. #ifdef DEBUG
  12. #define  PAD()    printf("   ");
  13. #define  PBAL(r)  printf("(%c)", r->bal==B ? 'B': r->bal==L ? 'L' : 'R');
  14. #else
  15. #define  PAD()
  16. #define  PBAL(r)
  17. #endif
  18.  
  19. static char *Graph_chars[] = {"\263",       "\332\304\304", "\300\304\304",
  20.                               "\304\304\264", "\304\304\331", "\304\304\277"
  21.                              };
  22.  
  23. static char *Norm_chars[] = { "|", "+--", "+--", "--+", "--+" };
  24.  
  25. static int  (*Print) ();
  26. static FILE *Out;
  27. static char **Cset;
  28. static char Map[ 64 / 8];
  29.  
  30. #define testbit(c) ( Map[c>>3] & (1 << (c & 0x07)) )
  31.  
  32. static   setbit(c,val)
  33. int      c,val;
  34. {
  35.    if(val)
  36.       Map[c>>3] |= 1 << (c & 0x07);
  37.    else
  38.       Map[c>>3] &= ~(1 << (c & 0x07));
  39. }
  40.  
  41. static   trav(root, amleft)
  42. HEADER   *root;
  43. int      amleft;
  44. {
  45.    /* Prints a binary tree graphically, with lines showing
  46.     * all the pointers. This is essentially the same routine
  47.     * we looked at last month.  See that article for more
  48.     * info about how it works.
  49.    */
  50.  
  51.    static   int   depth = -1;    /* current depth in tree */
  52.    static   int   i;
  53.  
  54.    if(root)
  55.       {
  56.       ++depth;
  57.  
  58.       if(root->right)
  59.          trav(root->right,0);
  60.       else
  61.          setbit(depth+1, 1);
  62.  
  63.       for(i=1;i<=depth;i++)
  64.          {
  65.          (*Print)(Out, 0);
  66.          PAD();
  67.          if(i == depth)
  68.             fprintf(Out, "  %s", amleft ? ELL : GAMMA );
  69.          else
  70.             if(testbit(i))
  71.                fprintf(Out, "  %s  ", VERT );
  72.             else
  73.                fprintf(Out, "     ", VERT );
  74.          }
  75.  
  76.       (*Print) (Out, root + 1);
  77.       PBAL(root);
  78.  
  79.       fprintf(Out, "%s\n",
  80.          (root->left) ? (root->right ? T_LEFT : T_DOWN)
  81.                       : (root->right ? T_UP   : ""    )
  82.                       );
  83.  
  84.       setbit(depth, amleft ? 0 : 1 );
  85.  
  86.       if(root->left)
  87.          trav(root->left, 1);
  88.       else
  89.          setbit(depth+1, 0);
  90.       --depth;
  91.    }
  92. }
  93.  
  94. void     tprint( root, print, stream )
  95. HEADER   *root;
  96. int      (*print)();
  97. FILE     *stream;
  98. {
  99.    Out = stream;
  100.    Print = print;
  101.    Cset = Out == stdout && isatty(fileno(stdout)) ? Graph_chars
  102.                                                   : Norm_chars ;
  103.    trav(root, 0);
  104. }
  105.