24#include <boost/shared_ptr.hpp>
33 template<
class KEY,
class VALUE>
38 typedef std::pair<KEY, VALUE> value_type;
48 const value_type keyValue_;
49 const BTree left, right;
59 Node(
const value_type& keyValue) :
60 height_(1), keyValue_(keyValue) {
66 Node(
const BTree& l,
const value_type& keyValue,
const BTree& r) :
68 keyValue_(keyValue), left(l), right(r) {
71 inline const KEY& key()
const {
return keyValue_.first;}
72 inline const VALUE& value()
const {
return keyValue_.second;}
78 typedef boost::shared_ptr<const Node> sharedNode;
81 inline const value_type& keyValue()
const {
return root_->keyValue_;}
82 inline const KEY& key()
const {
return root_->key(); }
83 inline const VALUE& value()
const {
return root_->value(); }
84 inline const BTree& left()
const {
return root_->left; }
85 inline const BTree& right()
const {
return root_->right; }
88 static BTree balance(
const BTree& l,
const value_type& xd,
const BTree& r) {
91 const BTree& ll = l.left(), lr = l.right();
92 if (ll.
height() >= lr.height())
93 return BTree(ll, l.keyValue(),
BTree(lr, xd, r));
95 BTree _left(ll, l.keyValue(), lr.left());
96 BTree _right(lr.right(), xd, r);
97 return BTree(_left, lr.keyValue(), _right);
99 }
else if (hr > hl + 2) {
100 const BTree& rl = r.left(), rr = r.right();
101 if (rr.height() >= rl.
height())
102 return BTree(
BTree(l, xd, rl), r.keyValue(), rr);
104 BTree _left(l, xd, rl.left());
105 BTree _right(rl.right(), r.keyValue(), rr);
106 return BTree(_left, rl.keyValue(), _right);
109 return BTree(l, xd, r);
125 root_(new Node(keyValue)) {
130 root_(new Node(l, keyValue, r)) {
147 const KEY& x = xd.first;
149 return BTree(left(), xd, right());
151 return balance(left().
add(xd), keyValue(), right());
153 return balance(left(), keyValue(), right().
add(xd));
158 return add(std::make_pair(x, d));
162 bool mem(
const KEY& x)
const {
163 if (!root_)
return false;
164 if (x == key())
return true;
166 return left().mem(x);
168 return right().mem(x);
173 return (other.root_ == root_);
181 if (other.root_ == root_)
return true;
185 return (keyValue() == other.keyValue()) && (left() == other.left())
186 && (right() == other.right());
194 const value_type&
min()
const {
195 if (!root_)
throw std::invalid_argument(
"BTree::min: empty tree");
196 if (left().
empty())
return keyValue();
202 if (!root_)
throw std::invalid_argument(
"BTree::remove_min: empty tree");
203 if (left().
empty())
return right();
204 return balance(left().
remove_min(), keyValue(), right());
209 if (t1.
empty())
return t2;
210 if (t2.
empty())
return t1;
211 const value_type& xd = t2.
min();
217 if (!root_)
return BTree();
219 return merge(left(), right());
221 return balance(left().
remove(x), keyValue(), right());
223 return balance(left(), keyValue(), right().
remove(x));
228 return (root_ !=
nullptr) ? root_->height_ : 0;
233 if (!root_)
return 0;
234 return left().size() + 1 + right().size();
241 const VALUE&
find(
const KEY& k)
const {
242 const Node* node = root_.get();
244 const KEY& key = node->key();
245 if (k < key) node = node->left.root_.get();
246 else if (key < k) node = node->right.root_.get();
247 else return node->value();
250 throw std::invalid_argument(
"BTree::find: key not found");
254 void print(
const std::string& s =
"")
const {
257 std::stringstream ss;
259 k.print(s + ss.str() +
" ");
260 left().print(s +
"L ");
261 right().print(s +
"R ");
265 void iter(std::function<
void(
const KEY&,
const VALUE&)> f)
const {
276 std::pair<KEY, TO> xd(key(), f(key(), value()));
287 ACC
fold(std::function<ACC(
const KEY&,
const VALUE&,
const ACC&)> f,
288 const ACC& a)
const {
289 if (!root_)
return a;
290 ACC ar = right().fold(f, a);
291 ACC am = f(key(), value(), ar);
292 return left().fold(f, am);
304 typedef std::pair<sharedNode, bool> flagged;
307 std::stack<flagged> path_;
309 const sharedNode& current()
const {
310 return path_.top().first;
314 return path_.top().second;
322 if (path_.empty())
return;
323 sharedNode t = current()->right.root_;
327 while (!path_.empty() && done())
330 path_.top().second =
true;
333 path_.push(std::make_pair(t,
false));
342 typedef ptrdiff_t difference_type;
343 typedef std::forward_iterator_tag iterator_category;
344 typedef std::pair<KEY, VALUE> value_type;
345 typedef const value_type* pointer;
346 typedef const value_type& reference;
356 path_.push(std::make_pair(t,
false));
363 return path_ == __x.path_;
368 return path_ != __x.path_;
373 if (path_.empty())
throw std::invalid_argument(
374 "operator*: tried to dereference end");
375 return current()->keyValue_;
380 if (path_.empty())
throw std::invalid_argument(
381 "operator->: tried to dereference end");
382 return &(current()->keyValue_);
402 typedef const_iterator iterator;
406 return const_iterator(root_);
410 const_iterator
end()
const {
411 return const_iterator();
Global functions in a separate testing namespace.
Definition chartTesting.h:28
bool operator!=(const Matrix &A, const Matrix &B)
inequality
Definition Matrix.h:107
Binary tree.
Definition BTree.h:34
bool mem(const KEY &x) const
member predicate
Definition BTree.h:162
BTree< KEY, TO > map(std::function< TO(const KEY &, const VALUE &)> f) const
map key-values in tree over function f that computes a new value
Definition BTree.h:274
BTree(const value_type &keyValue)
create leaf from key-value pair
Definition BTree.h:124
BTree remove_min() const
remove minimum key binding
Definition BTree.h:201
ACC fold(std::function< ACC(const KEY &, const VALUE &, const ACC &)> f, const ACC &a) const
t.fold(f,a) computes [(f kN dN ... (f k1 d1 a)...)], where [k1 ... kN] are the keys of all bindings i...
Definition BTree.h:287
BTree add(const value_type &xd) const
add a key-value pair
Definition BTree.h:145
BTree(const BTree &other)
copy constructor
Definition BTree.h:119
size_t height() const
Return height of the tree, 0 if empty.
Definition BTree.h:227
bool same(const BTree &other) const
Check whether trees are exactly the same (occupy same memory).
Definition BTree.h:172
void iter(std::function< void(const KEY &, const VALUE &)> f) const
iterate over tree
Definition BTree.h:265
const_iterator begin() const
return iterator
Definition BTree.h:405
BTree(const BTree &l, const value_type &keyValue, const BTree &r)
create from key-value pair and left, right subtrees
Definition BTree.h:129
BTree add(const KEY &x, const VALUE &d) const
add a key-value pair
Definition BTree.h:157
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:180
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:241
void print(const std::string &s="") const
print in-order
Definition BTree.h:254
BTree remove(const KEY &x) const
remove a key-value pair
Definition BTree.h:216
static BTree merge(const BTree &t1, const BTree &t2)
merge two trees
Definition BTree.h:208
const_iterator end() const
return iterator
Definition BTree.h:410
size_t size() const
return size of the tree
Definition BTree.h:232
bool empty() const
Check whether tree is empty.
Definition BTree.h:140
BTree()
default constructor creates an empty tree
Definition BTree.h:115
const value_type & min() const
minimum key binding
Definition BTree.h:194
BTree & operator=(const BTree &other)
assignment operator
Definition BTree.h:134
bool operator==(const Self &__x) const
equality
Definition BTree.h:362
const_iterator()
initialize end
Definition BTree.h:349
Self operator++(int)
post-increment
Definition BTree.h:392
Self & operator++()
pre-increment
Definition BTree.h:386
pointer operator->() const
dereference
Definition BTree.h:379
reference operator*() const
dereference
Definition BTree.h:372
const_iterator(const sharedNode &root)
initialize from root
Definition BTree.h:353
bool operator!=(const Self &__x) const
inequality
Definition BTree.h:367