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

  1. #include <stdio.h>
  2.  
  3. typedef  struct   {  int   key;  }  LEAF;
  4.  
  5. #include "tree.h"
  6.  
  7. /* -------------------------------------------------------------------------- */
  8.  
  9. prnt(stream, p)
  10. FILE  *stream;
  11. LEAF  *p;
  12. {
  13.    fprintf( stream, p ? "%2d" : "  ", p->key );
  14. }
  15.  
  16. /* -------------------------------------------------------------------------- */
  17.  
  18. icmp(n1,n2)                   /* comparison routine for insert() to use */
  19. LEAF  *n1, *n2;
  20. {
  21.    return(n1->key - n2->key);
  22. }
  23.  
  24. dcmp(key, n2)                 /* comparison routine for delete() and find() */
  25. int   key;
  26. LEAF  *n2;
  27. {
  28.    return(key - n2->key);
  29. }
  30.  
  31. /* -------------------------------------------------------------------------- */
  32.  
  33. docmd( cmd, n)
  34. {
  35.    static   TREE  *root = NULL;
  36.    LEAF           *p, *p2;
  37.  
  38.    switch(cmd)
  39.       {
  40.       case 'd':
  41.       case 'D':
  42.          if( !delete(&root, (LEAF *) n, dcmp) )
  43.             fprintf(stderr, "Node is NOT in tree\n");
  44.          break;
  45.  
  46.       case 'f':
  47.       case 'F':
  48.          if(p = find(root, (LEAF *) n, dcmp) )
  49.             fprintf(stderr, "Node %d found\n", p->key);
  50.          else
  51.             fprintf(stderr, "Node NOT found\n" );
  52.          break;
  53.  
  54.       case 'i':
  55.       case 'I':
  56.          if( !(p = talloc(sizeof(LEAF))) )
  57.             fprintf(stderr, "Out of memory\n");
  58.          else
  59.             {
  60.             p->key = n;
  61.             if(p2 = insert(&root, p, icmp) )
  62.                {
  63.                fprintf(stderr, "%d is already in the tree\n",p2->key);
  64.                tfree(p);
  65.                }
  66.             }
  67.          break;
  68.  
  69.       case 'a':
  70.       case 'A':
  71.          freeall(&root);
  72.          break;
  73.  
  74.       case 'q':
  75.       case 'Q':
  76.          exit(0);
  77.       }
  78.  
  79.    tprint( root, prnt, stdout );
  80.    printf("\n");
  81. }
  82.  
  83. /* -------------------------------------------------------------------------- */
  84.  
  85. main(argc, argv)
  86. int   argc;
  87. char  **argv;
  88. {
  89.    /* Assemble a tree, first get commands from the command line
  90.       until these are exhausted, then get commands from the
  91.       keyboard.
  92.    */
  93.  
  94.    char     buf[128];
  95.  
  96.    for(++argv; --argc > 0; ++argv)
  97.       docmd( **argv, atoi(*argv + 1) );
  98.  
  99.    printf("commands are: iN -insert node N into tree\n");
  100.    printf("              dN -delete node N\n");
  101.    printf("              fN -find   node N\n");
  102.    printf("              a  -delete entire tree\n");
  103.    printf("              q  -quit (exit)\n");
  104.  
  105.    for(; gets(buf); printf("i/d/f/a/q: ") )
  106.       docmd(*buf, atoi(buf+1) );
  107. }
  108.