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

Top-down splay tree with rank support. More...

#include <tpl_splay_treeRk.H>

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

Classes

class  Iterator
 Inorder iterator over the extended splay tree. More...
 

Public Types

typedef NodeType< Key > Node
 
typedef Key key_type
 The key type stored in the node.
 

Public Member Functions

Compare & key_comp ()
 Returns a reference to the comparison functor.
 
Compare & get_compare ()
 See key_comp().
 
void splay (const Key &key) noexcept
 Search key within the tree and splay that node; if key is not found, the last node of the search is splayed.
 
 GenTdSplayTreeRk () noexcept(std::is_nothrow_default_constructible_v< Compare >)
 Builds an empty tree with a comparison criteria built by default.
 
 GenTdSplayTreeRk (Compare __cmp) noexcept(std::is_nothrow_constructible_v< Compare, Compare & >)
 Builds an empty tree with the comparison criteria __cmp.
 
 GenTdSplayTreeRk (const GenTdSplayTreeRk &)=delete
 Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
 
GenTdSplayTreeRk & operator= (const GenTdSplayTreeRk &)=delete
 Not copyable, for the same reason as the copy constructor.
 
 GenTdSplayTreeRk (GenTdSplayTreeRk &&tree) noexcept(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 (GenTdSplayTreeRk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
 Exchange in O(1) the nodes and the comparison criteria of this tree and another.
 
GenTdSplayTreeRk & operator= (GenTdSplayTreeRk &&tree) noexcept(noexcept(swap(tree)))
 Exchanges this tree with another, which is left with the nodes and the comparison criteria that this one had.
 
virtual ~GenTdSplayTreeRk ()
 Destructor.
 
Node * insert (Node *p)
 Inserts a node in a top down splay tree.
 
Node * insert_dup (Node *p)
 Inserts a node in a top down splay tree, even if its key is already in the tree.
 
Node * search (const Key &key)
 Searches a key in a top down splay tree.
 
Node * search_or_insert (Node *p)
 Searches the key of p and, if it is not in the tree, inserts p.
 
Node * remove (const Key &key)
 Remove a key from a top-down splay tree.
 
Node *& getRoot ()
 Get the top-down splay tree's root.
 
bool verify () const
 
size_t size () const
 Returns the number of nodes stored in the tree.
 
bool is_empty () const
 Returns true if the tree is empty.
 
std::pair< long, Node * > position (const Key &key)
 Returns the inorder (sorted) position of key.
 
std::pair< long, Node * > find_position (const Key &key)
 Returns the inorder (sorted) position of key.
 
Node * select (const size_t &i)
 Returns the node whose inorder position in the extended tree is i.
 

Private Member Functions

signed char splay_impl (const Key &key) noexcept
 
Node * __insert (Node *p)
 
Node * splay_max (Node *r) noexcept
 Splay the maximum element to the root.
 

Private Attributes

Node * root
 
Compare cmp
 

Static Private Attributes

static constexpr signed char CmpLess = -1
 
static constexpr signed char CmpEqual = 0
 
static constexpr signed char CmpGreater = 1
 

Detailed Description

template<template< class > class NodeType, typename Key, class Compare>
requires StrictWeakOrder<Compare, Key>
class GenTdSplayTreeRk< NodeType, Key, Compare >

Top-down splay tree with rank support.

A splay tree is a self-adjusting binary search tree that moves recently accessed elements closer to the root. This provides amortized O(log n) time complexity for all operations, with frequently accessed elements having faster access times.

This implementation extends the basic splay tree with COUNT fields in each node, enabling:

  • O(log n) selection of the k-th smallest element
  • O(log n) rank queries (position of an element)
  • All standard BST operations with amortized O(log n) complexity

The splay operation brings the accessed node to the root using a sequence of zig, zig-zig, and zig-zag rotations. This top-down variant performs splaying during the descent, avoiding a second pass.

Template Parameters
NodeTypeTemplate for node type (typically BinNodeXt).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
Complexity (amortized):
  • Search: O(log n)
  • Insert: O(log n)
  • Delete: O(log n)
  • Select k-th: O(log n)
  • Rank query: O(log n)
Example:
// Find 2nd smallest element
auto node = tree.select(1); // 0-indexed
// Get rank of element
size_t pos = tree.position(KEY(node));
@ KEY
Definition btreepic.C:169
std::pair< long, Node * > position(const Key &key)
Returns the inorder (sorted) position of key.
Node * insert(Node *p)
Inserts a node in a top down splay tree.
NodeType< Key > Node
Node * select(const size_t &i)
Returns the node whose inorder position in the extended tree is i.
Note
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetSplayTree.
The comparison must not throw: the splay of the operations that compare is noexcept, and if it throws the program ends (std::terminate()). A splay relinks the tree as it compares, so a comparison that threw in the middle would leave nodes hanging from a local header, out of the tree.
Nor must the default construction of a Key throw: that local header is a whole node, whose key each splay builds by default inside noexcept. In a DynMapTree, whose keys are pairs, that is the default construction of its Data.
See also
Splay_Tree Basic splay tree without rank support.
DynSetSplayTree High-level wrapper.

Definition at line 120 of file tpl_splay_treeRk.H.

Member Typedef Documentation

◆ key_type

template<template< class > class NodeType, typename Key , class Compare >
typedef Key GenTdSplayTreeRk< NodeType, Key, Compare >::key_type

The key type stored in the node.

Definition at line 220 of file tpl_splay_treeRk.H.

◆ Node

template<template< class > class NodeType, typename Key , class Compare >
typedef NodeType<Key> GenTdSplayTreeRk< NodeType, Key, Compare >::Node

Definition at line 123 of file tpl_splay_treeRk.H.

Constructor & Destructor Documentation

◆ GenTdSplayTreeRk() [1/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTreeRk< NodeType, Key, Compare >::GenTdSplayTreeRk ( )
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 comparison throws, and nothing else: this tree has no header node, so that it builds no key.

Definition at line 258 of file tpl_splay_treeRk.H.

◆ GenTdSplayTreeRk() [2/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTreeRk< NodeType, Key, Compare >::GenTdSplayTreeRk ( Compare  __cmp)
inlinenoexcept

Builds an empty tree with the comparison criteria __cmp.

Parameters
[in]__cmpThe comparison criteria, which is copied.
Exceptions
Whatthe copy of __cmp throws, and nothing else: a copy from a Compare that is not const, which a comparison may have apart from the one from a const one.

Definition at line 271 of file tpl_splay_treeRk.H.

◆ GenTdSplayTreeRk() [3/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTreeRk< NodeType, Key, Compare >::GenTdSplayTreeRk ( const GenTdSplayTreeRk< 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 left two trees with the same root, each of which splayed the nodes of the other, and std::move() of a tree copied it too. Move the tree, which leaves the other one empty, or copy its nodes (copyRec()).

◆ GenTdSplayTreeRk() [4/4]

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

Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.

This tree has no header node: it builds no key.

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; then tree keeps its nodes. Nothing when it is noexcept.

Definition at line 305 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::root.

◆ ~GenTdSplayTreeRk()

template<template< class > class NodeType, typename Key , class Compare >
virtual GenTdSplayTreeRk< NodeType, Key, Compare >::~GenTdSplayTreeRk ( )
inlinevirtual

Destructor.

Definition at line 347 of file tpl_splay_treeRk.H.

Member Function Documentation

◆ __insert()

◆ find_position()

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

Returns the inorder (sorted) position of key.

find_position(key) searches in the extended splay tree for key (which takes \(O(\lg n)\) time) and returns the inorder position of the node that contains the key together with the node. If the key is not in the tree, find_position(key) returns the position where it would be if key belonged to the tree.

The return value is a pair<long, Node*>. The first component is the inorder position and the second is the node.

If key is not found in the tree, three cases can be distinguished:

  1. If key is smaller than the minimum key in the tree, then first is -1 and second is the node containing the minimum key.
  2. If key is greater than the maximum key in the tree, then first is COUNT(root) (number of nodes in the tree) and second is the node containing the maximum key.
  3. In any other case, first is the position that key would have in the tree and second (node p) is a key immediately adjacent to key. Note that second can hold a smaller or greater key, but it is guaranteed to be immediately adjacent to key.
Parameters
[in]keykey to search and whose inorder position is requested.
Returns
a pair containing the inorder position of key within the ordered set together with the node if the key is found in the tree; otherwise it returns -1 and the node is unspecified.
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 651 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpLess, Aleph::COUNT(), LLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

◆ get_compare()

template<template< class > class NodeType, typename Key , class Compare >
Compare & GenTdSplayTreeRk< NodeType, Key, Compare >::get_compare ( )
inline

◆ getRoot()

template<template< class > class NodeType, typename Key , class Compare >
Node *& GenTdSplayTreeRk< NodeType, Key, Compare >::getRoot ( )
inline

Get the top-down splay tree's root.

Definition at line 576 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::root.

Referenced by main().

◆ insert()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTreeRk< NodeType, Key, Compare >::insert ( Node *  p)
inline

Inserts a node in a top down splay tree.

Returns
a pointer to the inserted node if node is not found in tree; nullptr otherwise.
Parameters
pa pointer to the node to be inserted
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 385 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), Aleph::and, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), KEY, LLINK, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

Referenced by main().

◆ insert_dup()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTreeRk< NodeType, Key, Compare >::insert_dup ( Node *  p)
inline

Inserts a node in a top down splay tree, even if its key is already in the tree.

Parameters
pa pointer to the node to be inserted
Returns
the pointer p
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 413 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), Aleph::and, KEY, LLINK, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

◆ is_empty()

template<template< class > class NodeType, typename Key , class Compare >
bool GenTdSplayTreeRk< NodeType, Key, Compare >::is_empty ( ) const
inline

Returns true if the tree is empty.

Definition at line 590 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::root.

◆ key_comp()

template<template< class > class NodeType, typename Key , class Compare >
Compare & GenTdSplayTreeRk< NodeType, Key, Compare >::key_comp ( )
inline

Returns a reference to the comparison functor.

Definition at line 223 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp.

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

◆ operator=() [1/2]

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

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

◆ operator=() [2/2]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTreeRk & GenTdSplayTreeRk< NodeType, Key, Compare >::operator= ( GenTdSplayTreeRk< NodeType, Key, Compare > &&  tree)
inlinenoexcept

Exchanges this tree with another, which is left with the nodes and the comparison criteria that this one had.

The tree does not own its nodes: those of this tree are not freed, and go to tree, as in swap(). The assignment that the compiler wrote copied the root, and left the two trees with the same nodes.

Parameters
[in,out]treeThe tree to move from.
Returns
*this.
Exceptions
Whatswapping the comparison criteria throws; then nothing is exchanged.

Definition at line 340 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::swap().

◆ position()

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

Returns the inorder (sorted) position of key.

position(key) searches in the extended splay tree for key (which takes \(O(\lg n)\) time) and returns the inorder position of the node that contains the key.

Parameters
[in]keykey to search and whose inorder position is requested.
Returns
a pair containing the inorder position of key within the ordered set together with the node if the key is found in the tree; otherwise it returns -1 and the node is unspecified.
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 609 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), LLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

Referenced by main().

◆ remove()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTreeRk< NodeType, Key, Compare >::remove ( const Key &  key)
inline

Remove a key from a top-down splay tree.

Searches a key in a top-down splay tree and remove the containing the key if this is found.

Returns
a pointer to node containing the removed key.
Parameters
keyto search
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 539 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), LLINK, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_max().

Referenced by main().

◆ search()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTreeRk< NodeType, Key, Compare >::search ( const Key &  key)
inline

Searches a key in a top down splay tree.

Returns
a pointer to the node containing the key if the key is found; nullptr otherwise.
Parameters
keykey to search
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 436 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

Referenced by main().

◆ search_or_insert()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTreeRk< NodeType, Key, Compare >::search_or_insert ( Node *  p)
inline

Searches the key of p and, if it is not in the tree, inserts p.

Parameters
pa pointer to the node to search or insert
Returns
a pointer to the node of the tree with the key of p, or p if it was inserted
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()), since the splay is noexcept: it relinks the tree as it compares.

