home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06069a < prev    next >
Text File  |  1990-11-12  |  3KB  |  90 lines

  1.  
  2.  
  3. #include <stdio.h>
  4. #include <graph.h>
  5.  
  6. /* Tree struct */
  7. typedef struct tnode {          /* Standard binary tree node for storage */
  8.   struct tnode *left, *right;   /* of integer values                     */
  9.   int num;
  10. } TREENODE;
  11.  
  12. /* Display printing/spacing constants */
  13. #define NUMWID          4       /* Width of a number shown in a binary tree */
  14. #define WIDSTR    "%4d\n"       /* Format string for number in binary tree  */
  15. #define ROWSPC          3       /* Row spacing in showing binary tree       */
  16.  
  17. /* Function prototypes */
  18. static
  19. int width(TREENODE *);                  /* Determine width of a subtree     */
  20.  
  21. void printtree(TREENODE *),             /* Print contents of tree           */
  22.      showtree(TREENODE *, int, int);    /* Draw the tree                    */
  23.  
  24.  
  25.  
  26. /* Recursive function to graphically display the tree.
  27.    Usage:  void showtree(node, r, c)
  28.              TREENODE *node;    /* Pointer to a local root
  29.              int r,             /* Row coordinate to print the node value
  30.                  c;             /* Column coordinate to print the node value
  31.    Returns:  Nothing
  32. */
  33. void showtree(node, r, c)
  34.   TREENODE *node;
  35.   int r, c;
  36. {
  37.   if (node != NULL) {
  38.     /* Position graphics line pointer only if within an 80 x 25 screen */
  39.     if ((r > 0) && (r < 26) && (c > 0) && (c < 81)) {
  40.       _lineto((c << 3) + 13, (r << 3) - 10);
  41.       _moveto((c << 3) + 13, r << 3);
  42.     }
  43.  
  44.     /* Show left subtree */
  45.     if (node->left)
  46.       showtree(node->left, r + ROWSPC, c - (NUMWID + width(node->left->right)));
  47.  
  48.     /* Print value only if within 80 x 25 screen */
  49.     if ((r > 0) && (r < 26) && (c > 0) && (c < 76)) {
  50.       _settextposition(r, c);
  51.       printf("%4d", node->num);
  52.       _moveto((c << 3) + 13, r << 3);
  53.     }
  54.  
  55.     /* Show right subtree */
  56.     if (node->right)
  57.       showtree(node->right, r + ROWSPC, c + NUMWID + width(node->right->left));
  58.   }
  59. }
  60.  
  61.  
  62. /* Recursive function to determine the width of a local root.  Used in the
  63.    graphic display of the tree.
  64.    Usage:  int width(node)
  65.              TREENODE *node;    /* Pointer to a local root
  66.    Returns:  The width of the local root.
  67. */
  68. int width(node)
  69.   TREENODE *node;
  70. {
  71.   return(node != NULL ? NUMWID + width(node->left) + width(node->right) : 0);
  72. }
  73.  
  74.  
  75. /* Recursive function to perform a simple traversal of the tree in numerical
  76.    order while printing the node values.
  77.    Usage:  void printtree(node)
  78.              TREENODE *node;    /* A pointer to a local root
  79.    Returns:  Nothing
  80. */
  81. void printtree(node)
  82.   TREENODE *node;
  83. {
  84.   if (node != NULL) {
  85.     printtree(node->left);
  86.     printf("%d\n", node->num);
  87.     printtree(node->right);
  88.   }
  89. }
  90.