Program Listing for File AVLTree.h
↰ Return to documentation for file (ADS/tree/AVLTree.h)
/*--------------------------------------------------------------------------*/
/*----------------------------- File AVLTree.h -----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/
#ifndef __ADS_AVLTree
#define __ADS_AVLTree
/* self-identification: #endif constant */
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <memory>
#include <optional>
#include <utility>
/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace ADS {
/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS AVLTree -------------------------------*/
/*--------------------------------------------------------------------------*/
template< std::totally_ordered K , class V >
class AVLTree {
/*--------------------------------------------------------------------------*/
public:
using Key = K;
using Value = V;
[[nodiscard]] std::optional< V > lookup( const K & k ) const;
void insert( const K & k , const V & v );
void remove( const K & k );
[[nodiscard]] bool contains( const K & k ) const;
[[nodiscard]] std::size_t size( void ) const;
[[nodiscard]] bool empty( void ) const;
[[nodiscard]] int height( void ) const;
template< class F >
void forEach( F && f ) const;
/*--------------------------------------------------------------------------*/
private:
struct Node {
K key;
V value;
int h = 0;
std::unique_ptr< Node > left , right;
};
public:
/*------------------------- THE STANDARD INTERFACE -------------------------*/
// a constant view of the pairs ( key , value ) by increasing key,
// bidirectional, along the path from the root (the nodes have no parent)
struct Shape {
static auto root( const auto & o ) { return( o.f_root.get() ); }
static auto left( const auto * n ) { return( n->left.get() ); }
static auto right( const auto * n ) { return( n->right.get() ); }
static auto read( const auto * n ) {
return( std::pair< const K & , const V & >( n->key , n->value ) );
}
};
using value_type = std::pair< const K & , const V & >;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type;
using const_reference = value_type;
using const_iterator = PathIterator< AVLTree , Shape >;
using iterator = const_iterator;
[[nodiscard]] const_iterator begin( void ) const {
return( const_iterator( this , true ) );
}
[[nodiscard]] const_iterator end( void ) const {
return( const_iterator( this , false ) );
}
[[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
[[nodiscard]] const_iterator cend( void ) const { return( end() ); }
private:
using Ptr = std::unique_ptr< Node >;
static int h( const Ptr & t ) { return( t ? t->h : -1 ); }
static void update( Ptr & t );
static void rotateRight( Ptr & t );
static void rotateLeft( Ptr & t );
static void balance( Ptr & t );
bool insert( Ptr & t , const K & k , const V & v );
bool remove( Ptr & t , const K & k );
template< class F >
static void visit( const Ptr & t , F & f );
static bool valid( const Ptr & t , const K * lo , const K * hi );
Ptr f_root;
std::size_t f_size = 0;
}; // end( class AVLTree )
/*--------------------------------------------------------------------------*/
} // end( namespace ADS )
/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADS/tree/impl/AVLTree.tpp"
/*--------------------------------------------------------------------------*/
#endif /* AVLTree.h included */
/*--------------------------------------------------------------------------*/
/*--------------------------- End File AVLTree.h ---------------------------*/
/*--------------------------------------------------------------------------*/