home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d103 / avltrees.lha / AvlTrees / t_trtest.c < prev    next >
C/C++ Source or Header  |  1987-10-22  |  2KB  |  127 lines

  1. /* t_trtest - test the tree functions
  2.  * vix 24jul87 [documented, added savestr for net distribution]
  3.  */
  4.  
  5. #define MAIN
  6.  
  7. #include <stdio.h>
  8. #include "vixie.h"
  9. #include "tree.h"
  10.  
  11. main()
  12. {
  13.     tree    *t;
  14.     char    line[100];
  15.  
  16.     tree_init(&t);
  17.     while (printf("line (or .):  "), gets(line), line[0] != '.')
  18.     {
  19.         if (strncmp(line, "~r ", 3)) {
  20.             trtest(&t, line, 1);
  21.         }
  22.         else {
  23.             FILE *f;
  24.  
  25.             if (!(f = fopen(&line[3], "r")))
  26.                 perror(&line[3]);
  27.             else {
  28.                 while (fgets(line, 100, f)) {
  29.                     line[strlen(line)-1] = '\0';
  30.                     printf("(%s)\n", line);
  31.                     trtest(&t, line, 0);
  32.                 }
  33.                 fclose(f);
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  39. trtest(tt, line, inter)
  40. tree    **tt;
  41. char    *line;
  42. {
  43.     char    opts[100], *tree_srch(), *pc, *n;
  44.     int    uar_print(), duar(), compar(), opt, status;
  45.  
  46.     pc = tree_srch(tt, compar, line);
  47.     printf("tree_srch=%08lx\n", pc);
  48.     if (pc)
  49.     {
  50.         printf("     <%s>\n", pc);
  51.  
  52.         if (inter) {
  53.             printf("delete? "); gets(opts); opt = (opts[0]=='y');
  54.         }
  55.         else
  56.             opt = 1;
  57.  
  58.         if (opt) {
  59.             status = tree_delete(tt, compar, line, duar);
  60.             printf("delete=%d\n", status);
  61.         }
  62.     }
  63.     else
  64.     {
  65.         if (inter) {
  66.             printf("add? "); gets(opts); opt = (opts[0]=='y');
  67.         }
  68.         else
  69.             opt = 1;
  70.  
  71.         if (opt) {
  72.             char    *savestr();
  73.  
  74.             n = savestr(line);
  75.             tree_add(tt, compar, n, duar);
  76.         }
  77.     }
  78.     tree_trav1(*tt, 0);
  79. }
  80.  
  81. duar(pc)
  82. char *pc;
  83. {
  84.     printf("duar called, pc=%08X: <%s>\n", pc, pc?pc:"");
  85.     free(pc);
  86. }
  87.  
  88. tree_trav1(t, l)
  89. tree    *t;
  90. {
  91.     int    i;
  92.  
  93.     if (!t) return;
  94.     tree_trav1(t->tree_l, l+1);
  95.     for (i=0;  i<l;  i++) printf("  ");
  96.     printf("%08lx (%s)\n", t->tree_p, t->tree_p);
  97.     tree_trav1(t->tree_r, l+1);
  98. }    
  99.     
  100. uar_print(pc)
  101. char    *pc;
  102. {
  103.     printf("uar_print(%08lx)", pc);
  104.     if (pc)
  105.         printf(" '%s'", pc);
  106.     putchar('\n');
  107.     return 1;
  108. }
  109.  
  110. compar(l, r)
  111.     char *l, *r;
  112. {
  113.     printf("compar(%s,%s)=%d\n", l, r, strcmp(l, r));
  114.     return strcmp(l, r);
  115. }
  116.  
  117. char *
  118. savestr(str)
  119.     char    *str;
  120. {
  121.     char    *save;
  122.  
  123.     save = malloc(strlen(str) + 1);
  124.     strcpy(save, str);
  125.     return save;
  126. }
  127.