|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
AVL balanced binary search tree. More...
#include <tpl_avl.H>
Classes | |
| struct | Iterator |
| Iterator over the nodes. More... | |
Public Types | |
| using | Node = NodeType< Key > |
| using | key_type = Key |
Public Member Functions | |
| constexpr Compare & | key_comp () noexcept |
| The key type. | |
| constexpr Compare & | get_compare () noexcept |
| Gen_Avl_Tree (Compare __cmp=Compare()) noexcept | |
| void | swap (Gen_Avl_Tree &tree) noexcept |
Swap in constant time all the items of this with the items of tree. | |
| virtual | ~Gen_Avl_Tree () noexcept |
| 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 |
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwise nullptr is returned. | |
| Node * | insert (Node *p) noexcept |
Insert the node pointed by p in the tree. | |
| Node * | search_or_insert (Node *p) noexcept |
| Search or insert a key. | |
| Node * | insert_dup (Node *p) noexcept |
Insert the node p without testing for key duplicity. | |
| Node * | remove (const Key &key) noexcept |
Remove from an AVL tree the node containing key key. | |
| bool | verify () const noexcept |
Private Types | |
| enum | Rotation_Type { ROTATE_LEFT , ROTATE_RIGHT , DOUBLE_ROTATE_LEFT , DOUBLE_ROTATE_RIGHT } |
Private Member Functions | |
| bool | avl_stack_empty () noexcept |
| void | clean_avl_stack () noexcept |
| Node * | search_and_stack_avl (const Key &key, signed char &cmp_result) noexcept |
| Node * | search_dup_and_stack_avl (const Key &key, signed char &cmp_result) noexcept |
| void | restore_avl_after_insertion (Node *p) noexcept |
| Node * | swapWithSuccessor (Node *p, Node *&pp) noexcept |
| void | restore_avl_after_deletion (bool left_deficit) noexcept |
Static Private Member Functions | |
| static Node * | rotateLeft (Node *p) noexcept |
| static Node * | rotateRight (Node *p) noexcept |
| static Node * | doubleRotateLeft (Node *p) noexcept |
| static Node * | doubleRotateRight (Node *p) noexcept |
| static Rotation_Type | rotation_type (Node *p) noexcept |
| static Node * | restore_avl (Node *p, Node *pp) noexcept |
Private Attributes | |
| FixedStack< Node * > | avl_stack |
| The type of node. | |
| Node | head_node |
| Node * | head_ptr |
| Node *& | root |
| Compare | cmp |
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.
| NodeType | Template for the node type (typically AvlNode). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
| using Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::key_type = Key |
| using Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Node = NodeType<Key> |
|
inlinenoexcept |
Definition at line 485 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, and Aleph::FixedStack< T >::push().
|
inlinevirtualnoexcept |
Definition at line 503 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack_empty(), and Aleph::maps().
|
inlineprivatenoexcept |
Definition at line 118 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::~Gen_Avl_Tree(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl().
|
inlineprivatenoexcept |
Definition at line 120 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::FixedStack< T >::popn(), and Aleph::FixedStack< T >::size().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_or_insert().
|
inlinestaticprivatenoexcept |
Definition at line 240 of file tpl_avl.H.
References DIFF, Aleph::LLINK(), Aleph::maps(), and Aleph::RLINK().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().
|
inlinestaticprivatenoexcept |
Definition at line 277 of file tpl_avl.H.
References DIFF, Aleph::LLINK(), Aleph::maps(), and Aleph::RLINK().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().
|
inlineconstexprnoexcept |
Definition at line 483 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::key_comp().
|
inlineconstexprnoexcept |
Return a modifiable reference to tree's root.
Definition at line 509 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
|
inlineconstexprnoexcept |
|
inlinenoexcept |
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.
| [in] | p | the node to be inserted |
p if KEY(p) is not found in the tree, nullptr otherwise. Definition at line 529 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::clean_avl_stack(), Aleph::CmpEqual, Aleph::CmpGreater, Aleph::CmpLess, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), Aleph::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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_avl().
|
inlinenoexcept |
Insert the node p without testing for key duplicity.
Definition at line 589 of file tpl_avl.H.
References Aleph::CmpEqual, Aleph::CmpLess, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), Aleph::RLINK(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl().
|
inlineconstexprnoexcept |
The key type.
Return a reference to the comparison criteria
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 480 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp.
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::get_compare().
|
inlinenoexcept |
Remove from an AVL tree the node containing key key.
Returns the removed node if the key is found; nullptr otherwise.
Definition at line 608 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::clean_avl_stack(), Aleph::CmpEqual, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_ptr, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion(), Aleph::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::FixedStack< T >::top().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinestaticprivatenoexcept |
Definition at line 340 of file tpl_avl.H.
References AH_ERROR, DIFF, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::DOUBLE_ROTATE_LEFT, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::DOUBLE_ROTATE_RIGHT, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::doubleRotateLeft(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::doubleRotateRight(), Aleph::LLINK(), Aleph::maps(), Aleph::RLINK(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::ROTATE_LEFT, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::ROTATE_RIGHT, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::rotateLeft(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::rotateRight(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::rotation_type().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion().
|
inlineprivatenoexcept |
Definition at line 451 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::clean_avl_stack(), DIFF, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::popn(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove().
|
inlineprivatenoexcept |
Definition at line 361 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack_empty(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::clean_avl_stack(), DIFF, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert_dup(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_or_insert().
|
inlinestaticprivatenoexcept |
Definition at line 200 of file tpl_avl.H.
References DIFF, Aleph::LLINK(), Aleph::maps(), and Aleph::RLINK().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().
|
inlinestaticprivatenoexcept |
Definition at line 220 of file tpl_avl.H.
References DIFF, Aleph::LLINK(), Aleph::maps(), and Aleph::RLINK().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().
|
inlinestaticprivatenoexcept |
Definition at line 319 of file tpl_avl.H.
References DIFF, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::DOUBLE_ROTATE_LEFT, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::DOUBLE_ROTATE_RIGHT, Aleph::LLINK(), Aleph::maps(), Aleph::RLINK(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::ROTATE_LEFT, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::ROTATE_RIGHT.
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().
|
inlinenoexcept |
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwise nullptr is returned.
Definition at line 513 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_avl().
|
inlineprivatenoexcept |
Definition at line 122 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack_empty(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, Aleph::CmpEqual, Aleph::CmpGreater, Aleph::CmpLess, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::popn(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, Aleph::FixedStack< T >::size(), and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_or_insert().
|
inlineprivatenoexcept |
Definition at line 168 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack_empty(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, Aleph::CmpEqual, Aleph::CmpGreater, Aleph::CmpLess, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert_dup().
|
inlinenoexcept |
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,
| [in] | p | the node whose key must be searched and eventually inserted, |
p is inserted, the value is returned; otherwise, a pointer to the node containing KEY(p) is returned. Definition at line 566 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::clean_avl_stack(), Aleph::CmpEqual, Aleph::CmpGreater, Aleph::CmpLess, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), Aleph::RLINK(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl().
|
inlinenoexcept |
Swap in constant time all the items of this with the items of tree.
| [in] | tree | the tree to swap with this |
Definition at line 497 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.
|
inlineprivatenoexcept |
Definition at line 402 of file tpl_avl.H.
References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack, DIFF, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove().
|
inlinenoexcept |
|
private |
The type of node.
Definition at line 112 of file tpl_avl.H.
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Gen_Avl_Tree(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack_empty(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::clean_avl_stack(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::swapWithSuccessor().
|
private |
Definition at line 116 of file tpl_avl.H.
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::key_comp(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::swap().
|
private |
|
private |
Definition at line 114 of file tpl_avl.H.
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Gen_Avl_Tree(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::avl_stack_empty(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove().
|
private |
Definition at line 115 of file tpl_avl.H.
Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::getRoot(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::getRoot(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_and_stack_avl(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::swap(), and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::verify().