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

Treap - A randomized binary search tree using heap-ordered priorities. More...

#include <tpl_treap.H>

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

Classes

struct  Iterator
 Iterator on nodes of the tree. More...
 

Public Types

using Node = NodeType< Key >
 

Public Member Functions

void set_seed (const unsigned long seed) noexcept
 Set the random number generator seed.
 
void swap (Gen_Treap &tree) noexcept
 Swap in constant time all the nodes of this with tree
 
Compare & key_comp () noexcept
 return the comparison criteria
 
Compare & get_compare () noexcept
 
 Gen_Treap (const unsigned long seed, const Compare &__cmp=Compare())
 Initialize a treap with random seed and comparison criteria __cmp
 
 Gen_Treap (const Compare &cmp=Compare())
 Initialize a treap with random seed from system time and comparison criteria cmp
 
 ~Gen_Treap ()
 
gsl_rnggsl_rng_object () noexcept
 Get a pointer to gsl random number generator.
 
Node *& getRoot () noexcept
 Return the tree's root.
 
Nodesearch (const Key &key) const noexcept
 Search a key in a treap.
 
Nodeinsert (Node *p) noexcept
 Insert a node in a treap.
 
Nodesearch_or_insert (Node *p) noexcept
 Search or insert a key.
 
Nodeinsert_dup (Node *p) noexcept
 Insert a node in the tree.
 
bool verify () const noexcept
 Return true if this is a consistent treap.
 
Noderemove (const Key &key) noexcept
 Remove a key from the tree.
 
void join (Gen_Treap &t, Gen_Treap &dup) noexcept
 Join this with t filtering the duplicated keys.
 
void join_dup (Gen_Treap &t) noexcept
 Join this with t independently of the presence of duplicated keys.
 
void join_exclusive (Gen_Treap &t) noexcept
 Join exclusive of this with t
 
bool split_key (const Key &key, Gen_Treap &t1, Gen_Treap &t2)
 Split the tree according to a key.
 
void split_key_dup (const Key &key, Gen_Treap &t1, Gen_Treap &t2)
 Split the tree according to a key that could be in the tree.
 

Private Member Functions

void init (const unsigned int seed)
 
Nodeinsert (Node *root, Node *p) noexcept
 
Nodesearch_or_insert (Node *&root, Node *p) noexcept
 
Nodeinsert_dup (Node *root, Node *p) noexcept
 
Noderemove (Node *&root, const Key &key) noexcept
 
void join_dup (Node *&t1, Node *t2) noexcept
 
void join (Node *&t1, Node *t2, Node *&dup) noexcept
 

Static Private Member Functions

static Nodejoin_exclusive (Node *t1, Node *t2) noexcept
 

Private Attributes

Node head
 The type of node.
 
Nodehead_ptr
 
Node *& tree_root
 
gsl_rngr
 
Compare cmp
 

Detailed Description

template<template< typename > class NodeType, typename Key, class Compare>
class Aleph::Gen_Treap< NodeType, Key, Compare >

Treap - A randomized binary search tree using heap-ordered priorities.

A Treap (tree + heap) is a randomized binary search tree that combines BST properties on keys with heap properties on randomly assigned priorities. Each node has a key (BST-ordered) and a random priority (heap-ordered).

Key Properties:
  • Keys follow BST ordering: left < parent < right
  • Priorities follow max-heap ordering: parent priority > child priorities
  • Priorities are randomly assigned at insertion time
  • Expected O(log n) height due to randomization

The dual ordering (BST for keys, heap for priorities) creates a unique tree structure for any set of keys with given priorities, ensuring good balance with high probability.

Comparison with Other Randomized Trees:
  • Treap vs Rand_Tree: Treap is typically faster by a constant factor because priorities are chosen once at insertion (bottom-up balancing), while Rand_Tree makes random decisions at each level (top-down).
  • Treap vs AVL/RB: Treap has simpler code, uses randomization instead of deterministic balancing, but has expected (not worst-case) guarantees.
