Aleph-w 3.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]
Collaboration 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
 
 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 NodegetRoot () const noexcept
 Return a modifiable reference to tree's root.
 
Nodesearch (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.
 
Nodeinsert (Node *p) noexcept
 Insert the node pointed by p in the tree.
 
Nodesearch_or_insert (Node *p) noexcept
 Search or insert a key.
 
Nodeinsert_dup (Node *p) noexcept
 Insert the node p without testing for key duplicity.
 
Noderemove (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
 
Nodesearch_and_stack_avl (const Key &key, signed char &cmp_result) noexcept
 
Nodesearch_dup_and_stack_avl (const Key &key, signed char &cmp_result) noexcept
 
void restore_avl_after_insertion (Node *p) noexcept
 
NodeswapWithSuccessor (Node *p, Node *&pp) noexcept
 
void restore_avl_after_deletion (bool left_deficit) noexcept
 

Static Private Member Functions

static NoderotateLeft (Node *p) noexcept
 
static NoderotateRight (Node *p) noexcept
 
static NodedoubleRotateLeft (Node *p) noexcept
 
static NodedoubleRotateRight (Node *p) noexcept
 
static Rotation_Type rotation_type (Node *p) noexcept
 
static Noderestore_avl (Node *p, Node *pp) noexcept
 

Private Attributes

FixedStack< Node * > avl_stack
 The type of node.
 
Node head_node
 
Nodehead_ptr
 
Node *& root
 
Compare cmp
 

Detailed Description

template<template< typename > class NodeType, typename Key, class Compare>
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 * search(const Key &key) const noexcept
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwi...
Definition tpl_avl.H:513
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avl.H:529
NodeType< Key > Node
Definition tpl_avl.H:109
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:704
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 106 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 477 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 109 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 314 of file tpl_avl.H.

Constructor & Destructor Documentation

◆ Gen_Avl_Tree()

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

◆ ~Gen_Avl_Tree()

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

Member Function Documentation

◆ avl_stack_empty()

◆ clean_avl_stack()

◆ doubleRotateLeft()

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

◆ doubleRotateRight()

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

◆ 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 509 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 506 of file tpl_avl.H.

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

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

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

◆ insert_dup()

◆ key_comp()

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

◆ remove()

◆ restore_avl()

◆ restore_avl_after_deletion()

◆ restore_avl_after_insertion()

◆ rotateLeft()

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

◆ rotateRight()

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

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

◆ search_and_stack_avl()

◆ search_dup_and_stack_avl()

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

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

Referenced by 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

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.

◆ swapWithSuccessor()

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

◆ verify()

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

Member Data Documentation

◆ avl_stack

◆ cmp

◆ head_node

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

Definition at line 113 of file tpl_avl.H.

◆ head_ptr

◆ root


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