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

Top-down splay tree - Self-adjusting BST with amortized O(log n) operations. More...

#include <tpl_splay_tree.H>

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

Classes

struct  Iterator
 Iterator over the nodes. More...
 

Public Types

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

Public Member Functions

Compare & key_comp () noexcept
 Returns a reference to the comparison criteria.
 
Compare & get_compare () noexcept
 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.
 
 GenTdSplayTree () 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.
 
 GenTdSplayTree (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.
 
 GenTdSplayTree (const GenTdSplayTree &)=delete
 Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
 
GenTdSplayTree & operator= (const GenTdSplayTree &)=delete
 Not copyable, for the same reason as the copy constructor.
 
 GenTdSplayTree (GenTdSplayTree &&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 (GenTdSplayTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swaps in constant time the nodes and the comparison criteria of this and tree.
 
virtual ~GenTdSplayTree ()=default
 Destructor.
 
Node * insert (Node *p) noexcept
 Inserts a node in a top-down splay tree.
 
Node * insert_dup (Node *p) noexcept
 Inserts a node in a top-down splay tree, even if its key is already in the tree.
 
Node * search (const Key &key) noexcept
 Searches a key in a top-down splay tree.
 
Node * search_or_insert (Node *p) noexcept
 Searches the key of p and, if it is not in the tree, inserts p.
 
Node * remove (const Key &key) noexcept
 Remove a key from a top down splay tree.
 
Node *& getRoot () noexcept
 Get the top-down splay tree's root.
 
bool verify () const
 

Private Member Functions

signed char splay_impl (const Key &key) noexcept
 
Node * __insert (Node *p) noexcept
 

Static Private Member Functions

static Node * splay_max (Node *r) noexcept
 Brings the maximum of the subtree of root r to its root, with the zig-zig rotations of a splay.
 

Private Attributes

Node headnode
 
Node * head
 
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 GenTdSplayTree< NodeType, Key, Compare >

Top-down splay tree - Self-adjusting BST with amortized O(log n) operations.

GenTdSplayTree implements a splay tree, a self-adjusting binary search tree where every access operation (search, insert, delete) moves the accessed node to the root through a sequence of tree rotations called "splaying".

Key Characteristics:
  • Self-adjusting: Frequently accessed items migrate toward the root
  • Amortized O(log n): While individual operations may be O(n), sequences of m operations take O(m log n) time total
  • Cache-friendly: Recent accesses stay near the root for fast re-access
  • Simple implementation: No balance information or color bits needed
  • No worst-case guarantee: Individual operations can be O(n)
Splaying Rotations:
  • Zig: Single rotation when node is child of root
  • Zig-zig: Two rotations in same direction (child and parent on same side)
  • Zig-zag: Two rotations in opposite directions (child and parent on different sides)

These rotations ensure amortized efficiency by reducing tree depth.

When to Use Splay Trees:
✓ Access patterns have locality (same items accessed repeatedly) ✓ Amortized complexity is acceptable (no hard real-time constraints) ✓ Simplicity preferred over guaranteed worst-case performance ✓ Cache performance is critical
When NOT to Use:
✗ Need guaranteed O(log n) worst-case (use AVL or Red-Black instead) ✗ Hard real-time requirements ✗ Uniform random access patterns (no locality benefit)
Template Parameters
NodeTypeNode template (BinNode or BinNodeVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
Complexity:
  • Search: O(log n) amortized
  • Insert: O(log n) amortized
  • Delete: O(log n) amortized
  • Individual operation worst case: O(n)
  • Space: O(n) - no extra balance information needed
Example:
// Insert nodes
// Search (splays node to root)
auto node = tree.search(42);
if (node != nullptr)
std::cout << "Found and splayed to root: " << node->get_key() << '\n';
// Subsequent access to 42 is now O(1)
node = tree.search(42); // Very fast - already at root
// Remove (splays then removes root)
auto removed = tree.remove(42);
delete removed;
NodeType< Key > Node
Node * remove(const Key &key) noexcept
Remove a key from a top down splay tree.
Node * insert(Node *p) noexcept
Inserts a node in a top-down splay tree.
Node * search(const Key &key) noexcept
Searches a key in a top-down splay tree.
Note
This implementation does NOT support rank operations. For indexed access, use Splay_Tree_Rk instead.
The comparison must not throw in the operations that splay: they are 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. verify(), which does not splay, lets what the comparison throws reach the caller.
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.
Based on Danny Sleator's top-down splay tree implementation.
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetSplayTree.
See also
Splay_Tree Convenient typedef for Splay_Tree<Key, Compare>.
Splay_Tree_Rk Splay tree variant with rank support.
Avl_Tree Alternative with O(log n) worst-case guarantee.
DynSetSplayTree High-level wrapper with automatic memory management.
Author
Leandro Rabindranath León

Definition at line 146 of file tpl_splay_tree.H.

Member Typedef Documentation

◆ key_type

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

The key type stored in the node.

Definition at line 250 of file tpl_splay_tree.H.

◆ Node

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

Definition at line 149 of file tpl_splay_tree.H.

Constructor & Destructor Documentation

◆ GenTdSplayTree() [1/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTree< NodeType, Key, Compare >::GenTdSplayTree ( )
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 287 of file tpl_splay_tree.H.

◆ GenTdSplayTree() [2/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTree< NodeType, Key, Compare >::GenTdSplayTree ( 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 304 of file tpl_splay_tree.H.

◆ GenTdSplayTree() [3/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTree< NodeType, Key, Compare >::GenTdSplayTree ( const GenTdSplayTree< 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()).

◆ GenTdSplayTree() [4/4]

template<template< class > class NodeType, typename Key , class Compare >
GenTdSplayTree< NodeType, Key, Compare >::GenTdSplayTree ( GenTdSplayTree< 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 342 of file tpl_splay_tree.H.

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

◆ ~GenTdSplayTree()

template<template< class > class NodeType, typename Key , class Compare >
virtual GenTdSplayTree< NodeType, Key, Compare >::~GenTdSplayTree ( )
virtualdefault

Destructor.

Member Function Documentation

◆ __insert()

template<template< class > class NodeType, typename Key , class Compare >
Node * GenTdSplayTree< NodeType, Key, Compare >::__insert ( Node *  p)
inlineprivatenoexcept

◆ get_compare()

template<template< class > class NodeType, typename Key , class Compare >
Compare & GenTdSplayTree< NodeType, Key, Compare >::get_compare ( )
inlinenoexcept

◆ getRoot()

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

Get the top-down splay tree's root.

Definition at line 535 of file tpl_splay_tree.H.

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

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

◆ insert()

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

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()).
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 403 of file tpl_splay_tree.H.

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

Referenced by main(), and write_splay().

◆ insert_dup()

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

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()).
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 433 of file tpl_splay_tree.H.

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

◆ key_comp()

template<template< class > class NodeType, typename Key , class Compare >
Compare & GenTdSplayTree< NodeType, Key, Compare >::key_comp ( )
inlinenoexcept

Returns a reference to the comparison criteria.

Definition at line 253 of file tpl_splay_tree.H.

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

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

◆ operator=()

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

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

◆ remove()

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

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()).
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 506 of file tpl_splay_tree.H.

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

Referenced by main(), and TEST().

◆ search()

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

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()).
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 458 of file tpl_splay_tree.H.

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

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

◆ search_or_insert()

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

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()).
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 480 of file tpl_splay_tree.H.

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

◆ splay()

template<template< class > class NodeType, typename Key , class Compare >
void GenTdSplayTree< 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
Precondition
The tree is not empty: an empty tree has no node to splay.
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 269 of file tpl_splay_tree.H.

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

◆ splay_impl()

◆ splay_max()

template<template< class > class NodeType, typename Key , class Compare >
static Node * GenTdSplayTree< NodeType, Key, Compare >::splay_max ( Node *  r)
inlinestaticprivatenoexcept

Brings the maximum of the subtree of root r to its root, with the zig-zig rotations of a splay.

The new root has no right child.

Definition at line 223 of file tpl_splay_tree.H.

References l, LLINK, r, RLINK, and Aleph::rotate_to_left().

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

◆ swap()

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

Swaps in constant time the nodes and the comparison criteria of this and tree.

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

Definition at line 360 of file tpl_splay_tree.H.

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

◆ verify()

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

Member Data Documentation

◆ cmp

◆ CmpEqual

template<template< class > class NodeType, typename Key , class Compare >
constexpr signed char GenTdSplayTree< NodeType, Key, Compare >::CmpEqual = 0
staticconstexprprivate

◆ CmpGreater

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

◆ CmpLess

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

◆ head

template<template< class > class NodeType, typename Key , class Compare >
Node* GenTdSplayTree< NodeType, Key, Compare >::head
private

Definition at line 153 of file tpl_splay_tree.H.

◆ headnode

template<template< class > class NodeType, typename Key , class Compare >
Node GenTdSplayTree< NodeType, Key, Compare >::headnode
private

Definition at line 152 of file tpl_splay_tree.H.

◆ root


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