Template Parameters
NodeTypeNode template (TreapNode or TreapNodeVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
Complexity:
  • Search: O(log n) expected
  • Insert: O(log n) expected
  • Delete: O(log n) expected
  • Split: O(log n) expected
  • Join: O(log n) expected
  • Space: O(n) + O(1) per node for priority
Random Number Generator:
Uses GSL (GNU Scientific Library) Mersenne Twister (mt19937) by default. Seed can be set via set_seed() for reproducible behavior.
Example:
// Set seed for reproducibility (optional)
tree.set_seed(12345);
// Insert nodes
tree.insert(new Treap<int>::Node(42));
tree.insert(new Treap<int>::Node(17));
tree.insert(new Treap<int>::Node(99));
// Search
auto node = tree.search(42);
if (node != nullptr)
std::cout << "Found: " << node->get_key()
<< " (priority: " << PRIO(node) << ")" << '\n';
// Split tree at key 50
Treap<int> left, right;
tree.split_key(50, left, right);
// Join trees
tree.join(left, right);
// Remove
auto removed = tree.remove(42);
delete removed;
void set_seed(const unsigned long seed) noexcept
Set the random number generator seed.
Definition tpl_treap.H:171
Node * remove(const Key &key) noexcept
Remove a key from the tree.
Definition tpl_treap.H:379
Node * search(const Key &key) const noexcept
Search a key in a treap.
Definition tpl_treap.H:220
NodeType< Key > Node
Definition tpl_treap.H:150
void join(Node *&t1, Node *t2, Node *&dup) noexcept
Definition tpl_treap.H:472
bool split_key(const Key &key, Gen_Treap &t1, Gen_Treap &t2)
Split the tree according to a key.
Definition tpl_treap.H:550
Node * insert(Node *root, Node *p) noexcept
Definition tpl_treap.H:227
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
Definition treapNode.H:120
DynList< T > maps(const C &c, Op op)
Classic map operation.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:611
Note
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetTreap.
Priorities are assigned automatically and transparently.
For ranked operations (indexed access), use TreapRk instead.
See also
Treap Convenient typedef for Treap<Key, Compare>.
TreapRk Treap variant with rank support for O(log n) indexed access.
Rand_Tree Alternative randomized tree with top-down balancing.
DynSetTreap High-level wrapper with automatic memory management.

Definition at line 147 of file tpl_treap.H.

Member Typedef Documentation

◆ Node

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

Definition at line 150 of file tpl_treap.H.

Constructor & Destructor Documentation

◆ Gen_Treap() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Treap< NodeType, Key, Compare >::Gen_Treap ( const unsigned long  seed,
const Compare &  __cmp = Compare() 
)
inline

Initialize a treap with random seed and comparison criteria __cmp

Definition at line 189 of file tpl_treap.H.

References Aleph::init.

◆ Gen_Treap() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Treap< NodeType, Key, Compare >::Gen_Treap ( const Compare &  cmp = Compare())
inline

Initialize a treap with random seed from system time and comparison criteria cmp

Definition at line 197 of file tpl_treap.H.

◆ ~Gen_Treap()

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Treap< NodeType, Key, Compare >::~Gen_Treap ( )
inline

Definition at line 202 of file tpl_treap.H.

References Aleph::maps(), and Aleph::Gen_Treap< NodeType, Key, Compare >::r.

Member Function Documentation

◆ get_compare()

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

◆ getRoot()

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

Return the tree's root.

Definition at line 212 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

Referenced by TreapTest::tree_empty(), TreapTest::tree_size(), and write_treap().

◆ gsl_rng_object()

template<template< typename > class NodeType, typename Key , class Compare >
gsl_rng * Aleph::Gen_Treap< NodeType, Key, Compare >::gsl_rng_object ( )
inlinenoexcept

Get a pointer to gsl random number generator.

Definition at line 209 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::r.

◆ init()

◆ insert() [1/2]

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

Insert a node in a treap.

insert(p) inserta el nodo p en el treap this.

Parameters
[in]ppointer to the node to be inserted
Returns
if p->get_key() is not in the tree, then p is inserted and this node pointer is returned. Otherwise, it is returned nullptr.

Definition at line 318 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::maps(), Aleph::PRIO(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ insert() [2/2]

◆ insert_dup() [1/2]

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

Insert a node in the tree.

This method does not fail. It always inserts.

Parameters
[in]ppointer to the node to insert
Returns
the p pointer

Definition at line 360 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::maps(), Aleph::PRIO(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ insert_dup() [2/2]

◆ join() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Treap< NodeType, Key, Compare >::join ( Gen_Treap< NodeType, Key, Compare > &  t,
Gen_Treap< NodeType, Key, Compare > &  dup 
)
inlinenoexcept

Join this with t filtering the duplicated keys.

join(t, dup) produces a treap result of join of this and t. The resulting tree is stored in this.

Nodes containing duplicated keys are inserted in the randomized tree dup. The nodes could belong to any of two trees

Parameters
[in]tramdomized tree to join with this
[out]dupramdomized tree where nodes containing duplicated keys are inserted

Definition at line 505 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::join(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ join() [2/2]

◆ join_dup() [1/2]

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

Join this with t independently of the presence of duplicated keys.

join(t) produces a treap result of join of this and t. The resulting tree is stored in this.

Parameters
[in]tramdomized tree to join with this keys are inserted

Definition at line 519 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ join_dup() [2/2]

◆ join_exclusive() [1/2]

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

Join exclusive of this with t

Exclusive means that all the keys of this are lesser than all the keys of t. This knowlege allows a more effcient way for joining that when the keys ranks are overlapped. However, use very carefully because the algorithm does not perform any check and the result would be incorrect.

Parameters
[in]tramdomized tree to exclusively join with this keys are inserted

Definition at line 536 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ join_exclusive() [2/2]

◆ key_comp()

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

return 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 182 of file tpl_treap.H.

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

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

◆ remove() [1/2]

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

◆ remove() [2/2]

◆ search()

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

Search a key in a treap.

Parameters
[in]keyto be searched
Returns
a pointer to the node containing key if this is found; otherwise, nullptr is returned

Definition at line 220 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::maps(), Aleph::searchInBinTree(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

Referenced by write_treap().

◆ search_or_insert() [1/2]

◆ search_or_insert() [2/2]

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

Search or insert a key.

search_or_insert(p) searches in the tree the key KEY(p). If this key is found, then a pointer to the node containing it is returned. Otherwise, p is inserted.

Parameters
[in]pnode containing a key to be searched and eventually inserted
Returns
if the key contained in p is found, then a pointer to the containing key in the tree is returned. Otherwise, p is inserted and returned

Definition at line 344 of file tpl_treap.H.

References Aleph::maps(), Aleph::PRIO(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ set_seed()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Treap< NodeType, Key, Compare >::set_seed ( const unsigned long  seed)
inlinenoexcept

Set the random number generator seed.

Definition at line 171 of file tpl_treap.H.

References Aleph::maps(), and Aleph::Gen_Treap< NodeType, Key, Compare >::r.

Referenced by TreapTest::SetUp().

◆ split_key()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Treap< NodeType, Key, Compare >::split_key ( const Key &  key,
Gen_Treap< NodeType, Key, Compare > &  t1,
Gen_Treap< NodeType, Key, Compare > &  t2 
)
inline

Split the tree according to a key.

Parameters
[in]keyfor splitting
[out]t1tree with keys lesser than key
[out]t2tree with keys greater than key
Returns
true if tree was split; that is if key is not in the tree. Otherwise, if key is in the tree, false is returned

Definition at line 550 of file tpl_treap.H.

References Aleph::maps(), Aleph::split_key_rec(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ split_key_dup()

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

Split the tree according to a key that could be in the tree.

split_dup(key, t1, t2) splits the tree according to key in two trees t1 which contains the key lesser than key and t2 which contains the keys greater or equal than key.

Parameters
[in]keyfor splitting
[out]t1resulting tree with the keys lesser than key
[out]t2resulting tree with the keys greater or equal than key

Definition at line 565 of file tpl_treap.H.

References Aleph::maps(), Aleph::split_key_dup_rec(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ swap()

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

Swap in constant time all the nodes of this with tree

Definition at line 174 of file tpl_treap.H.

References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::Gen_Treap< NodeType, Key, Compare >::r, and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

◆ verify()

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

Return true if this is a consistent treap.

Definition at line 371 of file tpl_treap.H.

References Aleph::is_treap(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.

Member Data Documentation

◆ cmp

◆ head

template<template< typename > class NodeType, typename Key , class Compare >
Node Aleph::Gen_Treap< NodeType, Key, Compare >::head
private

The type of node.

Definition at line 153 of file tpl_treap.H.

◆ head_ptr

template<template< typename > class NodeType, typename Key , class Compare >
Node* Aleph::Gen_Treap< NodeType, Key, Compare >::head_ptr
private

◆ r

◆ tree_root


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