Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #ifndef BSTSET_H 00010 #define BSTSET_H 00011 00012 #include "Adt.h" 00013 #include "Util.h" 00014 00015 namespace grafalgo { 00016 00017 typedef int bst; // tree in collection 00018 typedef uint64_t keytyp; 00019 00026 class BstSet : public Adt { 00027 public: 00028 BstSet(int); 00029 ~BstSet(); 00030 00031 // pair of bsts, returned by split 00032 struct BstPair { 00033 bst t1, t2; 00034 BstPair(bst tt1, bst tt2) : t1(tt1), t2(tt2) {} 00035 }; 00036 00037 // common methods 00038 void clear(); 00039 void resize(int); 00040 void expand(int); 00041 void copyFrom(const BstSet&); 00042 00043 keytyp key(index) const; 00044 bst find(index) const; 00045 index first(bst) const; 00046 index last(bst) const; 00047 index suc(index) const; 00048 index pred(index) const; 00049 index access(keytyp,bst&) const; 00050 00051 void setkey(index,keytyp); 00052 bool insert(index,bst&); 00053 void remove(index,bst&); 00054 bst join(bst,index,bst); 00055 BstSet::BstPair split(index,bst); 00056 00057 string& bst2string(bst, string&) const; 00058 string& toString(string&) const; 00059 protected: 00060 struct BstNode { 00061 index left, right, p; // left child, right child, parent 00062 keytyp kee; // key values 00063 }; 00064 BstNode *node; 00065 00066 void virtual swap(index,index); 00067 void virtual rotate(index); 00068 index sibling(index, index); 00069 index remove(index); 00070 00071 virtual string& node2string(index, string&) const; 00072 00073 void makeSpace(int); 00074 void freeSpace(); 00075 }; 00076 00081 inline keytyp BstSet::key(index i) const { return node[i].kee; } 00082 00087 inline void BstSet::setkey(index i, keytyp k) { 00088 assert(node[i].left == 0 && node[i].right == 0 && node[i].p == 0); 00089 node[i].kee = k; 00090 } 00091 00097 index inline BstSet:: sibling(index x, index px) { 00098 return (x == node[px].left ? node[px].right : node[px].left); 00099 } 00100 00101 } // ends namespace 00102 00103 #endif