Aleph-w 3.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
 
void splay (const Key &key) noexcept
 search key within tree and splay that node, if not found it return Node::NullPtr
 
 GenTdSplayTree (Compare __cmp=Compare()) noexcept
 Constructor.
 
void swap (GenTdSplayTree &tree) noexcept
 
virtual ~GenTdSplayTree ()=default
 Destructor.
 
Nodeinsert (Node *p) noexcept
 Inserts a node in a top-down splay tree.
 
Nodeinsert_dup (Node *p) noexcept
 
Nodesearch (const Key &key) noexcept
 Searches a key in a top-down splay tree.
 
Nodesearch_or_insert (Node *p) noexcept
 
Noderemove (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
 

Private Attributes

Node headnode
 
Nodehead
 
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>
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Note
This implementation does NOT support rank operations. For indexed access, use Splay_Tree_Rk instead.
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 134 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 211 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 137 of file tpl_splay_tree.H.

Constructor & Destructor Documentation

◆ GenTdSplayTree()

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

Constructor.

Definition at line 227 of file tpl_splay_tree.H.

◆ ~GenTdSplayTree()

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

Destructor.

Member Function Documentation

◆ __insert()

◆ 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 368 of file tpl_splay_tree.H.

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

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

Definition at line 269 of file tpl_splay_tree.H.

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

Referenced by write_splay().

◆ insert_dup()

◆ key_comp()

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

Returns 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 214 of file tpl_splay_tree.H.

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

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

◆ 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

Definition at line 341 of file tpl_splay_tree.H.

References GenTdSplayTree< NodeType, Key, Compare >::CmpEqual, Aleph::LLINK(), Aleph::maps(), Aleph::HTList::reset(), Aleph::RLINK(), GenTdSplayTree< NodeType, Key, Compare >::root, and GenTdSplayTree< NodeType, Key, Compare >::splay_impl().

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

Definition at line 309 of file tpl_splay_tree.H.

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

Referenced by TEST(), and write_splay().

◆ search_or_insert()

◆ splay()

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

search key within tree and splay that node, if not found it return Node::NullPtr

Definition at line 221 of file tpl_splay_tree.H.

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

◆ splay_impl()

◆ swap()

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

◆ verify()

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

Member Data Documentation

◆ cmp

◆ CmpEqual

◆ 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 141 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 140 of file tpl_splay_tree.H.

◆ root


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