Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_splay_tree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
46# ifndef TPL_SPLAY_TREE_H
47# define TPL_SPLAY_TREE_H
48
49# include <tpl_binNode.H>
50# include <tpl_binNodeUtils.H>
51# include <ah-concepts.H>
52
53using namespace Aleph;
54
55
144template <template <class> class NodeType, typename Key, class Compare>
145 requires StrictWeakOrder<Compare, Key>
147{
148public:
149 using Node = NodeType<Key>;
150
151private:
154 Compare cmp;
155 static constexpr signed char CmpLess = -1;
156 static constexpr signed char CmpEqual = 0;
157 static constexpr signed char CmpGreater = 1;
158
159 signed char splay_impl(const Key & key) noexcept
160 {
161 Node headNode;
162 Node *l = &headNode;
163 Node *r = &headNode;
164 Node *t = root;
165 signed char cmp_result = CmpEqual;
166
167 while (true)
168 {
169 if (const Key & tk = KEY(t); cmp(key, tk)) // key < tk: go left
170 {
171 cmp_result = CmpLess;
172 if (LLINK(t) == Node::NullPtr) [[unlikely]]
173 break;
174
175 if (cmp(key, KEY(LLINK(t)))) // zig-zig case
176 {
177 t = rotate_to_right(t);
178 if (LLINK(t) == Node::NullPtr) [[unlikely]]
179 break;
180 }
181
182 LLINK(r) = t;
183 r = t;
184 t = LLINK(t);
185 }
186 else if (cmp(tk, key)) // tk < key: go right
187 {
188 cmp_result = CmpGreater;
189 if (RLINK(t) == Node::NullPtr) [[unlikely]]
190 break;
191
192 if (cmp(KEY(RLINK(t)), key)) // zig-zig case
193 {
194 t = rotate_to_left(t);
195 if (RLINK(t) == Node::NullPtr) [[unlikely]]
196 break;
197 }
198
199 RLINK(l) = t;
200 l = t;
201 t = RLINK(t);
202 }
203 else [[unlikely]] // key == tk: found
204 {
205 cmp_result = CmpEqual;
206 break;
207 }
208 }
209
210 /* reassembling of subtrees in one with current as root */
211 RLINK(l) = LLINK(t);
212 LLINK(r) = RLINK(t);
213 LLINK(t) = headNode.getR();
214 RLINK(t) = headNode.getL();
215
216 root = t;
217
218 return cmp_result;
219 }
220
223 static Node * splay_max(Node *r) noexcept
224 {
225 assert(r != Node::NullPtr);
226
227 Node headNode;
228 Node *l = &headNode;
229 Node *t = r;
230
231 while (RLINK(t) != Node::NullPtr)
232 {
233 if (RLINK(RLINK(t)) != Node::NullPtr) // zig-zig case
234 t = rotate_to_left(t);
235
236 RLINK(l) = t;
237 l = t;
238 t = RLINK(t);
239 }
240
241 /* reassembling: what was left behind goes at the left of the maximum */
242 RLINK(l) = LLINK(t);
243 LLINK(t) = headNode.getR();
244
245 return t;
246 }
247
248public:
250 using key_type = Key;
251
253 Compare &key_comp() noexcept { return cmp; }
254
256 Compare &get_compare() noexcept { return key_comp(); }
257
269 void splay(const Key & key) noexcept
270 {
271 (void) splay_impl(key);
272 }
273
288 noexcept(std::is_nothrow_default_constructible_v<Node> and
289 std::is_nothrow_default_constructible_v<Compare> and
290 std::is_nothrow_constructible_v<Compare, Compare &>)
291 : GenTdSplayTree(Compare())
292 {
293 // empty
294 }
295
304 GenTdSplayTree(Compare __cmp)
305 noexcept(std::is_nothrow_default_constructible_v<Node> and
306 std::is_nothrow_constructible_v<Compare, Compare &>)
307 : head(&headnode), root(headnode.getR()), cmp(__cmp)
308 {
309 // Empty
310 }
311
321
324
343 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
344 bin_node_detail::nothrow_compare_taken_v<Compare>)
345 : headnode(bin_node_detail::header_from(tree.headnode)),
346 head(&headnode), root(headnode.getR()),
347 cmp(bin_node_detail::compare_to_take(tree.cmp))
348 {
349 root = tree.root;
350 tree.root = Node::NullPtr;
351 }
352
360 void swap(GenTdSplayTree & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
361 {
362 // The comparison first: if its swap throws, the nodes stay where
363 // they were
364 std::swap(cmp, tree.cmp);
365 std::swap(root, tree.root);
366 }
367
369 virtual ~GenTdSplayTree() = default;
370
371private:
372 Node * __insert(Node *p) noexcept
373 {
374 const Key & pk = KEY(p);
375 const Key & rk = KEY(root);
376 if (cmp(pk, rk))
377 { /* root is predecessor of p */
378 LLINK(p) = LLINK(root);
379 RLINK(p) = root;
380 LLINK(root) = Node::NullPtr;
381 }
382 else
383 { /* root is successor of p */
384 RLINK(p) = RLINK(root);
385 LLINK(p) = root;
386 RLINK(root) = Node::NullPtr;
387 }
388 return root = p; // inserted node become root
389 }
390
391public:
403 Node * insert(Node *p) noexcept
404 {
405 assert(p != Node::NullPtr);
406 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
407
408 /* test insertion in empty tree */
409 if (root == Node::NullPtr)
410 return root = p;
411
412 const Key & key = KEY(p);
413
414 const signed char cmp_result = splay_impl(key);
415
416 if (cmp_result == CmpEqual)
417 return nullptr; // item is already in tree
418
419 return __insert(p);
420 }
421
433 Node * insert_dup(Node *p) noexcept
434 {
435 assert(p != Node::NullPtr);
436 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
437
438 /* test insertion in empty tree */
439 if (root == Node::NullPtr)
440 return root = p;
441
442 splay_impl(KEY(p));
443
444 return __insert(p);
445 }
446
458 Node * search(const Key & key) noexcept
459 {
460 if (root == Node::NullPtr)
461 return nullptr;
462
463 const signed char cmp_result = splay_impl(key);
464
465 return cmp_result == CmpEqual ? root : nullptr;
466 }
467
481 {
482 if (root == Node::NullPtr)
483 return root = p;
484
485 const Key & key = KEY(p);
486 const signed char cmp_result = splay_impl(key);
487 if (cmp_result == CmpEqual)
488 return root;
489
490 return __insert(p);
491 }
492
506 Node * remove(const Key & key) noexcept
507 {
508 if (root == Node::NullPtr)
509 return nullptr;
510
511 if (const signed char cmp_result = splay_impl(key); cmp_result != CmpEqual)
512 return nullptr; /* key not found */
513
514 Node *ret_val = root; /* store node to delete */
515
516 if (LLINK(root) == Node::NullPtr)
517 root = RLINK(root);
518 else
519 {
520 Node *p = RLINK(root);
521
522 // The maximum of the left subtree, and not a search of the key:
523 // with repeated keys that search stops at a node of the same key,
524 // which can have others at its right, and they would be lost
526 RLINK(root) = p;
527 }
528
529 ret_val->reset();
530
531 return ret_val;
532 }
533
535 Node *&getRoot() noexcept
536 {
537 return root;
538 }
539
540 [[nodiscard]] bool verify() const { return check_bst(root, cmp); }
541
549 struct Iterator : public BinNodeInfixIterator<Node>
550 {
552 Iterator() noexcept = default;
554 };
555};
556
557
558template <typename Key, class Compare = Aleph::less<Key>>
559 requires StrictWeakOrder<Compare, Key>
560struct Splay_Tree : public GenTdSplayTree<BinNode, Key, Compare>
561{
563 using Base::Base;
564};
565
566
567template <typename Key, class Compare = Aleph::less<Key>>
568 requires StrictWeakOrder<Compare, Key>
569struct Splay_Tree_Vtl : public GenTdSplayTree<BinNodeVtl, Key, Compare>
570{
572 using Base::Base;
573};
574
575
576#endif /* TPL_SPLAY_TREE_H */
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
@ KEY
Definition btreepic.C:169
Inorder iterator on the nodes of a binary tree.
Top-down splay tree - Self-adjusting BST with amortized O(log n) operations.
GenTdSplayTree(GenTdSplayTree &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
bool verify() const
void swap(GenTdSplayTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
GenTdSplayTree(Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria __cmp.
static constexpr signed char CmpEqual
Key key_type
The key type stored in the node.
Compare & get_compare() noexcept
See key_comp().
NodeType< Key > Node
static constexpr signed char CmpLess
static Node * splay_max(Node *r) noexcept
Brings the maximum of the subtree of root r to its root, with the zig-zig rotations of a splay.
Node *& getRoot() noexcept
Get the top-down splay tree's root.
GenTdSplayTree & operator=(const GenTdSplayTree &)=delete
Not copyable, for the same reason as the copy constructor.
virtual ~GenTdSplayTree()=default
Destructor.
GenTdSplayTree(const GenTdSplayTree &)=delete
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
Node * insert_dup(Node *p) noexcept
Inserts a node in a top-down splay tree, even if its key is already in the tree.
static constexpr signed char CmpGreater
Node * remove(const Key &key) noexcept
Remove a key from a top down splay tree.
Node * insert(Node *p) noexcept
Inserts a node in a top-down splay tree.
Node * search(const Key &key) noexcept
Searches a key in a top-down splay tree.
Node * search_or_insert(Node *p) noexcept
Searches the key of p and, if it is not in the tree, inserts p.
GenTdSplayTree() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with a comparison criteria built by default.
signed char splay_impl(const Key &key) noexcept
Node * __insert(Node *p) noexcept
Compare & key_comp() noexcept
Returns a reference to the comparison criteria.
void splay(const Key &key) noexcept
Search key within the tree and splay that node; if key is not found, the last node of the search is s...
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Iterator over the nodes.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Utility functions for binary tree operations.
Basic binary tree node definitions.
DynList< int > l