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

AVL balanced binary search tree with rank (order statistics). More...

#include <tpl_avlRk.H>

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

Classes

class  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_Rk (Compare cmf_fct=Compare()) noexcept
 
void swap (Gen_Avl_Tree_Rk &tree) noexcept
 Swap in constant time all the items of this with the items of tree.
 
virtual ~Gen_Avl_Tree_Rk () 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.
 
size_t size () const noexcept
 Return the number of nodes in the tree.
 
constexpr bool is_empty () const noexcept
 Return true if tree is empty.
 
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 tree the node containing key.
 
Nodeselect (const size_t i) const
 Return the i-th node in order sense.
 
std::pair< long, Node * > position (const Key &key) const noexcept
 Compute the inorder position of a key.
 
std::pair< long, Node * > find_position (const Key &key) const noexcept
 Find the inorder position of a key in the tree.
 
bool verify () const noexcept
 Return true if the tree is a valid AVL tree with correct counters.
 
void join_exclusive (Gen_Avl_Tree_Rk &t) noexcept
 Join this tree exclusively with another tree.
 
Nodesplit_key (const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
 Split tree by key.
 
void split_key_dup (const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
 Split tree by key including duplicates.
 
void split_pos (const size_t pos, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
 Split tree by inorder position.
 

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
 
void update_counters_after_insertion () noexcept
 
void update_counters_after_deletion () noexcept
 
Nodesplit_key_rec (Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
 
void split_key_dup_rec (Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
 
void split_pos_rec (Node *p, size_t pos, Node *&t1, Node *&t2) 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
 
static size_t avl_height (Node *p) noexcept
 
static Nodeextract_min (Node *&p) noexcept
 
static Nodeextract_max (Node *&p) noexcept
 
static Nodejoin_exclusive_rec (Node *t1, size_t h1, Node *t2, size_t h2) noexcept
 
static Nodejoin_with_pivot (Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
 
static Nodejoin_right (Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
 
static Nodejoin_left (Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) 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_Rk< NodeType, Key, Compare >

AVL balanced binary search tree with rank (order statistics).

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 extended version maintains a count in each node representing the number of nodes in its subtree, enabling O(log n) operations for selecting by position and finding the position of a key.

Template Parameters
NodeTypeTemplate for the node type (typically AvlNodeRk).
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
  • Select: O(log n) worst case
  • Position: O(log n) worst case
  • Space: O(n)
Example:
auto node = tree.select(0); // Get smallest element
auto [pos, found] = tree.position(42); // Get position of key 42
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avlRk.H:568
NodeType< Key > Node
Definition tpl_avlRk.H:113
Node * select(const size_t i) const
Return the i-th node in order sense.
Definition tpl_avlRk.H:703
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
Definition tpl_avlRk.H:714
DynList< T > maps(const C &c, Op op)
Classic map operation.
AVL binary search tree with nodes without virtual destructor and with subtree counters for select/pos...
Definition tpl_avlRk.H:1424
Note
This is a low-level implementation that manages raw nodes. For automatic memory management, use DynSetAvlTreeRk instead.
See also
Avl_Tree_Rk Convenient typedef with default node type.
Avl_Tree_Rk_Vtl Version with virtual destructor nodes.
tpl_avl.H Base AVL tree without rank operations.

Definition at line 110 of file tpl_avlRk.H.

Member Typedef Documentation

◆ key_type

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

Definition at line 515 of file tpl_avlRk.H.

◆ Node

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

Definition at line 113 of file tpl_avlRk.H.

Member Enumeration Documentation

◆ Rotation_Type

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

Definition at line 333 of file tpl_avlRk.H.

Constructor & Destructor Documentation

◆ Gen_Avl_Tree_Rk()

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::Gen_Avl_Tree_Rk ( Compare  cmf_fct = Compare())
inlinenoexcept

◆ ~Gen_Avl_Tree_Rk()

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

Member Function Documentation

◆ avl_height()

◆ avl_stack_empty()

◆ clean_avl_stack()

◆ doubleRotateLeft()

◆ doubleRotateRight()

◆ extract_max()

◆ extract_min()

◆ find_position()

template<template< typename > class NodeType, typename Key , class Compare >
std::pair< long, Node * > Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::find_position ( const Key &  key) const
inlinenoexcept

Find the inorder position of a key in the tree.

Parameters
[in]keyto be searched
Returns
a pair with the position and the node pointer

Definition at line 729 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::find_position(), Aleph::maps(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::find_position().

◆ get_compare()

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

◆ getRoot() [1/2]

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

Return a modifiable reference to tree's root.

Definition at line 547 of file tpl_avlRk.H.

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

◆ getRoot() [2/2]

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

Return a modifiable reference to tree's root.

Definition at line 544 of file tpl_avlRk.H.

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

Referenced by TEST().

◆ insert()

◆ insert_dup()

◆ is_empty()

template<template< typename > class NodeType, typename Key , class Compare >
constexpr bool Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::is_empty ( ) const
inlineconstexprnoexcept

Return true if tree is empty.

Definition at line 553 of file tpl_avlRk.H.

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

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ join_exclusive()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::join_exclusive ( Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t)
inlinenoexcept

Join this tree exclusively with another tree.

All keys in this tree must be less than all keys in t. After the operation, t becomes empty and this tree contains all nodes.

Parameters
[in,out]ttree to join with (will be empty after)
Note
O(m log(n+m)) time complexity where m is size of t

Definition at line 1197 of file tpl_avlRk.H.

References Aleph::COUNT(), DIFF, Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::insert(), Aleph::FixedStack< T >::is_empty(), Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

◆ join_exclusive_rec()

◆ join_left()

◆ join_right()

◆ join_with_pivot()

◆ key_comp()

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Avl_Tree_Rk< 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 518 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::cmp.

Referenced by Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::get_compare().

◆ position()

template<template< typename > class NodeType, typename Key , class Compare >
std::pair< long, Node * > Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::position ( const Key &  key) const
inlinenoexcept

Compute the inorder position of a key.

Parameters
[in]keyto be searched
Returns
a pair with the inorder position and the node pointer. If key is not found, position is -1.

Definition at line 714 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::inorder_position(), Aleph::maps(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST(), TEST(), and TEST().

◆ remove()

◆ restore_avl()

◆ restore_avl_after_deletion()

◆ restore_avl_after_insertion()

◆ rotateLeft()

◆ rotateRight()

◆ rotation_type()

◆ search()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree_Rk< 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 557 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST().

◆ 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_Rk< NodeType, Key, Compare >::search_or_insert ( Node p)
inlinenoexcept

◆ select()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::select ( const size_t  i) const
inline

Return the i-th node in order sense.

Parameters
[in]iinorder position of node to be selected
Returns
a pointer to the i-th node inorder sense
Exceptions
out_of_rangeif i is greater or equal than the number of nodes of tree

Definition at line 703 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root, and Aleph::select().

Referenced by TEST(), TEST(), and TEST().

◆ size()

template<template< typename > class NodeType, typename Key , class Compare >
size_t Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::size ( ) const
inlinenoexcept

Return the number of nodes in the tree.

Definition at line 550 of file tpl_avlRk.H.

References Aleph::COUNT(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::split_pos(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ split_key()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::split_key ( const Key &  key,
Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t1,
Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split tree by key.

After the operation:

  • t1 contains all keys < key
  • t2 contains all keys > key
  • This tree becomes empty
Parameters
[in]keythe splitting key
[out]t1tree to receive keys < key
[out]t2tree to receive keys > key
Returns
pointer to node containing key, or nullptr if not found
Note
O(n) time complexity (simple iterative implementation)

Definition at line 1247 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::COUNT(), DIFF, Aleph::DynList< T >::insert(), Aleph::FixedStack< T >::is_empty(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST(), and TEST().

◆ split_key_dup()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::split_key_dup ( const Key &  key,
Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t1,
Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split tree by key including duplicates.

After the operation:

  • t1 contains all keys <= key
  • t2 contains all keys > key
  • This tree becomes empty
Parameters
[in]keythe splitting key
[out]t1tree to receive keys <= key
[out]t2tree to receive keys > key
Note
O(n) time complexity (simple iterative implementation)

Definition at line 1297 of file tpl_avlRk.H.

References Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::COUNT(), DIFF, Aleph::DynList< T >::insert(), Aleph::FixedStack< T >::is_empty(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST().

◆ split_key_dup_rec()

◆ split_key_rec()

◆ split_pos()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::split_pos ( const size_t  pos,
Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t1,
Gen_Avl_Tree_Rk< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split tree by inorder position.

After the operation:

  • t1 contains nodes at positions [0, pos)
  • t2 contains nodes at positions [pos, size)
  • This tree becomes empty
Parameters
[in]posthe splitting position (0-based)
[out]t1tree to receive positions [0, pos)
[out]t2tree to receive positions [pos, size)
Note
O(n) time complexity (simple iterative implementation)

Definition at line 1341 of file tpl_avlRk.H.

References Aleph::count(), Aleph::COUNT(), DIFF, Aleph::DynList< T >::insert(), Aleph::FixedStack< T >::is_empty(), Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root, and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::size().

Referenced by TEST(), TEST(), and TEST().

◆ split_pos_rec()

◆ swap()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::swap ( Gen_Avl_Tree_Rk< 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 535 of file tpl_avlRk.H.

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

◆ swapWithSuccessor()

◆ update_counters_after_deletion()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::update_counters_after_deletion ( )
inlineprivatenoexcept

◆ update_counters_after_insertion()

◆ verify()

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

Return true if the tree is a valid AVL tree with correct counters.

Definition at line 740 of file tpl_avlRk.H.

References Aleph::check_rank_tree(), Aleph::is_avl_rk(), Aleph::maps(), and Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST(), TEST(), and TEST().

Member Data Documentation

◆ avl_stack

◆ cmp

◆ head_node

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

Definition at line 117 of file tpl_avlRk.H.

◆ head_ptr

◆ root

template<template< typename > class NodeType, typename Key , class Compare >
Node*& Aleph::Gen_Avl_Tree_Rk< NodeType, Key, Compare >::root
private

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