|
| constexpr Compare & | key_comp () noexcept |
| | The key type.
|
| |
| constexpr Compare & | get_compare () noexcept |
| | See key_comp().
|
| |
| | 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.
|
| |
| | 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.
|
| |
| | 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 & | operator= (const Gen_Avl_Tree &)=delete |
| | Not copyable, for the same reason as the copy constructor.
|
| |
| | 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.
|
| |
| 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.
|
| |
| virtual | ~Gen_Avl_Tree () noexcept=default |
| |
| constexpr Node *& | getRoot () noexcept |
| | Return a modifiable reference to tree's root.
|
| |
| constexpr Node * | getRoot () const noexcept |
| | Return a modifiable reference to tree's root.
|
| |
| Node * | search (const Key &key) const noexcept(nothrow_compare_v< Compare, Key >) |
| | Search a node containing key.
|
| |
| Node * | insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| | Insert the node pointed by p in the tree.
|
| |
| Node * | search_or_insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| | Search or insert a key.
|
| |
| Node * | insert_dup (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| | Insert the node p without testing for key duplicity.
|
| |
| Node * | remove (const Key &key) noexcept(nothrow_compare_v< Compare, Key >) |
| | Remove from an AVL tree the node containing key key.
|
| |
| bool | verify () const |
| | Return true if the tree is a valid AVL tree: the balance of every node and the order of the keys.
|
| |
template<template< typename > class NodeType, typename Key, class Compare>
requires StrictWeakOrder<Compare, Key>
class Aleph::Gen_Avl_Tree< NodeType, Key, Compare >
AVL balanced binary search tree.
An AVL tree is a self-balancing binary search tree where the heights of the two child subtrees of any node differ by at most one. This guarantees O(log n) worst-case time complexity for search, insertion, and deletion operations.
The maximum height of an AVL tree with n nodes is bounded by: \( 1.4404 \lg{(n + 2)} - 0.3277 \)
This implementation uses balance factors stored in each node to track the height difference between subtrees. Rotations are performed after insertions and deletions to maintain the AVL property.
- Template Parameters
-
| NodeType | Template for the node type (typically AvlNode). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
- Complexity:
- Search: O(log n) worst case
- Insert: O(log n) worst case
- Delete: O(log n) worst case
- Space: O(n)
- Example:
if (node != nullptr)
std::cout << "Found: " << node->get_key() << '\n';
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
AVL binary search tree with nodes without a virtual destructor.
- Note
- This is a low-level implementation that manages raw nodes. For automatic memory management, use DynSetAvlTree instead.
- See also
- Avl_Tree Convenient typedef with default node type.
-
DynSetAvlTree High-level wrapper with automatic memory management.
Definition at line 104 of file tpl_avl.H.
template<template< typename > class NodeType, typename Key , class Compare >
Builds an empty tree with a comparison criteria built by default.
It is a constructor apart from the one that takes the comparison, and not a default argument of that one: GCC and Clang do not count a default argument in the noexcept of the implicit default constructor of a tree that inherits these, and what the construction of the comparison threw would end the program there.
- Exceptions
-
| what | the default construction of the key of a header node or of the comparison, or the copy of the comparison, throws; nothing when they are noexcept. |
Definition at line 502 of file tpl_avl.H.
template<template< typename > class NodeType, typename Key , class Compare >
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
The copy that the compiler wrote did worse: the copy used the header node of the tree that it copied, so that the two were one tree, and std::move() of a tree copied it too. Move the tree, which leaves the other one empty, or copy its nodes (copyRec()).
template<template< typename > class NodeType, typename Key , class Compare >
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
It builds no key, if it can: the key of the header node is built by default when that cannot throw; else it is that of the header node of tree, moved, when the move of a key cannot throw (see bin_node_detail::header_from()).
- Parameters
-
| [in,out] | tree | the tree to move from. It keeps its comparison criteria, which are copied: from a const one, or from one that is not const, if they have only that copy. Criteria that cannot be copied are moved, and tree keeps criteria that were moved from. |
- Exceptions
-
| what | the copy of the comparison criteria throws, or their move if they cannot be copied; and what the default construction of the key of the header node throws, with a key whose move may throw too. Then tree keeps its nodes. Nothing when they are noexcept. |
Definition at line 557 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
template<template< typename > class NodeType, typename Key , class Compare >
Return a modifiable reference to tree's root.
Definition at line 586 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_avl().
template<template< typename > class NodeType, typename Key , class Compare >
Insert the node pointed by p in the tree.
‘insert(p)’ inserts the node p into AVL tree. If KEY(p) is found in the tree, then no insertion is done and nullptr is returned. Otherwise, p is inserted and this same value is returned.
- Parameters
-
| [in] | p | the node to be inserted |
- Returns
- a the pointer
p if KEY(p) is not found in the tree, nullptr otherwise.
- Exceptions
-
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was, without p. |
- Note
- It is
noexcept only if the comparison cannot throw, so that what it throws reaches the caller.
Definition at line 640 of file tpl_avl.H.
References Aleph::CmpEqual, Aleph::CmpGreater, Aleph::CmpLess, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, KEY, LLINK, Aleph::InlineStack< T, N >::push(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), RLINK, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl().
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_avl().
template<template< typename > class NodeType, typename Key , class Compare >
Insert the node p without testing for key duplicity.
- Parameters
-
| [in] | p | the node to be inserted |
- Returns
- the pointer
p
- Exceptions
-
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was, without p. |
- Note
- It is
noexcept only if the comparison cannot throw, so that what it throws reaches the caller.
Definition at line 710 of file tpl_avl.H.
References Aleph::CmpEqual, Aleph::CmpLess, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, KEY, LLINK, Aleph::InlineStack< T, N >::push(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), RLINK, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl().
Referenced by TEST(), and TEST().
template<template< typename > class NodeType, typename Key , class Compare >
Remove from an AVL tree the node containing key key.
- Parameters
-
- Returns
- the removed node if the key is found;
nullptr otherwise
- Exceptions
-
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was. |
- Note
- It is
noexcept only if the comparison cannot throw, so that what it throws reaches the caller.
Definition at line 738 of file tpl_avl.H.
References Aleph::CmpEqual, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, LLINK, Aleph::InlineStack< T, N >::push(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion(), RLINK, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::swapWithSuccessor(), and Aleph::InlineStack< T, N >::top().
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
template<template< typename > class NodeType, typename Key , class Compare >
Search a node containing key.
- Parameters
-
- Returns
- a pointer to the node containing
key if it is found; otherwise nullptr
- Exceptions
-
| anything | that the comparison throws; the search does not modify the tree, so it is left as it was. |
- Note
- It is
noexcept only if the comparison cannot throw, so that what it throws reaches the caller.
Definition at line 607 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, KEY, LLINK, RLINK, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_avl().
template<template< typename > class NodeType, typename Key , class Compare >
Search or insert a key.
search_or_insert(p) searches in the tree a node containing KEY(p). If this is found, then a pointer to the node in the tree is returned. Otherwise, p is inserted and its value returned,
This method is very useful when it is required to search and eventually to insert,
- Parameters
-
| [in] | p | the node whose key must be searched and eventually inserted, |
- Returns
- if
p is inserted, the value is returned; otherwise, a pointer to the node containing KEY(p) is returned.
- Exceptions
-
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was, without p. |
- Note
- It is
noexcept only if the comparison cannot throw, so that what it throws reaches the caller.
Definition at line 680 of file tpl_avl.H.
References Aleph::CmpEqual, Aleph::CmpGreater, Aleph::CmpLess, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, KEY, LLINK, Aleph::InlineStack< T, N >::push(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), RLINK, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl().
Referenced by TEST(), TEST(), and TEST().
template<template< typename > class NodeType, typename Key , class Compare >
template<template< typename > class NodeType, typename Key , class Compare >
Return true if the tree is a valid AVL tree: the balance of every node and the order of the keys.
It throws what the comparator throws.
Definition at line 790 of file tpl_avl.H.
References Aleph::and, Aleph::check_bst(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, is_avl(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().