36 template <
typename Key>
38 BinTreeNodeNullValue::SENTINEL>
60 :
BaseNode(std::forward<Key>(k)), count(1), prior(0)
88 template <
class RkNode>
91 return p->get_count();
94 template <
class TreapNode>
97 return p->get_priority();
101 template <
typename Key,
class Cmp = std::less<Key>>
115 static bool verify(
Node *, Cmp & cmp);
117 static bool verify_dup(
Node *, Cmp & cmp);
121 static void destroy(
Node *&);
129 static bool split_key(
Node *,
const Key &,
Node *&,
Node *&, Cmp &);
131 static void split_key_dup(
Node *,
const Key &,
Node *&,
Node *&,
136 static void join_dup(
Node *&,
Node *&, Cmp &);
142 static Node * search(
Node *,
const Key &, Cmp &);
144 static Node * search(
Node *, Key &&, Cmp &);
148 static Node * remove_root(
Node *& root)
150 Node * ret_val = root;
151 root = exclusive_join(
L(root),
R(root));
155 static Node *
remove(
Node *&,
const Key &, Cmp &);
157 static Node * remove_pos(Node *&,
nat_t);
159 static Node * select(Node *,
nat_t);
161 static lint_t position(Node *,
const Key &, Cmp &);
163 static Node * min(Node *);
165 static Node * max(Node *);
168 static void preorder_rec(Node *, Op &);
171 static void inorder_rec(Node *, Op &);
174 static void postorder_rec(Node *, Op &);
176 Key * insert(Node * p)
180 if (insert(root, p, cmp) != Node::null)
187 Key * insert_dup(Node * p)
190 return &
KEY(insert_dup(root, p, cmp));
193 Key * search_or_insert(Node * p)
197 Node * result = search_or_insert(root, p, cmp);
215 return verify(root, cmp);
220 return verify_dup(root, cmp);
224 : head(), root(
L(&head)), cmp(_cmp), rng(seed)
285 std::swap(root, t.root);
286 std::swap(cmp, t.cmp);
291 return root == Node::null;
321 Node * p =
new Node(k);
327 Node * p =
new Node(std::forward<Key>(k));
333 Node * p =
new Node(k);
334 return insert_dup(p);
339 Node * p =
new Node(std::forward<Key>(k));
340 return insert_dup(p);
350 return insert(std::forward<Key>(k));
355 return insert_dup(k);
360 return insert(std::forward<Key>(k));
365 Node * result = search(root, k, cmp);
367 if (result == Node::null)
375 Node * result = search(root, k, cmp);
377 if (result == Node::null)
385 Node * p =
new Node(k);
386 return search_or_insert(p);
391 Node * p =
new Node(std::forward<Key>(k));
392 return search_or_insert(p);
397 Key * result = search(k);
399 if (result ==
nullptr)
400 throw std::domain_error(
"Key not found");
405 const Key &
find(
const Key & k)
const 407 const Key * result = search(k);
409 if (result ==
nullptr)
410 throw std::domain_error(
"Key not found");
415 bool remove(
const Key & k)
418 Node * result =
remove(root, k, cmp);
420 if (result == Node::null)
430 throw std::out_of_range(
"Infix position is out of range");
432 Node * result = remove_pos(root, i);
433 Key ret_val = std::move(
KEY(result));
441 throw std::underflow_error(
"Tree is empty");
443 return KEY(min(root));
449 throw std::underflow_error(
"Tree is empty");
451 return KEY(max(root));
457 throw std::out_of_range(
"Infix position is out of range");
460 split_pos(root, i, ts.root, tg.root);
462 return std::make_tuple(std::move(ts), std::move(tg));
465 std::tuple<RankedTreap, RankedTreap>
split_key(
const Key & k)
469 if (split_key(root, k, ts.root, tg.root, cmp))
472 return std::make_tuple(std::move(ts), std::move(tg));
478 split_key_dup(root, k, ts.root, tg.root, cmp);
480 return std::make_tuple(std::move(ts), std::move(tg));
485 root = exclusive_join(ts.root, tg.root);
486 ts.root = tg.root = Node::null;
491 join_dup(ts.root, tg.root, cmp);
493 ts.root = tg.root = Node::null;
499 throw std::out_of_range(
"Infix position is out of range");
501 return KEY(select(root, i));
507 throw std::out_of_range(
"Infix position is out of range");
509 return KEY(select(root, i));
514 return position(root, k, cmp);
522 const Key & operator [] (
nat_t i)
const 530 preorder_rec<Op>(root, op);
536 for_each_preorder<Op>(op);
542 inorder_rec<Op>(root, op);
548 for_each_inorder<Op>(op);
554 postorder_rec<Op>(root, op);
560 for_each_postorder<Op>(op);
568 Node * root = Node::null;
569 Node * curr = Node::null;
576 : root(t.root), curr(Node::
null), pos(t.size())
588 : root(t.root), curr(root)
594 : stack(it.stack), root(it.root), curr(it.curr), pos(it.pos)
625 std::swap(stack, it.stack);
626 std::swap(root, it.root);
627 std::swap(curr, it.curr);
628 std::swap(pos, it.pos);
645 return curr != Node::null;
650 if (not has_current())
651 throw std::overflow_error(
"There is not current element");
658 if (not has_current())
659 throw std::overflow_error(
"There is not current element");
666 if (not has_current())
675 if (curr != Node::null)
678 while (not stack.
is_empty() and curr == Node::null)
679 curr =
R(stack.
pop());
688 Node * root = Node::null;
689 Node * curr = Node::null;
692 Node * search_min(Node *);
694 Node * search_max(Node *);
698 if (root == Node::null)
702 curr = search_min(root);
707 : root(t.root), curr(Node::
null), pos(t.size())
725 : stack(it.stack), root(it.root), curr(it.curr), pos(it.pos)
756 std::swap(stack, it.stack);
757 std::swap(root, it.root);
758 std::swap(curr, it.curr);
759 std::swap(pos, it.pos);
772 curr = search_max(root);
782 return curr != Node::null;
787 if (not has_current())
788 throw std::overflow_error(
"There is not current element");
795 if (not has_current())
796 throw std::overflow_error(
"There is not current element");
803 if (not has_current())
804 throw std::overflow_error(
"There is not current element");
809 if (curr != Node::null)
810 curr = search_min(curr);
822 Node * root = Node::null;
823 Node * curr = Node::null;
826 Node * first(Node *);
830 if (root == Node::null)
839 : root(t.root), curr(Node::
null), pos(t.size())
857 : stack(it.stack), root(it.root), curr(it.curr), pos(it.pos)
888 std::swap(stack, it.stack);
889 std::swap(root, it.root);
890 std::swap(curr, it.curr);
891 std::swap(pos, it.pos);
914 return curr != Node::null;
919 if (not has_current())
920 throw std::overflow_error(
"There is not current element");
927 if (not has_current())
928 throw std::overflow_error(
"There is not current element");
935 if (not has_current())
936 throw std::overflow_error(
"There is not current element");
946 Node *& parent = stack.
top();
948 if (curr ==
R(parent) or
R(parent) == Node::null)
954 curr = first(
R(parent));
988 template <
typename Key,
class Cmp>
992 for (
const auto & item : l)
996 template <
typename Key,
class Cmp>
1015 test = test and cmp(
KEY(lc),
KEY(r));
1018 test = test and cmp(
KEY(r),
KEY(rc));
1023 template <
typename Key,
class Cmp>
1042 test = test and not cmp(
KEY(r),
KEY(lc));
1045 test = test and not cmp(
KEY(rc),
KEY(r));
1050 template <
typename Key,
class Cmp>
1064 template <
typename Key,
class Cmp>
1076 template <
typename Key,
class Cmp>
1090 template <
typename Key,
class Cmp>
1104 template <
typename Key,
class Cmp>
1106 Node *& ts, Node *& tg)
1119 split_pos(
L(r), i, ts,
L(r));
1125 split_pos(
R(r), i - (
COUNT(
L(r)) + 1),
R(r), tg);
1131 template <
typename Key,
class Cmp>
1133 Node *& ts, Node *& tg, Cmp & cmp)
1143 if (split_key(
L(r), k, ts,
L(r), cmp))
1150 else if (cmp(
KEY(r), k))
1152 if (split_key(
R(r), k,
R(r), tg, cmp))
1163 template <
typename Key,
class Cmp>
1165 Node *& ts, Node *& tg,
1176 split_key_dup(
L(r), k, ts,
L(r), cmp);
1182 split_key_dup(
R(r), k,
R(r), tg, cmp);
1188 template <
typename Key,
class Cmp>
1201 R(ts) = exclusive_join(
R(ts), tg);
1207 L(tg) = exclusive_join(ts,
L(tg));
1212 template <
typename Key,
class Cmp>
1222 insert_dup(t1, t2, cmp);
1223 join_dup(t1, l, cmp);
1224 join_dup(t1, r, cmp);
1227 template <
typename Key,
class Cmp>
1239 Node * result = insert(
L(r), p, cmp);
1247 r = result = rotate_right(r);
1251 else if (cmp(
KEY(r),
KEY(p)))
1253 Node * result = insert(
R(r), p, cmp);
1261 r = result = rotate_left(r);
1269 template <
typename Key,
class Cmp>
1281 Node * result = insert_dup(
L(r), p, cmp);
1289 r = result = rotate_right(r);
1294 Node * result = insert_dup(
R(r), p, cmp);
1302 r = result = rotate_left(r);
1307 template <
typename Key,
class Cmp>
1315 return search(
L(r), k, cmp);
1316 else if (cmp(
KEY(r), k))
1317 return search(
R(r), k, cmp);
1322 template <
typename Key,
class Cmp>
1330 return search(
L(r), std::forward<Key>(k), cmp);
1331 else if (cmp(
KEY(r), k))
1332 return search(
R(r), std::forward<Key>(k), cmp);
1338 template <
typename Key,
class Cmp>
1350 Node * result = search_or_insert(
L(r), p, cmp);
1357 r = result = rotate_right(r);
1362 else if (cmp(
KEY(r),
KEY(p)))
1364 Node * result = search_or_insert(
R(r), p, cmp);
1371 r = result = rotate_left(r);
1380 template <
typename Key,
class Cmp>
1389 Node * result =
remove(
L(r), k, cmp);
1396 else if (cmp(
KEY(r), k))
1398 Node * result =
remove(
R(r), k, cmp);
1406 return remove_root(r);
1409 template <
typename Key,
class Cmp>
1414 return remove_root(r);
1419 result = remove_pos(
L(r), i);
1421 result = remove_pos(
R(r), i -
COUNT(
L(r)) - 1);
1427 template <
typename Key,
class Cmp>
1435 return select(
L(r), i);
1437 return select(
R(r), i -
COUNT(
L(r)) - 1);
1440 template <
typename Key,
class Cmp>
1447 return position(
L(r), k, cmp);
1448 else if (cmp(
KEY(r), k))
1450 lint_t p = position(
R(r), k, cmp);
1455 return p +
COUNT(
L(r)) + 1;
1460 template <
typename Key,
class Cmp>
1469 template <
typename Key,
class Cmp>
1478 template <
typename Key,
class Cmp>
1486 preorder_rec(
L(r), op);
1487 preorder_rec(
R(r), op);
1490 template <
typename Key,
class Cmp>
1497 inorder_rec(
L(r), op);
1499 inorder_rec(
R(r), op);
1502 template <
typename Key,
class Cmp>
1509 postorder_rec(
L(r), op);
1510 postorder_rec(
R(r), op);
1514 template <
typename Key,
class Cmp>
1532 template <
typename Key,
class Cmp>
1545 template <
typename Key,
class Cmp>
1555 template <
typename Key,
class Cmp>
1579 # endif // DSGBINTREE_H PreorderIterator(const RankedTreap &t, int)
Definition: tree.H:575
bool is_sorted() const
Definition: tree.H:294
static TreapRkNode< Key > *const null
Definition: nodesdef.H:909
~RankedTreap()
Definition: tree.H:261
const Key & select(nat_t i) const
Definition: tree.H:504
Key ItemType
Definition: tree.H:206
Iterator end() const
Definition: tree.H:982
void for_each_postorder(Op &&op=Op())
Definition: tree.H:558
RankedTreap(Cmp &_cmp)
Definition: tree.H:229
void for_each_preorder(Op &op)
Definition: tree.H:528
void next()
Definition: tree.H:933
const Cmp & get_cmp() const
Definition: tree.H:314
Key * search_or_insert(Key &&k)
Definition: tree.H:389
Key & select(nat_t i)
Definition: tree.H:496
Definition: iterator.H:92
std::tuple< RankedTreap, RankedTreap > split_pos(nat_t i)
Definition: tree.H:454
rng_seed_t & get_priority()
Definition: tree.H:76
PostorderIterator(const RankedTreap &t, int)
Definition: tree.H:838
Definition: setalgorithms.H:36
TreapRkNode(BinTreeNodeCtor ctor)
Definition: tree.H:65
Iterator begin()
Definition: tree.H:967
BaseBinTreeNode & operator=(const BaseBinTreeNode &)=delete
bool has_current() const
Definition: tree.H:643
void join_dup(RankedTreap &ts, RankedTreap &tg)
Definition: tree.H:489
InorderIterator(const RankedTreap &t)
Definition: tree.H:718
Cmp CmpType
Definition: tree.H:211
Key * append(Key &&k)
Definition: tree.H:348
void swap(PreorderIterator &it)
Definition: tree.H:623
void for_each_inorder(Op &op)
Definition: tree.H:540
nat_t get_position() const
Definition: tree.H:638
Definition: iterator.H:36
Cmp & get_cmp()
Definition: tree.H:309
Key * append_dup(const Key &k)
Definition: tree.H:353
TreapRkNode< Key > Node
Definition: tree.H:106
const Key & max() const
Definition: tree.H:446
const Key & get_current() const
Definition: tree.H:656
PostorderIterator(PostorderIterator &&it)
Definition: tree.H:862
const Key & get_current() const
Definition: tree.H:925
Node * get_location() const
Definition: tree.H:844
void exclusive_join(RankedTreap &ts, RankedTreap &tg)
Definition: tree.H:483
std::mt19937_64 rng_t
Definition: types.H:52
BinTreeNode *& R(BinTreeNode *p)
Definition: nodesdef.H:993
Iterator end()
Definition: tree.H:977
Definition: nodesdef.H:902
void for_each_inorder(Op &&op=Op())
Definition: tree.H:546
Iterator begin() const
Definition: tree.H:972
Node * get_location() const
Definition: tree.H:581
Key & find(const Key &k)
Definition: tree.H:395
RankedTreap(rng_seed_t seed, Cmp &&_cmp=Cmp())
Definition: tree.H:241
bool is_empty() const
Definition: tree.H:289
Key * append_dup(Key &&k)
Definition: tree.H:358
PostorderIterator(const PostorderIterator &it)
Definition: tree.H:856
void swap(PostorderIterator &it)
Definition: tree.H:886
PostorderIterator(const RankedTreap &t)
Definition: tree.H:850
void reset()
Definition: nodesdef.H:965
rng_seed_t & PRIOR(TreapNode *p)
Definition: tree.H:95
Key * insert_dup(const Key &k)
Definition: tree.H:331
void swap(InorderIterator &it)
Definition: tree.H:754
void next()
Definition: tree.H:801
RankedTreap(RankedTreap &&t)
Definition: tree.H:253
TreapRkNode()
Definition: tree.H:47
T pop()
Definition: stack.H:299
rng_t::result_type rng_seed_t
Definition: types.H:53
PreorderIterator(const PreorderIterator &it)
Definition: tree.H:593
typename GT::Node Node
Definition: graphutilities.H:78
Key * search_or_insert(const Key &k)
Definition: tree.H:383
void for_each_preorder(Op &&op=Op())
Definition: tree.H:534
void swap(RankedTreap &t)
Definition: tree.H:283
const Key & get_current() const
Definition: tree.H:793
BinTreeNode *& L(BinTreeNode *p)
Definition: nodesdef.H:987
TreapRkNode(Key &&k)
Definition: tree.H:59
void for_each_postorder(Op &op)
Definition: tree.H:552
long long lint_t
Definition: types.H:49
Key DataType
Definition: tree.H:208
RankedTreap(Cmp &&_cmp=Cmp())
Definition: tree.H:235
Key ValueType
Definition: tree.H:209
Key & get_current()
Definition: tree.H:917
bool is_empty() const
Definition: stack.H:240
Definition: containeralgorithms.H:33
TreapRkNode(const Key &k)
Definition: tree.H:53
nat_t get_position() const
Definition: tree.H:775
void next()
Definition: tree.H:664
Key * insert(Key &&k)
Definition: tree.H:325
T & top()
Definition: stack.H:267
Key * append(const Key &k)
Definition: tree.H:343
Node * get_location() const
Definition: tree.H:712
void reset()
Definition: tree.H:81
const Key * search(const Key &k) const
Definition: tree.H:373
void reset_last()
Definition: tree.H:900
BinTreeNode::KeyType & KEY(BinTreeNode *p)
Definition: nodesdef.H:981
const Key & find(const Key &k) const
Definition: tree.H:405
bool has_current() const
Definition: tree.H:912
BinTreeNodeCtor
Definition: nodesdef.H:897
Key remove_pos(nat_t i)
Definition: tree.H:427
nat_t get_position() const
Definition: tree.H:907
nat_t size() const
Definition: tree.H:299
nat_t SizeType
Definition: tree.H:210
const Key & min() const
Definition: tree.H:438
unsigned long int nat_t
Definition: types.H:50
PreorderIterator(const RankedTreap &t)
Definition: tree.H:587
InorderIterator(InorderIterator &&it)
Definition: tree.H:730
Key * search(const Key &k)
Definition: tree.H:363
bool verify() const
Definition: tree.H:213
Key * insert_dup(Key &&k)
Definition: tree.H:337
RankedTreap(const RankedTreap &t)
Definition: tree.H:247
std::tuple< RankedTreap, RankedTreap > split_key(const Key &k)
Definition: tree.H:465
Key KeyType
Definition: tree.H:207
void clear()
Definition: stack.H:245
Key & get_current()
Definition: tree.H:785
void clear()
Definition: tree.H:304
std::tuple< RankedTreap, RankedTreap > split_key_dup(const Key &k)
Definition: tree.H:475
lint_t position(const Key &k) const
Definition: tree.H:512
Key & get_current()
Definition: tree.H:648
InorderIterator(const InorderIterator &it)
Definition: tree.H:724
nat_t & COUNT(RkNode *p)
Definition: tree.H:89
nat_t & get_count()
Definition: tree.H:71
bool verify_dup() const
Definition: tree.H:218
void reset()
Definition: tree.H:762
void reset_last()
Definition: tree.H:768
InorderIterator(const RankedTreap &t, int)
Definition: tree.H:706
bool has_current() const
Definition: tree.H:780
RankedTreap(rng_seed_t seed, Cmp &_cmp)
Definition: tree.H:223
PreorderIterator(PreorderIterator &&it)
Definition: tree.H:599
T & push(const T &item)
Definition: stack.H:255
Key * insert(const Key &k)
Definition: tree.H:319
void reset()
Definition: tree.H:894
void reset()
Definition: tree.H:631