Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/dataStructures/searchTrees/SaBstSet.cpp
Go to the documentation of this file.
00001 
00008 #include "SaBstSet.h"
00009 
00010 #define left(x) node[x].left
00011 #define right(x) node[x].right
00012 #define p(x) node[x].p
00013 #define kee(x) node[x].kee
00014 
00015 namespace grafalgo {
00016 
00020 SaBstSet::SaBstSet(int size) : BstSet(size) {}
00021 
00023 SaBstSet::~SaBstSet() {}
00024 
00030 index SaBstSet::splay(index x) {
00031         while (p(x) != 0) splaystep(x);
00032         return x;
00033 }
00034 
00038 void SaBstSet::splaystep(index x) {
00039         index y = p(x);
00040         if (y == 0) return;
00041         index z = p(y);
00042         if (z != 0) {
00043                 if (x == left(left(z)) || x == right(right(z)))
00044                         rotate(y);
00045                 else // x is "inner grandchild"
00046                         rotate(x);
00047         }
00048         rotate(x);
00049 }
00050 
00056 bst SaBstSet::find(index i) { return splay(i); }
00057 
00065 index SaBstSet::access(keytyp k, bst& t) {
00066         assert (0 <= t && t <= n());
00067         index x = t;
00068         while (true) {
00069                      if (k < kee(x) && left(x) != 0) x = left(x);
00070                 else if (k > kee(x) && right(x) != 0) x = right(x);
00071                 else break;
00072         }
00073         splay(x); t = x;
00074         return key(x) == k ? x : 0;
00075 }
00076 
00083 bool SaBstSet::insert(index i, bst& t) {
00084         if (t == 0) { t = i; return true; }
00085         index x = t;
00086         while (1) {
00087                      if (kee(i) < kee(x) &&  left(x) != 0) x = left(x);
00088                 else if (kee(i) > kee(x) && right(x) != 0) x = right(x);
00089                 else break;
00090         }
00091              if (kee(i) < kee(x))  left(x) = i;
00092         else if (kee(i) > kee(x)) right(x) = i;
00093         else { splay(x); return false; }
00094         p(i) = x;
00095         splay(i); t = i;
00096         return true;
00097 }
00098 
00105 void SaBstSet::remove(index i, bst& t) {
00106         assert(1 <= i && i <= n() && 1 <= t && t <= n());
00107         index j;
00108         if (left(i) != 0 && right(i) != 0) {
00109                 for (j = left(i); right(j) != 0; j = right(j)) {}
00110                 swap(i,j);
00111         }
00112         // now, i has at most one child
00113         j = (left(i) != 0 ? left(i) : right(i));
00114         // j is now the index of the only child that could be non-null
00115         if (j != 0) p(j) = p(i);
00116         if (p(i) != 0) {
00117                      if (i ==  left(p(i)))  left(p(i)) = j;
00118                 else if (i == right(p(i))) right(p(i)) = j;
00119                 t = splay(p(i));
00120         } else t = j;
00121         p(i) = left(i) = right(i) = 0;
00122         return;
00123 }
00124 
00132 BstSet::BstPair SaBstSet::split(index i, bst t) {
00133         assert(1 <= i && i <= n() && 1 <= t && t <= n());
00134         splay(i);
00135         BstSet::BstPair pair(left(i),right(i));
00136         left(i) = right(i) = p(i) = 0;
00137         p(pair.t1) = p(pair.t2) = 0;
00138         return pair;
00139 }
00140 
00141 } // ends namespace
 All Classes Files Functions Variables Typedefs Friends