home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / bstset.ccp < prev    next >
Encoding:
Text File  |  1993-07-23  |  5.2 KB  |  307 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #include <stream.h>
  25. #include "<T>.BSTSet.h"
  26.  
  27.  
  28. /*
  29.  traversal primitives
  30. */
  31.  
  32.  
  33. <T>BSTNode* <T>BSTSet::leftmost()
  34. {
  35.   <T>BSTNode* t = root;
  36.   if (t != 0) while (t->lt != 0) t = t->lt;
  37.   return t;
  38. }
  39.  
  40. <T>BSTNode* <T>BSTSet::rightmost()
  41. {
  42.   <T>BSTNode* t = root;
  43.   if (t != 0) while (t->rt != 0) t = t->rt;
  44.   return t;
  45. }
  46.  
  47. <T>BSTNode* <T>BSTSet::succ(<T>BSTNode* t)
  48. {
  49.   if (t == 0)
  50.     return 0;
  51.   if (t->rt != 0)
  52.   {
  53.     t = t->rt;
  54.     while (t->lt != 0) t = t->lt;
  55.     return t;
  56.   }
  57.   else
  58.   {
  59.     for (;;)
  60.     {
  61.       if (t->par == 0 || t == t->par->lt)
  62.         return t->par;
  63.       else
  64.         t = t->par;
  65.     }
  66.   }
  67. }
  68.  
  69. <T>BSTNode* <T>BSTSet::pred(<T>BSTNode* t)
  70. {
  71.   if (t == 0)
  72.     return 0;
  73.   else if (t->lt != 0)
  74.   {
  75.     t = t->lt;
  76.     while (t->rt != 0) t = t->rt;
  77.     return t;
  78.   }
  79.   else
  80.   {
  81.     for (;;)
  82.     {
  83.       if (t->par == 0 || t == t->par->rt)
  84.         return t->par;
  85.       else
  86.         t = t->par;
  87.     }
  88.   }
  89. }
  90.  
  91.  
  92. Pix <T>BSTSet::seek(<T&> key)
  93. {
  94.   <T>BSTNode* t = root;
  95.   for (;;)
  96.   {
  97.     if (t == 0)
  98.       return 0;
  99.     int comp = <T>CMP(key, t->item);
  100.     if (comp == 0)
  101.       return Pix(t);
  102.     else if (comp < 0)
  103.       t = t->lt;
  104.     else
  105.       t = t->rt;
  106.   }
  107. }
  108.  
  109.  
  110. Pix <T>BSTSet::add(<T&> item)
  111. {
  112.   if (root == 0)
  113.   {
  114.     ++count;
  115.     root = new <T>BSTNode(item);
  116.     return Pix(root);
  117.   }
  118.  
  119.   <T>BSTNode* t = root;
  120.   <T>BSTNode* p = root;
  121.   int comp;
  122.   for (;;)
  123.   {
  124.     if (t == 0)
  125.     {
  126.       ++count;
  127.       t = new <T>BSTNode(item);
  128.       if (comp > 0)
  129.         p->lt = t;
  130.       else
  131.         p->rt = t;
  132.       t->par = p;
  133.       return Pix(t);
  134.     }
  135.     p = t;
  136.     comp = <T>CMP(t->item, item);
  137.     if (comp == 0)
  138.       return Pix(t);
  139.     else if (comp > 0)
  140.       t = t->lt;
  141.     else
  142.       t = t->rt;
  143.   }
  144. }
  145.  
  146.  
  147. void <T>BSTSet::del(<T&> key)
  148. {
  149.   <T>BSTNode* t = root;
  150.   <T>BSTNode* p = root;
  151.   int comp;
  152.   for (;;)
  153.   {
  154.     if (t == 0)
  155.       return;
  156.     comp = <T>CMP(key, t->item);
  157.     if (comp == 0)
  158.     {
  159.       --count;
  160.       <T>BSTNode* repl;
  161.       if (t->lt == 0)
  162.         repl = t->rt;
  163.       else if (t->rt == 0)
  164.         repl = t->lt;
  165.       else
  166.       {
  167.         <T>BSTNode* prepl = t;
  168.         repl = t->lt;
  169.         while (repl->rt != 0)
  170.         {
  171.           prepl = repl;
  172.           repl = repl->rt;
  173.         }
  174.         if (prepl != t)
  175.         {
  176.           prepl->rt = repl->lt;
  177.           if (prepl->rt != 0) prepl->rt->par = prepl;
  178.           repl->lt = t->lt;
  179.           if (repl->lt != 0) repl->lt->par = repl;
  180.         }
  181.         repl->rt = t->rt;
  182.         if (repl->rt != 0) repl->rt->par = repl;
  183.       }
  184.       if (t == root)
  185.       {
  186.         root = repl;
  187.         if (repl != 0) repl->par = 0;
  188.       }
  189.       else
  190.       {
  191.         if (t == p->lt)
  192.           p->lt = repl;
  193.         else
  194.           p->rt = repl;
  195.         if (repl != 0) repl->par = p;
  196.       }
  197.       delete t;
  198.       return;
  199.     }
  200.     p = t;
  201.     if (comp < 0)
  202.       t = t->lt;
  203.     else
  204.       t = t->rt;
  205.   }
  206. }
  207.  
  208.  
  209. void <T>BSTSet::_kill(<T>BSTNode* t)
  210. {
  211.   if (t != 0)
  212.   {
  213.     _kill(t->lt);
  214.     _kill(t->rt);
  215.     delete t;
  216.   }
  217. }
  218.  
  219. <T>BSTNode* <T>BSTSet::_copy(<T>BSTNode* t)
  220. {
  221.   if (t == 0)
  222.     return 0;
  223.   else
  224.   {
  225.     <T>BSTNode* u = new <T>BSTNode(t->item, _copy(t->lt), _copy(t->rt));
  226.     if (u->lt != 0) u->lt->par = u;
  227.     if (u->rt != 0) u->rt->par = u;
  228.     return u;
  229.   }
  230. }
  231.  
  232.  
  233. int <T>BSTSet::operator == (<T>BSTSet& y)
  234. {
  235.   if (count != y.count)
  236.     return 0;
  237.   else
  238.   {
  239.     <T>BSTNode* t = leftmost();
  240.     <T>BSTNode* u = y.leftmost();
  241.     for (;;)
  242.     {
  243.       if (t == 0)
  244.         return 1;
  245.       else if (!<T>EQ(t->item, u->item))
  246.         return 0;
  247.       else
  248.       {
  249.         t = succ(t);
  250.         u = y.succ(u);
  251.       }
  252.     }
  253.   }
  254. }
  255.  
  256. int <T>BSTSet::operator <= (<T>BSTSet& y)
  257. {
  258.   if (count > y.count)
  259.     return 0;
  260.   else
  261.   {
  262.     <T>BSTNode* t = leftmost();
  263.     <T>BSTNode* u = y.leftmost();
  264.     for (;;)
  265.     {
  266.       if (t == 0)
  267.         return 1;
  268.       else if (u == 0)
  269.         return 0;
  270.       int cmp = <T>CMP(t->item, u->item);
  271.       if (cmp == 0)
  272.       {
  273.         t = succ(t);
  274.         u = y.succ(u);
  275.       }
  276.       else if (cmp < 0)
  277.         return 0;
  278.       else
  279.         u = y.succ(u);
  280.     }
  281.   }
  282. }
  283.  
  284.  
  285. int <T>BSTSet::OK()
  286. {
  287.   int v = 1;
  288.   if (root == 0) 
  289.     v = count == 0;
  290.   else
  291.   {
  292.     int n = 1;
  293.     <T>BSTNode* trail = leftmost();
  294.     <T>BSTNode* t = succ(trail);
  295.     while (t != 0)
  296.     {
  297.       ++n;
  298.       v &= <T>CMP(trail->item, t->item) < 0;
  299.       trail = t;
  300.       t = succ(t);
  301.     }
  302.     v &= n == count;
  303.   }
  304.   if (!v) error("invariant failure");
  305.   return v;
  306. }
  307.