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 / MEMSLCPP.3 / AVLTREE2.CPP < prev    next >
C/C++ Source or Header  |  1996-05-11  |  4KB  |  112 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, 1995.  Windbase Software Inc.     **
  23. **       ALL RIGHTS RESERVED.                                              **
  24. \***************************************************************************/
  25.  
  26. /*
  27. ** AVL Balanced Binary Tree - Non Templates Version
  28. */
  29.  
  30. class MyAVLTree : public WBAVLTree
  31.   {
  32.     int WBCompareFunct(void *nullitem, void *item1, void *item2)
  33.       {
  34.     nullitem = nullitem;
  35.     return(strcmp((char *)item1, (char *)item2));
  36.       }
  37.  
  38.     void WBExecuteFunct(void *nullitem, void *item)
  39.       {
  40.     nullitem = nullitem;
  41.     cout << "ExecuteFunct: " << (char *) item << "\n";
  42.       }
  43.  
  44.     void WBDeleteFunct(void *nullitem, void *item)
  45.       {
  46.     nullitem = nullitem;
  47.     cout << "DeleteFunct: " << (char *) item << "\n";
  48.       }
  49.   };
  50.  
  51. void PrintTree(int bal, int level, int left, char *item)
  52.   {
  53.     if (level)
  54.       {
  55.     cout.width(level*2);
  56.     cout.fill(' ');
  57.     if (left)
  58.       cout << "/" << "-- ";
  59.     else
  60.       cout << "\\" << "-- ";
  61.     cout << bal << ":" << item << "\n";
  62.       }
  63.     else
  64.       cout << bal << ":" << item << "\n";
  65.   }
  66.  
  67. main()
  68.   {
  69.     MyAVLTree tree;
  70.     ifstream infile("data.dat");
  71.     char str[81], *strptr;
  72.     int i = 0;
  73.  
  74.     if ((!infile) == 0 && infile.rdbuf()->is_open())
  75.       {
  76.     while (infile.getline(str,80))
  77.       {
  78.         cout << "Adding " << ++i << "\n";
  79.         if ((strptr = new char [strlen(str)+1]) != NULL)
  80.           {
  81.         strcpy(strptr,str);
  82.         tree += strptr;
  83.           }
  84.       }
  85.  
  86.     cout << "IsEmpty: " << (tree.IsEmpty() ? "Yes" : "No") << "\n";
  87.     cout << "NumItems: " << tree.NumItems() << "\n";
  88.  
  89.     tree.InOrder();
  90.     tree.InOrderPrint((void (*)(int,int,int,void *))PrintTree);
  91.     infile.close();
  92.       }
  93.  
  94.     infile.open("data.dat");
  95.     if ((!infile) == 0 && infile.rdbuf()->is_open())
  96.       {
  97.     while (infile.getline(str,80))
  98.       cout << "Search: " << (char *) tree.Search(str) << "\n";
  99.  
  100.     infile.close();
  101.       }
  102.  
  103.     infile.open("data.dat");
  104.     if ((!infile) == 0 && infile.rdbuf()->is_open())
  105.       {
  106.     while (infile.getline(str,80))
  107.       cout << "Delete: " << str << " " << tree.Delete(str) << "\n";
  108.       }
  109.  
  110.     return(0);
  111.   }
  112.