22 #include <boost/shared_ptr.hpp> 23 #include <boost/function.hpp> 31 template<
class KEY,
class VALUE>
36 typedef std::pair<KEY, VALUE> value_type;
46 const value_type keyValue_;
47 const BTree left, right;
57 Node(
const value_type& keyValue) :
58 height_(1), keyValue_(keyValue) {
64 Node(
const BTree& l,
const value_type& keyValue,
const BTree& r) :
66 keyValue_(keyValue), left(l), right(r) {
69 inline const KEY& key()
const {
return keyValue_.first;}
70 inline const VALUE& value()
const {
return keyValue_.second;}
76 typedef boost::shared_ptr<const Node> sharedNode;
79 inline const value_type& keyValue()
const {
return root_->keyValue_;}
80 inline const KEY& key()
const {
return root_->key(); }
81 inline const VALUE& value()
const {
return root_->value(); }
82 inline const BTree& left()
const {
return root_->left; }
83 inline const BTree& right()
const {
return root_->right; }
86 static BTree balance(
const BTree& l,
const value_type& xd,
const BTree& r) {
89 const BTree& ll = l.left(), lr = l.right();
90 if (ll.
height() >= lr.height())
91 return BTree(ll, l.keyValue(),
BTree(lr, xd, r));
93 BTree _left(ll, l.keyValue(), lr.left());
94 BTree _right(lr.right(), xd, r);
95 return BTree(_left, lr.keyValue(), _right);
97 }
else if (hr > hl + 2) {
98 const BTree& rl = r.left(), rr = r.right();
99 if (rr.height() >= rl.
height())
100 return BTree(
BTree(l, xd, rl), r.keyValue(), rr);
102 BTree _left(l, xd, rl.left());
103 BTree _right(rl.right(), r.keyValue(), rr);
104 return BTree(_left, rl.keyValue(), _right);
107 return BTree(l, xd, r);
123 root_(new Node(keyValue)) {
128 root_(new Node(l, keyValue, r)) {
145 const KEY& x = xd.first;
147 return BTree(left(), xd, right());
149 return balance(left().
add(xd), keyValue(), right());
151 return balance(left(), keyValue(), right().
add(xd));
156 return add(std::make_pair(x, d));
160 bool mem(
const KEY& x)
const {
161 if (!root_)
return false;
162 if (x == key())
return true;
164 return left().
mem(x);
166 return right().
mem(x);
171 return (other.root_ == root_);
179 if (other.root_ == root_)
return true;
183 return (keyValue() == other.keyValue()) && (left() == other.left())
184 && (right() == other.right());
187 inline bool operator!=(
const BTree& other)
const {
192 const value_type&
min()
const {
193 if (!root_)
throw std::invalid_argument(
"BTree::min: empty tree");
194 if (left().
empty())
return keyValue();
200 if (!root_)
throw std::invalid_argument(
"BTree::remove_min: empty tree");
201 if (left().
empty())
return right();
202 return balance(left().
remove_min(), keyValue(), right());
207 if (t1.
empty())
return t2;
208 if (t2.
empty())
return t1;
209 const value_type& xd = t2.
min();
215 if (!root_)
return BTree();
217 return merge(left(), right());
219 return balance(left().
remove(x), keyValue(), right());
221 return balance(left(), keyValue(), right().
remove(x));
226 return (root_ != NULL) ? root_->height_ : 0;
231 if (!root_)
return 0;
232 return left().
size() + 1 + right().
size();
239 const VALUE&
find(
const KEY& k)
const {
240 const Node* node = root_.get();
242 const KEY& key = node->key();
243 if (k < key) node = node->left.root_.get();
244 else if (key < k) node = node->right.root_.get();
245 else return node->value();
248 throw std::invalid_argument(
"BTree::find: key not found");
252 void print(
const std::string& s =
"")
const {
255 std::stringstream ss;
257 k.print(s + ss.str() +
" ");
258 left().
print(s +
"L ");
259 right().
print(s +
"R ");
263 void iter(boost::function<
void(
const KEY&,
const VALUE&)> f)
const {
274 std::pair<KEY, TO> xd(key(), f(key(), value()));
285 ACC
fold(boost::function<ACC(
const KEY&,
const VALUE&,
const ACC&)> f,
286 const ACC& a)
const {
287 if (!root_)
return a;
288 ACC ar = right().
fold(f, a);
289 ACC am = f(key(), value(), ar);
290 return left().
fold(f, am);
302 typedef std::pair<sharedNode, bool> flagged;
305 std::stack<flagged> path_;
307 const sharedNode& current()
const {
308 return path_.top().first;
312 return path_.top().second;
320 if (path_.empty())
return;
321 sharedNode t = current()->right.root_;
325 while (!path_.empty() && done())
328 path_.top().second =
true;
331 path_.push(std::make_pair(t,
false));
340 typedef ptrdiff_t difference_type;
341 typedef std::forward_iterator_tag iterator_category;
342 typedef std::pair<KEY, VALUE> value_type;
343 typedef const value_type* pointer;
344 typedef const value_type& reference;
354 path_.push(std::make_pair(t,
false));
361 return path_ == __x.path_;
366 return path_ != __x.path_;
371 if (path_.empty())
throw std::invalid_argument(
372 "operator*: tried to dereference end");
373 return current()->keyValue_;
378 if (path_.empty())
throw std::invalid_argument(
379 "operator->: tried to dereference end");
380 return &(current()->keyValue_);
400 typedef const_iterator iterator;
404 return const_iterator(root_);
408 const_iterator
end()
const {
409 return const_iterator();
Self & operator++()
pre-increment
Definition: BTree.h:384
bool operator==(const BTree &other) const
Check whether trees are structurally the same, i.e., contain the same values in same tree-structure.
Definition: BTree.h:178
BTree remove(const KEY &x) const
remove a key-value pair
Definition: BTree.h:214
void print(const std::string &s="") const
print in-order
Definition: BTree.h:252
pointer operator->() const
dereference
Definition: BTree.h:377
void iter(boost::function< void(const KEY &, const VALUE &)> f) const
iterate over tree
Definition: BTree.h:263
BTree remove_min() const
remove minimum key binding
Definition: BTree.h:199
bool operator!=(const Self &__x) const
inequality
Definition: BTree.h:365
size_t size() const
return size of the tree
Definition: BTree.h:230
const_iterator()
initialize end
Definition: BTree.h:347
bool operator==(const Self &__x) const
equality
Definition: BTree.h:360
reference operator *() const
dereference
Definition: BTree.h:370
const_iterator end() const
return iterator
Definition: BTree.h:408
bool mem(const KEY &x) const
member predicate
Definition: BTree.h:160
size_t height() const
Return height of the tree, 0 if empty.
Definition: BTree.h:225
ACC fold(boost::function< ACC(const KEY &, const VALUE &, const ACC &)> f, const ACC &a) const
t.fold(f,a) computes [(f kN dN ...
Definition: BTree.h:285
Const iterator Not trivial: iterator keeps a stack to indicate current path from root_.
Definition: BTree.h:297
BTree()
default constructor creates an empty tree
Definition: BTree.h:113
const_iterator(const sharedNode &root)
initialize from root
Definition: BTree.h:351
BTree< KEY, TO > map(boost::function< TO(const KEY &, const VALUE &)> f) const
map key-values in tree over function f that computes a new value
Definition: BTree.h:272
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
const_iterator begin() const
return iterator
Definition: BTree.h:403
const value_type & min() const
minimum key binding
Definition: BTree.h:192
BTree add(const KEY &x, const VALUE &d) const
add a key-value pair
Definition: BTree.h:155
Self operator++(int)
post-increment
Definition: BTree.h:390
BTree(const value_type &keyValue)
create leaf from key-value pair
Definition: BTree.h:122
BTree(const BTree &other)
copy constructor
Definition: BTree.h:117
static BTree merge(const BTree &t1, const BTree &t2)
merge two trees
Definition: BTree.h:206
const VALUE & find(const KEY &k) const
find a value given a key, throws exception when not found Optimized non-recursive version as [find] i...
Definition: BTree.h:239
BTree add(const value_type &xd) const
add a key-value pair
Definition: BTree.h:143
bool empty() const
Check whether tree is empty.
Definition: BTree.h:138
BTree & operator=(const BTree &other)
assignment operator
Definition: BTree.h:132
bool same(const BTree &other) const
Check whether trees are exactly the same (occupy same memory)
Definition: BTree.h:170
BTree(const BTree &l, const value_type &keyValue, const BTree &r)
create from key-value pair and left, right subtrees
Definition: BTree.h:127