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 / memslc.3 / tavl2.c < prev    next >
C/C++ Source or Header  |  1996-07-30  |  6KB  |  196 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. #include <malloc.h>
  34.  
  35. #include "../memsl.h"
  36.  
  37. WBTAVL *tavl;
  38. int stk = 0, maxstk = 0;
  39.  
  40. #ifdef WBSTDC
  41.   int Compare(void *nullitem, char *item1, char *item2)
  42. #else
  43.   int Compare(nullitem, item1, item2)
  44.     void *nullitem;
  45.     char *item1,
  46.          *item2;
  47. #endif
  48.   {
  49.     WBTrcEntry(0,"Compare",("%p, %s, %s",nullitem,item1?item1:"NULL",item2?item2:"NULL"));
  50.  
  51.     nullitem = nullitem;
  52.  
  53.     WBTrcReturn(0,strcmp(item1,item2),("%d",strcmp(item1,item2)));
  54.   }
  55.  
  56. #ifdef WBSTDC
  57.   void Delete(void *nullitem, char *item)
  58. #else
  59.   void Delete(nullitem, item)
  60.     void *nullitem;
  61.     char *item;
  62. #endif
  63.   {
  64.     WBTrcEntry(0,"Delete",("%p, %s",nullitem,item?item:"NULL"));
  65.  
  66.     nullitem = nullitem;
  67.  
  68.     free(item);
  69.  
  70.     WBTrcVReturn(0,(""));
  71.   }
  72.  
  73. #ifdef WBSTDC
  74.   void PTree(struct tavl_tree *root, int len, int right)
  75. #else
  76.   void PTree(root, len, right)
  77.     struct tavl_tree *root;
  78.     int len;
  79.     int right;
  80. #endif
  81.   {
  82.     WBTrcEntry(0,"PTree",("%p, %d, %d",root,len,right));
  83.  
  84.     if (root)
  85.       {
  86.         stk++;
  87.  
  88.     if (root->left && root->ltag == 0)
  89.       PTree(root->left,len+3,0);
  90.     if (len)
  91.       {
  92.         if (right)
  93.           printf("%*s--\\ ",len,"");
  94.         else
  95.           printf("%*s--/ ",len,"");
  96.       }
  97.     printf("%d:%s\n",root->bal,(char *)root->item);
  98.     if (root->bal < -1 || root->bal > 1) /* Should never happen */
  99.       {
  100.         printf("Error: balance factor.\n");
  101.         WBTrcExit(0,1,("Error: balance factor"));
  102.       }
  103.     if (root->right && root->rtag == 0)
  104.       PTree(root->right,len+3,1);
  105.  
  106.         stk--;
  107.  
  108.         if (stk > maxstk)
  109.           maxstk = stk;
  110.       }
  111.  
  112.     WBTrcVReturn(0,(""));
  113.   }
  114.  
  115. #if defined(WBTRC_LEVEL1) || defined(WBTRC_LEVEL2)
  116.   #ifdef WBSTDC
  117.     int main(int argc, char **argv)
  118.   #else
  119.     int main(argc, argv)
  120.       int argc;
  121.       char **argv;
  122.   #endif
  123. #else
  124.   #ifdef WBSTDC
  125.     int main(void)
  126.   #else
  127.     int main()
  128.   #endif
  129. #endif
  130.   {
  131.     FILE *file;
  132.     char str[256], *strptr;
  133.     long numitems = 0;
  134.  
  135.     WBTrcMainEntry();
  136.  
  137. #if defined(WBMEMTRACE) || defined(WBUSERMEM)
  138.     if (getenv("WBOVRCHK") || getenv("WBMEMCHK"))
  139.       {
  140.     printf("This program will run extremely slow with the WBOVRCHK\n");
  141.     printf("and WBMEMCHK environment variables set.  Please unset\n");
  142.     printf("these environment variables.\n\n");
  143.     WBTrcExit(0,1,("environment"));
  144.       }
  145. #endif
  146.  
  147.     if ((tavl = WBTAVLOpen(NULL)) != NULL)
  148.       {
  149.     WBTAVLDuplicates(tavl,WB_TRUE);
  150. #ifdef FUNCTCAST
  151.     WBTAVLCompareF(tavl,NULL,(int (*)(void *, void *, void *))Compare);
  152.     WBTAVLDeleteF(tavl,NULL,(void (*)(void *, void *))Delete);
  153. #else
  154.     WBTAVLCompareF(tavl,NULL,(int (*)())Compare);
  155.     WBTAVLDeleteF(tavl,NULL,(void (*)())Delete);
  156. #endif
  157.  
  158.     if ((file = fopen("data3.dat","r")) != NULL)
  159.       {
  160.         while (fgets(str,255,file))
  161.           {
  162.         if (str[strlen(str)-1] == '\n')
  163.           str[strlen(str)-1] = 0;
  164.          if ((strptr = malloc(strlen(str)+1)) != NULL)
  165.           {
  166.             strcpy(strptr,str);
  167.             if (WBTAVLAdd(tavl,strptr) == 1)
  168.               printf("Added: %-20s  Numitems: %ld\n",str, ++numitems);
  169.           }
  170.           }
  171.         fclose(file);
  172.       }
  173.     printf("%ld Items added to threaded AVL tree.\n",numitems);
  174.     printf("==========================\n");
  175.     PTree(tavl->root,0,0);
  176.     printf("Max Stack = %d\n",maxstk);
  177.     printf("==========================\n");
  178.     while (WBTAVLIsEmpty(tavl) == 0)
  179.       if ((file = fopen("data3.dat","r")) != NULL)
  180.         {
  181.           while (WBTAVLIsEmpty(tavl) == 0 && fgets(str,255,file))
  182.         {
  183.           if (str[strlen(str)-1] == '\n')
  184.             str[strlen(str)-1] = 0;
  185.                    if (WBTAVLDelete(tavl,str) == 0)
  186.                     printf("%c%c%s not found\n",7,7,str);
  187.                   else
  188.                     printf("Deleted: %s\n",str);
  189.                 }
  190.               fclose(file);
  191.             }
  192.           WBTAVLClose(tavl);
  193.         }
  194.     WBTrcReturn(0,0,("0"));
  195.   }
  196.