Definition at line 456 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, KEY, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

◆ select()

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

Returns the node whose inorder position in the extended tree is i.

select(i) returns the node in the extended splay tree whose inorder position is i.

Parameters
[in]iinorder position to select.
Returns
pointer to the node corresponding to inorder position i.
Exceptions
out_of_rangeif i is greater than or equal to the number of nodes in the tree.

Definition at line 677 of file tpl_splay_treeRk.H.

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

Referenced by main().

◆ size()

template<template< class > class NodeType, typename Key , class Compare >
size_t GenTdSplayTreeRk< NodeType, Key, Compare >::size ( ) const
inline

Returns the number of nodes stored in the tree.

Definition at line 584 of file tpl_splay_treeRk.H.

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

◆ splay()

template<template< class > class NodeType, typename Key , class Compare >
void GenTdSplayTreeRk< NodeType, Key, Compare >::splay ( const Key &  key)
inlinenoexcept

Search key within the tree and splay that node; if key is not found, the last node of the search is splayed.

Parameters
[in]keythe key to search
Warning
The tree must not be empty. All public methods check for empty tree before calling splay().
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()).
Note
It stays noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree.

Definition at line 240 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().

◆ splay_impl()

◆ splay_max()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTreeRk< NodeType, Key, Compare >::splay_max ( Node *  r)
inlineprivatenoexcept

