home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / windbase / windbase.exe / MEMSLC.3 / TAVLTREE.C < prev    next >
C/C++ Source or Header  |  1996-07-30  |  6KB  |  193 lines

  1. /*****************************************************************************\
  2. **                                                                           **
  3. **  WW     WW IIIIIIII NNN   NN DDDDDDD  BBBBBBB     AA     SSSSSS EEEEEEEE  **
  4. **  WW  W  WW    II    NNNN  NN DD    DD BB    BB  AA  AA  SS      EE        **
  5. **  WW  W  WW    II    NN NN NN DD    DD BBBBBBB  AAAAAAAA  SSSSSS EEEEEE    **
  6. **   WW W WW     II    NN  NNNN DD    DD BB    BB AA    AA      SS EE        **
  7. **    WWWWW   IIIIIIII NN   NNN DDDDDDD  BBBBBBB  AA    AA SSSSSS  EEEEEEEE  **
  8. **                                                                           **
  9. **   SSSSSS  OOOOOO  FFFFFFFF TTTTTTTT WW     WW    AA    RRRRRRR  EEEEEEEE  **
  10. **  SS      OO    OO FF          TT    WW  W  WW  AA  AA  RR    RR EE        **
  11. **   SSSSS  OO    OO FFFFF       TT    WW  W  WW AAAAAAAA RRRRRRR  EEEEEE    **
  12. **       SS OO    OO FF          TT     WW W WW  AA    AA RR   RR  EE        **
  13. **  SSSSSS   OOOOOO  FF          TT      WWWWW   AA    AA RR    RR EEEEEEEE  **
  14. **                                                                           **
  15. *********** NOTICE ************************************************************
  16. **        This file contains valuable trade secrets and proprietary          **
  17. **        assets of Windbase Software Inc.  Embodying substantial            **
  18. **        creative efforts and confidential information.  Unauthorized       **
  19. **        use, copying, decompiling, translating, disclosure or              **
  20. **        transfer, of any kind, is strictly prohibited.                     **
  21. **                                                                           **
  22. **        COPYRIGHT (C) 1992, 1993, 1994.  Windbase Software Inc.            **
  23. **        ALL RIGHTS RESERVED.                                               **
  24. \*****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #ifdef HAS_UNISTD_H
  29. #  include <unistd.h>
  30. #else
  31. #  include <stdlib.h>
  32. #endif
  33.  
  34. #include "../memsl.h"
  35.  
  36. WBTAVL *tavl;
  37.  
  38. #ifdef WBSTDC
  39.   int Compare(void *nullitem, char *item1, char *item2)
  40. #else
  41.   int Compare(nullitem, item1, item2)
  42.     void *nullitem;
  43.     char *item1,
  44.          *item2;
  45. #endif
  46.   {
  47.     WBTrcEntry(0,"Compare",("%p, %s, %s",nullitem,item1?item1:"NULL",item2?item2:"NULL"));
  48.  
  49.     nullitem = nullitem;
  50.  
  51.     WBTrcReturn(0,strcmp(item1,item2),("%d",strcmp(item1,item2)));
  52.   }
  53.  
  54. #ifdef WBSTDC
  55.   void Delete(void *nullitem, char *item)
  56. #else
  57.   void Delete(nullitem, item)
  58.     void *nullitem;
  59.     char *item;
  60. #endif
  61.   {
  62.     WBTrcEntry(0,"Delete",("%p, %s",nullitem,item?item:"NULL"));
  63.  
  64.     nullitem = nullitem;
  65.  
  66.     free(item);
  67.  
  68.     WBTrcVReturn(0,(""));
  69.   }
  70.  
  71. #ifdef WBSTDC
  72.   void Execute(void *nullitem, char *item)
  73. #else
  74.   void Execute(nullitem, item)
  75.     void *nullitem;
  76.     char *item;
  77. #endif
  78.   {
  79.     WBTrcEntry(0,"Execute",("%p, %s",nullitem,item?item:"NULL"));
  80.  
  81.     nullitem = nullitem;
  82.  
  83.     printf("Execute(): %s\n",item);
  84.  
  85.     WBTrcVReturn(0,(""));
  86.   }
  87.  
  88. #ifdef WBSTDC
  89.   void PTree(struct tavl_tree *root, int len, int right)
  90. #else
  91.   void PTree(root, len, right)
  92.     struct tavl_tree *root;
  93.     int len;
  94.     int right;
  95. #endif
  96.   {
  97.     WBTrcEntry(0,"PTree",("%p, %d, %d",root,len,right));
  98.  
  99.     if (root)
  100.       {
  101.     if (root->left && root->ltag == 0)
  102.       PTree(root->left,len+3,0);
  103.     if (len)
  104.       {
  105.         if (right)
  106.           printf("%*s--\\ ",len,"");
  107.         else
  108.           printf("%*s--/ ",len,"");
  109.       }
  110.     printf("%d:%s\n",root->bal,(char *)root->item);
  111.     if (root->bal < -1 || root->bal > 1) /* Should never happen */
  112.       {
  113.         printf("Error: balance factor.\n");
  114.         exit(1);
  115.       }
  116.     if (root->right && root->rtag == 0)
  117.       PTree(root->right,len+3,1);
  118.       }
  119.     WBTrcVReturn(0,(""));
  120.   }
  121.  
  122. #if defined(WBTRC_LEVEL1) || defined(WBTRC_LEVEL2)
  123.   #ifdef WBSTDC
  124.     int main(int argc, char **argv)
  125.   #else
  126.     int main(argc, argv)
  127.       int argc;
  128.       char **argv;
  129.   #endif
  130. #else
  131.   #ifdef WBSTDC
  132.     int main(void)
  133.   #else
  134.     int main()
  135.   #endif
  136. #endif
  137.   {
  138.     FILE *file;
  139.     char str[256], *strptr;
  140.  
  141.     WBTrcMainEntry();
  142.  
  143.     if ((tavl = WBTAVLOpen(NULL)) != NULL)
  144.       {
  145.     WBTAVLDuplicates(tavl,1);
  146. #ifdef FUNCTCAST
  147.     WBTAVLCompareF(tavl,NULL,(int (*)(void *, void *, void *))Compare);
  148.     WBTAVLDeleteF(tavl,NULL,(void (*)(void *, void *))Delete);
  149.     WBTAVLExecuteF(tavl,NULL,(void (*)(void *, void *))Execute);
  150. #else
  151.     WBTAVLCompareF(tavl,NULL,(int (*)())Compare);
  152.     WBTAVLDeleteF(tavl,NULL,(void (*)())Delete);
  153.     WBTAVLExecuteF(tavl,NULL,(void (*)())Execute);
  154. #endif
  155.  
  156.     if ((file = fopen("data.dat","r")) != NULL)
  157.       {
  158.         while (fgets(str,255,file))
  159.           {
  160.         if (str[strlen(str)-1] == '\n')
  161.           str[strlen(str)-1] = 0;
  162.  
  163.         if ((strptr = malloc(strlen(str)+1)) != NULL)
  164.           {
  165.             strcpy(strptr,str);
  166.             if (WBTAVLAdd(tavl,strptr) == 1)
  167.                       printf("Added: %s\n",str);
  168.                   }
  169.               }
  170.             fclose(file);
  171.             printf("==========================\n");
  172.         PTree(tavl->root,0,0);
  173.             WBTAVLExecute(tavl);
  174.             if ((file = fopen("data.dat","r")) != NULL)
  175.               {
  176.                 while (fgets(str,255,file))
  177.                   {
  178.                     if (str[strlen(str)-1] == '\n')
  179.               str[strlen(str)-1] = 0;
  180.  
  181.             if (WBTAVLDelete(tavl,str) == 0)
  182.                       printf("%c%c%s not found\n",7,7,str);
  183.                     else
  184.                       printf("Deleted: %s\n",str);
  185.                   }
  186.                 fclose(file);
  187.               }
  188.           }
  189.         WBTAVLClose(tavl);
  190.       }
  191.     WBTrcReturn(0,0,("0"));
  192.   }
  193.