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