home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / btree / btreetst.cxx < prev    next >
C/C++ Source or Header  |  1994-11-10  |  8KB  |  246 lines

  1.  
  2.  
  3. // Miscellaneous calls to B-tree methods for testing
  4. // M. A. Sridhar 5/5/94
  5.  
  6. #include "base/tbtree.h"
  7. #include "base/string.h"
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <iostream.h>
  12.  
  13. extern unsigned _stklen = 24000;
  14.  
  15. typedef CL_BTree<CL_String>           CL_StringBTree;
  16. typedef CL_BTreeIterator<CL_String>   CL_StringBTreeIterator;
  17. main ()
  18. {
  19.     CL_StringBTree tree (4); // Order 4, just for kicks
  20.  
  21.  
  22.     CL_String s;
  23.     long i;
  24.  
  25.     for (i = 0; i < 40; i++) {
  26.         tree.Add ("A " + CL_String (i, 3));
  27.     }
  28. //     cout << tree << flush;
  29. //     cout << "-----------------------------------\n\n";
  30.  
  31.     tree.Add ("A 002");
  32.     cout << tree << flush;
  33.     cout << "0-----------------------------------\n\n";
  34.  
  35.     for (i = 0; i < 15; i++)
  36.         cout << "ExtractMin: " << tree.ExtractMin () << endl;
  37.  
  38.     for (i = 0; i < 15; i++) {
  39.         tree.Add ("A " + CL_String (i, 3));
  40.     }
  41.  
  42.     {
  43.         CL_String s;
  44.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  45.         s = "Axxx";
  46.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  47.         s = "A  36";
  48.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  49.         s = "A 002";
  50.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  51.         s = "A   8";
  52.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  53.         s = "A  141";
  54.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  55.         s = "A  14";
  56.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  57.         s = "A   0";
  58.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  59.     }
  60.  
  61.     s = "A 003";
  62.     printf ("%s is %s in the tree (it shouldn't be)\n", s.AsPtr(),
  63.             (tree.Find (s)) ? "" : "not");
  64.     s = "A   3";
  65.     printf ("%s is %s in the tree (it should be)\n", s.AsPtr(),
  66.             (tree.Find (s)) ? "" : "not"); 
  67.     long rank = 0;
  68.     printf ("The element with rank %ld is '%s'\n", rank,
  69.             tree.ItemWithRank(rank).AsString().AsPtr());
  70.     rank = 15;
  71.     printf ("The element with rank %ld is '%s'\n", rank,
  72.             tree.ItemWithRank(rank).AsString().AsPtr());
  73.     rank = 17;
  74.     printf ("The element with rank %ld is '%s'\n", rank,
  75.             tree.ItemWithRank(rank).AsString().AsPtr());
  76.     rank = 36;
  77.     printf ("The element with rank %ld is '%s'\n", rank,
  78.             tree.ItemWithRank(rank).AsString().AsPtr());
  79.     rank = 40;
  80.     printf ("The element with rank %ld is '%s'\n", rank,
  81.             tree.ItemWithRank(rank).AsString().AsPtr());
  82.     rank = 50;
  83.     printf ("The element with rank %ld is '%s'\n", rank,
  84.             tree.ItemWithRank(rank).AsString().AsPtr());
  85.     rank = -30;
  86.     printf ("The element with rank %ld is '%s'\n", rank,
  87.             tree.ItemWithRank(rank).AsString().AsPtr());
  88.  
  89.  
  90.     printf ("\nBegin Btree iterator test\n");
  91.     CL_StringBTreeIterator iter (tree);
  92.     for (iter.Reset(); iter.More();) {
  93.         CL_String s = iter.Next();
  94.         printf ("'%s'\n", (const char*) s);
  95.     }
  96.     printf ("z-----------------------------------------------\n\n");
  97.  
  98.     printf ("\nBegin Btree iterator test BeginFrom\n");
  99.     iter.BeginFrom ("A   8");
  100.     while (iter.More()) {
  101.         CL_String s = iter.Next ();
  102.         printf ("'%s'\n", (const char*) s);
  103.     }
  104.     printf ("zz-----------------------------------------------\n\n");
  105.     printf ("\nBegin Btree iterator test BeginFrom\n");
  106.     iter.BeginFrom ("A  26");
  107.     while (iter.More()) {
  108.         CL_String s = iter.Next ();
  109.         printf ("'%s'\n", (const char*) s);
  110.     }
  111.     printf ("zz-----------------------------------------------\n\n");
  112.     printf ("\nBegin Btree iterator test BeginFrom\n");
  113.     iter.BeginFrom ("A  27");
  114.     while (iter.More()) {
  115.         CL_String s = iter.Next ();
  116.         printf ("'%s'\n", (const char*) s);
  117.     }
  118.     printf ("zz-----------------------------------------------\n\n");
  119.         
  120.     printf ("\nBegin Btree iterator test BeginFrom\n");
  121.     iter.BeginFrom ("Axxxx");
  122.     while (iter.More()) {
  123.         CL_String s = iter.Next ();
  124.         printf ("'%s'\n", (const char*) s);
  125.     }
  126.     printf ("zz-----------------------------------------------\n\n");
  127.         
  128.     printf ("\nBegin Btree iterator test BeginFrom\n");
  129.     iter.BeginFrom ("A   0");
  130.     while (iter.More()) {
  131.         CL_String s = iter.Next ();
  132.         printf ("'%s'\n", (const char*) s);
  133.     }
  134.     printf ("zz-----------------------------------------------\n\n");
  135.         
  136.     for (i = 39; i >= 25; i--) {
  137.         tree.Remove ("A " + CL_String (i, 3));
  138.         cout << tree << flush;
  139.         // printf ("-----------------------------------\n\n");
  140.     }
  141.  
  142.     cout << tree << flush;
  143.     printf ("a-----------------------------------\n\n");
  144.  
  145.     {
  146.         CL_String s;
  147.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  148.         s = "Axxx";
  149.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  150.         s = "A  36";
  151.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  152.         s = "A 002";
  153.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  154.         s = "A   8";
  155.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  156.         s = "A  141";
  157.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  158.         s = "A  14";
  159.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  160.         s = "A   0";
  161.         printf ("The rank of '%s' is %ld\n", s.AsPtr(), tree.RankOf (s));
  162.     }
  163.  
  164.     i = 8;
  165.     tree.Remove ("A " + CL_String (i, 3));
  166.     cout << tree << flush;
  167.     printf ("-1----------------------------------\n\n");
  168.  
  169.     i = 20;
  170.     tree.Remove ("A " + CL_String (i, 3));
  171.     cout << tree << flush;
  172.     printf ("-2----------------------------------\n\n");
  173.  
  174.     i = 29;
  175.     tree.Remove ("A " + CL_String (i, 3));
  176.     cout << tree << flush;
  177.     printf ("-3----------------------------------\n\n");
  178.  
  179.     i = 17;
  180.     tree.Remove ("A " + CL_String (i, 3));
  181.     cout << tree << flush;
  182.     printf ("--4---------------------------------\n\n");
  183.  
  184.     i = 2;
  185.     tree.Remove ("A " + CL_String (i, 3));
  186.     cout << tree << flush;
  187.     printf ("---5--------------------------------\n\n");
  188.  
  189.     i = 5;
  190.     tree.Remove ("A " + CL_String (i, 3));
  191.     cout << tree << flush;
  192.     printf ("-6----------------------------------\n\n");
  193.  
  194.     for (i = 0; i <= 80; i++) {
  195.         tree.Remove ("A " + CL_String (i, 3));
  196.         cout << tree << flush;
  197.         printf ("-7----------------------------------\n\n");
  198.     }
  199.     cout << tree << flush;
  200.     printf ("-8----------------------------------\n\n");
  201.  
  202.  
  203.  
  204.     cout << tree << flush;
  205.     printf ("-9----------------------------------\n\n");
  206.     
  207.     for (i = 0; i < 40; i++) {
  208.         switch (i % 3) {
  209.         case 0:
  210.             s = "K " + CL_String(i);
  211.             break;
  212.             
  213.         case 1:
  214.             s = "B " + CL_String(i);
  215.             break;
  216.             
  217.         case 2:
  218.             s = "Q " + CL_String(i);
  219.             break;
  220.         }
  221.         tree.Add (s);
  222.         cout << tree << flush;
  223.     // printf ("-----------------------------------\n\n");
  224.     }
  225.             
  226.     tree.Add ("Z Goodbye, cruel world!");
  227.     cout << tree << flush;
  228.     // return 0;
  229.  
  230.     printf ("-10--------------------------------------\n\n");
  231.     tree.Remove ("Q 2");
  232.     cout << tree << flush;
  233.     printf ("-11--------------------------------------\n\n");
  234.     tree.Remove ("Q 29");
  235.     cout << tree << flush;
  236.     printf ("-12--------------------------------------\n\n");
  237.     tree.Remove ("Q 27");
  238.     cout << tree << flush;
  239.  
  240.  
  241.     return 0;
  242. }
  243.  
  244.  
  245.     
  246.