Splay the maximum element to the root.

Uses the same zig-zig and zig rotations as standard splay to maintain the amortized O(log n) guarantee. Unlike splay(key), this function always brings the actual maximum to the root, which is needed for correct behavior when there are duplicate keys.

Parameters
rroot of the subtree
Returns
the new root (the maximum element)

Definition at line 480 of file tpl_splay_treeRk.H.

References Aleph::COUNT(), l, LLINK, r, RLINK, Aleph::rotate_to_left_xt(), sentinelCtor, and y.

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

◆ swap()

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

Exchange in O(1) the nodes and the comparison criteria of this tree and another.

Parameters
[in,out]treethe tree to exchange with.
Exceptions
whatswapping the comparison criteria throws. They go first, so that, if that swap throws, nothing else is exchanged.

Definition at line 319 of file tpl_splay_treeRk.H.

References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::swap().

Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::operator=(), and GenTdSplayTreeRk< NodeType, Key, Compare >::swap().

◆ verify()

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

Member Data Documentation

◆ cmp

◆ CmpEqual

◆ CmpGreater

template<template< class > class NodeType, typename Key , class Compare >
constexpr signed char GenTdSplayTreeRk< NodeType, Key, Compare >::CmpGreater = 1
staticconstexprprivate

◆ CmpLess

template<template< class > class NodeType, typename Key , class Compare >
constexpr signed char GenTdSplayTreeRk< NodeType, Key, Compare >::CmpLess = -1
staticconstexprprivate

◆ root


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