Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Avl_Tree< NodeType, Key, Compare > Class Template Reference

AVL balanced binary search tree. More...

#include <tpl_avl.H>

Inheritance diagram for Aleph::Gen_Avl_Tree< NodeType, Key, Compare >:
[legend]

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
 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.
 

Private Types

enum  Rotation_Type { ROTATE_LEFT , ROTATE_RIGHT , DOUBLE_ROTATE_LEFT , DOUBLE_ROTATE_RIGHT }
 
using Path = InlineStack< Node *, Node::MaxHeight+1 >
 

Private Member Functions

Node * search_and_stack_avl (const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
 
Node * search_dup_and_stack_avl (const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
 
void restore_avl_after_insertion (Node *p, Path &path) noexcept
 
Node * swapWithSuccessor (Node *p, Node *&pp, Path &path) noexcept
 
void restore_avl_after_deletion (bool left_deficit, Path &path) noexcept
 

Static Private Member Functions

static bool at_head (const Path &path) noexcept
 true when path holds only the head node.
 
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

Node head_node
 The type of node.
 
Node * head_ptr
 
Node *& root
 
Compare cmp
 

Detailed Description

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
NodeTypeTemplate for the node type (typically AvlNode).
KeyThe type of keys stored in the tree.
CompareComparison 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:
auto node = tree.search(42);
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.
Definition tpl_avl.H:640
NodeType< Key > Node
Definition tpl_avl.H:107
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
Definition tpl_avl.H:607
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
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.

Member Typedef Documentation

◆ key_type

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::key_type = Key

Definition at line 475 of file tpl_avl.H.

◆ Node

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Node = NodeType<Key>

Definition at line 107 of file tpl_avl.H.

◆ Path

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Path = InlineStack<Node *, Node::MaxHeight + 1>
private

Definition at line 119 of file tpl_avl.H.

Member Enumeration Documentation

◆ Rotation_Type

template<template< typename > class NodeType, typename Key , class Compare >
enum Aleph::Gen_Avl_Tree::Rotation_Type
private
Enumerator
ROTATE_LEFT 
ROTATE_RIGHT 
DOUBLE_ROTATE_LEFT 
DOUBLE_ROTATE_RIGHT 

Definition at line 313 of file tpl_avl.H.

Constructor & Destructor Documentation

◆ Gen_Avl_Tree() [1/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Gen_Avl_Tree ( )
inlinenoexcept

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
whatthe 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.

◆ Gen_Avl_Tree() [2/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Gen_Avl_Tree ( Compare  _cmp)
inlinenoexcept

Builds an empty tree with the comparison criteria _cmp.

Parameters
[in]_cmpthe comparison criteria, which the tree copies.
Exceptions
whatthe default construction of the key of a header node or the copy of _cmp throws; nothing when both are noexcept. The copy is from a Compare that is not const, which a comparison may have apart from the one from a const one.

Definition at line 519 of file tpl_avl.H.

◆ Gen_Avl_Tree() [3/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Gen_Avl_Tree ( const Gen_Avl_Tree< NodeType, Key, Compare > &  )
delete

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()).

◆ Gen_Avl_Tree() [4/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::Gen_Avl_Tree ( Gen_Avl_Tree< NodeType, Key, Compare > &&  tree)
inlinenoexcept

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]treethe 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
whatthe 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.

◆ ~Gen_Avl_Tree()

template<template< typename > class NodeType, typename Key , class Compare >
virtual Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::~Gen_Avl_Tree ( )
virtualdefaultnoexcept

Member Function Documentation

◆ at_head()

template<template< typename > class NodeType, typename Key , class Compare >
static bool Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::at_head ( const Path &  path)
inlinestaticprivatenoexcept

◆ doubleRotateLeft()

template<template< typename > class NodeType, typename Key , class Compare >
static Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::doubleRotateLeft ( Node *  p)
inlinestaticprivatenoexcept

Definition at line 239 of file tpl_avl.H.

References Aleph::and, DIFF, LLINK, r, and RLINK.

Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().

◆ doubleRotateRight()

template<template< typename > class NodeType, typename Key , class Compare >
static Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::doubleRotateRight ( Node *  p)
inlinestaticprivatenoexcept

Definition at line 276 of file tpl_avl.H.

References Aleph::and, DIFF, LLINK, r, and RLINK.

Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().

◆ get_compare()

template<template< typename > class NodeType, typename Key , class Compare >
constexpr Compare & Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::get_compare ( )
inlineconstexprnoexcept

◆ getRoot() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
constexpr Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::getRoot ( ) const
inlineconstexprnoexcept

Return a modifiable reference to tree's root.

Definition at line 592 of file tpl_avl.H.

References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.

◆ getRoot() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
constexpr Node *& Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::getRoot ( )
inlineconstexprnoexcept

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().

◆ insert()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert ( Node *  p)
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.

Parameters
[in]pthe node to be inserted
Returns
a the pointer p if KEY(p) is not found in the tree, nullptr otherwise.
Exceptions
anythingthat 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().

◆ insert_dup()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::insert_dup ( Node *  p)
inlinenoexcept

Insert the node p without testing for key duplicity.

Parameters
[in]pthe node to be inserted
Returns
the pointer p
Exceptions
anythingthat 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().

◆ key_comp()

template<template< typename > class NodeType, typename Key , class Compare >
constexpr Compare & Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::key_comp ( )
inlineconstexprnoexcept

The key type.

Return a reference to the comparison criteria

Definition at line 478 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().

◆ operator=()

template<template< typename > class NodeType, typename Key , class Compare >
Gen_Avl_Tree & Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::operator= ( const Gen_Avl_Tree< NodeType, Key, Compare > &  )
delete

Not copyable, for the same reason as the copy constructor.

◆ remove()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove ( const Key &  key)
inlinenoexcept

Remove from an AVL tree the node containing key key.

Parameters
[in]keythe key to remove
Returns
the removed node if the key is found; nullptr otherwise
Exceptions
anythingthat 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().

◆ restore_avl()

◆ restore_avl_after_deletion()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_deletion ( bool  left_deficit,
Path &  path 
)
inlineprivatenoexcept

◆ restore_avl_after_insertion()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl_after_insertion ( Node *  p,
Path &  path 
)
inlineprivatenoexcept

◆ rotateLeft()

template<template< typename > class NodeType, typename Key , class Compare >
static Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::rotateLeft ( Node *  p)
inlinestaticprivatenoexcept

Definition at line 199 of file tpl_avl.H.

References DIFF, LLINK, and RLINK.

Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().

◆ rotateRight()

template<template< typename > class NodeType, typename Key , class Compare >
static Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::rotateRight ( Node *  p)
inlinestaticprivatenoexcept

Definition at line 219 of file tpl_avl.H.

References DIFF, LLINK, and RLINK.

Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::restore_avl().

◆ rotation_type()

◆ search()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search ( const Key &  key) const
inlinenoexcept

Search a node containing key.

Parameters
[in]keythe key to search
Returns
a pointer to the node containing key if it is found; otherwise nullptr
Exceptions
anythingthat 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().

◆ search_and_stack_avl()

◆ search_dup_and_stack_avl()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_dup_and_stack_avl ( const Key &  key,
signed char &  cmp_result,
Path &  path 
)
inlineprivatenoexcept

◆ search_or_insert()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::search_or_insert ( Node *  p)
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,

Parameters
[in]pthe 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
anythingthat 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().

◆ swap()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::swap ( Gen_Avl_Tree< NodeType, Key, Compare > &  tree)
inlinenoexcept

Swap in constant time all the items of this with the items of tree.

Parameters
[in]treethe tree to swap with this
Exceptions
whatthe swap of the comparison criteria throws, which goes first: then the nodes stay where they were.

Definition at line 575 of file tpl_avl.H.

References Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::cmp, and Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::root.

◆ swapWithSuccessor()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::swapWithSuccessor ( Node *  p,
Node *&  pp,
Path &  path 
)
inlineprivatenoexcept

Definition at line 402 of file tpl_avl.H.

References DIFF, LLINK, and RLINK.

Referenced by Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::remove().

◆ verify()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::verify ( ) const
inline

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().

Member Data Documentation

◆ cmp

◆ head_node

template<template< typename > class NodeType, typename Key , class Compare >
Node Aleph::Gen_Avl_Tree< NodeType, Key, Compare >::head_node
private

The type of node.

Definition at line 110 of file tpl_avl.H.

◆ head_ptr

◆ root


The documentation for this class was generated from the following file: