46# ifndef TPL_SPLAY_TREE_H
47# define TPL_SPLAY_TREE_H
144template <
template <
class>
class NodeType,
typename Key,
class Compare>
145 requires StrictWeakOrder<Compare, Key>
169 if (
const Key & tk =
KEY(t);
cmp(key, tk))
172 if (
LLINK(t) == Node::NullPtr) [[unlikely]]
178 if (
LLINK(t) == Node::NullPtr) [[unlikely]]
186 else if (
cmp(tk, key))
189 if (
RLINK(t) == Node::NullPtr) [[unlikely]]
195 if (
RLINK(t) == Node::NullPtr) [[unlikely]]
213 LLINK(t) = headNode.getR();
214 RLINK(t) = headNode.getL();
225 assert(
r != Node::NullPtr);
231 while (
RLINK(t) != Node::NullPtr)
243 LLINK(t) = headNode.getR();
269 void splay(
const Key & key)
noexcept
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 &>)
305 noexcept(std::is_nothrow_default_constructible_v<Node>
and
306 std::is_nothrow_constructible_v<Compare, Compare &>)
343 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
344 bin_node_detail::nothrow_compare_taken_v<Compare>)
347 cmp(bin_node_detail::compare_to_take(tree.
cmp))
350 tree.root = Node::NullPtr;
364 std::swap(
cmp, tree.cmp);
365 std::swap(
root, tree.root);
374 const Key & pk =
KEY(p);
405 assert(p != Node::NullPtr);
406 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
409 if (
root == Node::NullPtr)
412 const Key & key =
KEY(p);
414 const signed char cmp_result =
splay_impl(key);
435 assert(p != Node::NullPtr);
436 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
439 if (
root == Node::NullPtr)
460 if (
root == Node::NullPtr)
463 const signed char cmp_result =
splay_impl(key);
482 if (
root == Node::NullPtr)
485 const Key & key =
KEY(p);
486 const signed char cmp_result =
splay_impl(key);
508 if (
root == Node::NullPtr)
558template <
typename Key,
class Compare = Aleph::less<Key>>
559 requires StrictWeakOrder<Compare, Key>
567template <
typename Key,
class Compare = Aleph::less<Key>>
568 requires StrictWeakOrder<Compare, Key>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
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.
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().
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.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Utility functions for binary tree operations.
Basic binary tree node definitions.