102template <
template <
typename>
class NodeType,
typename Key,
class Compare>
103 requires StrictWeakOrder<Compare, Key>
124 return path.size() == 1;
130 noexcept(nothrow_compare_v<Compare, Key>)
135 Node *candidate =
nullptr;
136 size_t candidate_pos = 0;
140 __builtin_prefetch(
LLINK(p));
141 __builtin_prefetch(
RLINK(p));
151 candidate_pos = path.size();
155 }
while (p != Node::NullPtr);
159 if (candidate !=
nullptr and not
static_cast<bool>(
cmp(
KEY(candidate), key))) [[unlikely]]
163 const size_t to_pop = path.size() > candidate_pos ? path.size() - candidate_pos : 0;
173 noexcept(nothrow_compare_v<Compare, Key>)
181 __builtin_prefetch(
LLINK(p));
182 __builtin_prefetch(
RLINK(p));
194 }
while (p != Node::NullPtr);
201 assert(
DIFF(p) == 2);
202 assert(
RLINK(p) != Node::NullPtr);
221 assert(
DIFF(p) == -2);
222 assert(
LLINK(p) != Node::NullPtr);
241 assert(
DIFF(p) == 2 or
DIFF(p) == -2);
260 else if (
DIFF(
r) == -1)
278 assert(
DIFF(p) == 2 or
DIFF(p) == -2);
297 else if (
DIFF(
r) == -1)
323 assert(
DIFF(p) == 2 or
DIFF(p) == -2);
336 if (
DIFF(pc) == -1 or
DIFF(pc) == 0)
345 assert(
DIFF(p) == -2 or
DIFF(p) == 2);
369 Node *pp = path.pop();
382 Node *gpp = path.pop();
384 if (
LLINK(gpp) == pp)
391 if (
DIFF(gpp) == -2 or
DIFF(gpp) == 2)
393 Node *ggpp = path.pop();
405 Node *&ref_to_stack_top = path.top();
413 while (
LLINK(succ) != Node::NullPtr)
422 ref_to_stack_top = succ;
430 LLINK(p) = Node::NullPtr;
431 if (
RLINK(p) == succ)
453 Node *pp = path.top(1);
454 Node *ppp = path.popn(3);
462 if (
DIFF(pp) == -2 or
DIFF(pp) == 2)
468 left_deficit =
LLINK(ppp) == pp;
478 [[nodiscard]]
constexpr Compare &
key_comp() noexcept
503 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
504 std::is_nothrow_default_constructible_v<Compare>
and
505 std::is_nothrow_constructible_v<Compare, Compare &>)
520 noexcept(std::is_nothrow_default_constructible_v<Node>
and
521 std::is_nothrow_constructible_v<Compare, Compare &>)
558 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
559 bin_node_detail::nothrow_compare_taken_v<Compare>)
562 cmp(bin_node_detail::compare_to_take(tree.
cmp))
565 tree.root = Node::NullPtr;
579 std::swap(
cmp, tree.cmp);
580 std::swap(
root, tree.root);
608 noexcept(nothrow_compare_v<Compare, Key>)
611 while (p != Node::NullPtr)
613 __builtin_prefetch(
LLINK(p));
614 __builtin_prefetch(
RLINK(p));
617 else if (
cmp(
KEY(p), key))
642 if (
root == Node::NullPtr)
682 if (
root == Node::NullPtr)
712 if (
root == Node::NullPtr)
738 [[nodiscard]]
Node *
remove(
const Key &key)
noexcept(nothrow_compare_v<Compare, Key>)
740 if (
root == Node::NullPtr)
754 left_deficit =
LLINK(pp) == p;
755 if (
LLINK(p) == Node::NullPtr)
764 if (
RLINK(p) == Node::NullPtr)
828template <
typename Key,
class Compare = Aleph::less<Key>>
829 requires StrictWeakOrder<Compare, Key>
850template <
typename Key,
class Compare = Aleph::less<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define AH_ERROR(...)
Print an error message (always enabled).
Standard functor implementations and comparison objects.
AVL tree node with balance factor.
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
#define DIFF(p)
Access the balance factor of node p.
Inorder iterator on the nodes of a binary tree.
AVL balanced binary search tree.
Node * search_and_stack_avl(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Node head_node
The type of node.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
static Node * restore_avl(Node *p, Node *pp) noexcept
Gen_Avl_Tree(const Gen_Avl_Tree &)=delete
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
Gen_Avl_Tree(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.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove from an AVL tree the node containing key key.
Node * swapWithSuccessor(Node *p, Node *&pp, Path &path) noexcept
void restore_avl_after_insertion(Node *p, Path &path) noexcept
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
virtual ~Gen_Avl_Tree() noexcept=default
bool verify() const
Return true if the tree is a valid AVL tree: the balance of every node and the order of the keys.
static bool at_head(const Path &path) noexcept
true when path holds only the head node.
Gen_Avl_Tree(Gen_Avl_Tree &&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.
constexpr Compare & get_compare() noexcept
See key_comp().
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node p without testing for key duplicity.
static Node * doubleRotateLeft(Node *p) noexcept
constexpr Compare & key_comp() noexcept
The key type.
void restore_avl_after_deletion(bool left_deficit, Path &path) noexcept
void swap(Gen_Avl_Tree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap in constant time all the items of this with the items of tree.
Gen_Avl_Tree() 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.
static Rotation_Type rotation_type(Node *p) noexcept
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
Gen_Avl_Tree & operator=(const Gen_Avl_Tree &)=delete
Not copyable, for the same reason as the copy constructor.
Node * search_dup_and_stack_avl(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
constexpr Node * getRoot() const noexcept
Return a modifiable reference to tree's root.
static Node * doubleRotateRight(Node *p) noexcept
static Node * rotateRight(Node *p) noexcept
static Node * rotateLeft(Node *p) noexcept
Stack of fixed capacity whose storage is inside the object.
T & push(const T &data) noexcept
Push a copy of data.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
Strict weak ordering constraint for BST comparators.
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.
@ CmpGreater
First argument is greater than second.
@ CmpLess
First argument is less than second.
@ CmpEqual
Arguments are equal.
AVL binary search tree with nodes with a virtual destructor.
AVL binary search tree with nodes without a virtual destructor.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Iterator(const Gen_Avl_Tree &tree)
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.