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

Simple (unbalanced) binary search tree. More...

#include <tpl_binTree.H>

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

Classes

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

Public Types

using Node = NodeType< Key >
 

Public Member Functions

 GenBinTree (const GenBinTree &)=delete
 
GenBinTree & operator= (const GenBinTree &)=delete
 
void swap (GenBinTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swaps in constant time the nodes and the comparison criteria of this and tree.
 
Compare & key_comp () noexcept
 return the comparison criteria
 
Compare & get_compare () noexcept
 See key_comp().
 
 GenBinTree () 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.
 
 GenBinTree (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.
 
 GenBinTree (GenBinTree &&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.
 
Node * search (const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
 Search a key.
 
virtual ~GenBinTree ()=default
 
bool verify () const
 Return true if the tree is a consistent (correct) binary search tree.
 
Node *& getRoot () noexcept
 Return the root of tree.
 
Node * getRoot () const noexcept
 Return the root of tree.
 
bool verifyBin () const
 See verify().
 
Node * insert (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Insert a node in the tree.
 
Node * insert_dup (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Insert a node in the tree.
 
Node * search_or_insert (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Search or insert a key.
 
bool split (const Key &key, GenBinTree &l, GenBinTree &r) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Split the tree according to a key.
 
void split_dup (const Key &key, GenBinTree &l, GenBinTree &r) noexcept(nothrow_compare_v< Compare, Key >)
 Split the tree according to a key that could be in the tree.
 
Node * remove (const Key &key) noexcept(nothrow_compare_copy_v< Compare, Key >)
 Remove a key from the tree.
 
void join (GenBinTree &tree, GenBinTree &dup) noexcept
 Join tree with this.
 
void join_dup (GenBinTree &t) noexcept
 Join this with t independently of the presence of duplicated keys.
 
void join_exclusive (GenBinTree &t) noexcept
 Join exclusive of this with t
 

Static Private Member Functions

template<class Inserter >
static void move_all (Node *&src, Inserter inserter) noexcept
 

Private Attributes

Node headNode
 The node.
 
Node * head
 
Node *& root
 
Compare cmp
 

Detailed Description

template<template< typename > class NodeType, typename Key, class Compare>
requires StrictWeakOrder<Compare, Key>
class Aleph::GenBinTree< NodeType, Key, Compare >

Simple (unbalanced) binary search tree.

GenBinTree implements a basic binary search tree without any balancing operations. The tree's performance depends entirely on the insertion order of keys.

Performance Characteristics:
  • Random insertion order + few deletions: Operations trend toward O(log n) due to probabilistic balance. This is often the best choice for simplicity when insertion order is naturally random.
  • Many deletions: Performance degrades to approximately O(√n) as the tree becomes less balanced over time.
  • Sequential insertion order: Worst case O(n) - tree degenerates into a linked list. Do not use if insertion order is not random.
When to Use:
✓ Insertion order is random or randomized ✓ Few deletions relative to insertions ✓ Simplicity and minimal overhead are priorities ✓ Testing/benchmarking against balanced trees
When NOT to Use:
✗ Cannot guarantee random insertion order ✗ Frequent deletions ✗ Need guaranteed O(log n) worst-case performance ✗ Production code with adversarial inputs

WARNING**: If you cannot ensure random insertion order, use a balanced tree (Avl_Tree, Rb_Tree, Treap, etc.) instead.

Template Parameters
NodeTypeNode template (BinNode or BinNodeVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
Complexity (assuming random insertion order):
  • Search: O(log n) expected, O(n) worst case
  • Insert: O(log n) expected, O(n) worst case
  • Delete: O(log n) expected, O(n) worst case
  • Space: O(n) - minimal overhead
Example:
// Insert in random order (GOOD)
std::vector<int> keys = {50, 25, 75, 10, 30, 60, 90};
std::shuffle(keys.begin(), keys.end(), rng);
for (int k : keys)
tree.insert(new BinTree<int>::Node(k));
// Search
auto node = tree.search(30);
// Remove
auto removed = tree.remove(25);
delete removed;
// BAD: Sequential insertion (creates linked list)
// for (int i = 0; i < 1000; ++i)
// tree.insert(new BinTree<int>::Node(i)); // O(n²) total time!
WeightedDigraph::Node Node
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
Node * remove(const Key &key) noexcept(nothrow_compare_copy_v< Compare, Key >)
Remove a key from the tree.
Node * search(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Search a key.
static mt19937 rng
Binary search tree with nodes without virtual destructors,.
int keys[]
static int * k
Note
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetBinTree.
No balancing overhead - fastest when insertion order is favorable.
Use as a baseline for benchmarking balanced tree implementations.
See also
BinTree Convenient typedef for BinTree<Key, Compare>.
Avl_Tree Balanced alternative with O(log n) worst-case guarantee.
Treap Randomized alternative with O(log n) expected (simpler than AVL).
DynSetBinTree High-level wrapper with automatic memory management.

Definition at line 137 of file tpl_binTree.H.

Member Typedef Documentation

◆ Node

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

Definition at line 141 of file tpl_binTree.H.

Constructor & Destructor Documentation

◆ GenBinTree() [1/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::GenBinTree< NodeType, Key, Compare >::GenBinTree ( const GenBinTree< NodeType, Key, Compare > &  )
delete

◆ GenBinTree() [2/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::GenBinTree< NodeType, Key, Compare >::GenBinTree ( )
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 211 of file tpl_binTree.H.

◆ GenBinTree() [3/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::GenBinTree< NodeType, Key, Compare >::GenBinTree ( 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 228 of file tpl_binTree.H.

◆ GenBinTree() [4/4]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::GenBinTree< NodeType, Key, Compare >::GenBinTree ( GenBinTree< 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 253 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::root.

◆ ~GenBinTree()

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

Member Function Documentation

◆ get_compare()

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

◆ getRoot() [1/2]

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

Return the root of tree.

Definition at line 289 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::root.

◆ getRoot() [2/2]

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

Return the root of tree.

Definition at line 286 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::root.

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

◆ insert()

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

Insert a node in the tree.

Parameters
[in]ppointer to the node to insert
Returns
if p->get_key() is not in the tree, then the pointer p is returned (it was inserted); otherwise, nullptr is returned
Exceptions
anythingthat the comparison or its copy throws; p is linked only after the last comparison, so the tree is left as it was.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 306 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::BinTree_Operation< Node, Cmp >::insert(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.

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

◆ insert_dup()

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

Insert a node in the tree.

This method does not fail. It always inserts.

  @param[in] p pointer to the node to insert
  @return the `p` pointer
  @throws anything that the comparison or its copy throws; `p` is
  linked only after the last comparison, so the tree is left as it
  was.
  @note It is `noexcept` only if the comparison and its copy cannot
  throw, so that what it throws reaches the caller.

Definition at line 323 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::BinTree_Operation< Node, Cmp >::insert_dup(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.

Referenced by TEST().

◆ join()

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

Join tree with this.

Duplicated keys of tree are put in dup parameter.

  @param[in,out] tree to join with `this`
  @param[out] dup tree where the duplicated key belonging to
  `tree` are put.
  @pre The comparison does not throw; if it does, the program ends
  (`std::terminate()`).
  @note It stays `noexcept` because it moves the nodes one by one:
  a comparison that threw in the middle would leave nodes out of
  every tree.

Definition at line 424 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::move_all().

Referenced by TEST().

◆ join_dup()

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

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

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

Parameters
[in]ttree to join with this keys are inserted
Precondition
The comparison does not throw; if it does, the program ends (std::terminate()).
Note
It stays noexcept because it moves the nodes one by one: a comparison that threw in the middle would leave nodes out of every tree.

Definition at line 448 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::move_all().

Referenced by TEST().

◆ join_exclusive()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::GenBinTree< NodeType, Key, Compare >::join_exclusive ( GenBinTree< 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 knowledge 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]ttree to exclusively join with this keys are inserted

Definition at line 465 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::root.

Referenced by TEST().

◆ key_comp()

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

return the comparison criteria

Definition at line 193 of file tpl_binTree.H.

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

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

◆ move_all()

template<template< typename > class NodeType, typename Key , class Compare >
template<class Inserter >
static void Aleph::GenBinTree< NodeType, Key, Compare >::move_all ( Node *&  src,
Inserter  inserter 
)
inlinestaticprivatenoexcept

◆ operator=()

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

◆ remove()

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

Remove a key from the tree.

Parameters
[in]keyto remove
Returns
a valid pointer to the removed node if key was found in the tree, nullptr otherwise
Exceptions
anythingthat the comparison or its copy throws; the node is unlinked only after the last comparison, so the tree is left as it was.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 407 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::BinTree_Operation< Node, Cmp >::remove(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.

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

◆ search()

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

Search a key.

Parameters
[in]keyto be searched
Returns
a pointer to the containing key if this was found; otherwise nullptr
Exceptions
anythingthat the comparison or its copy throws; the search does not modify the tree, so it is left as it was.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 274 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::GenBinTree< NodeType, Key, Compare >::root, and Aleph::BinTree_Operation< Node, Cmp >::search().

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

◆ search_or_insert()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::GenBinTree< 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
Exceptions
anythingthat the comparison or its copy throws; p is linked only after the last comparison, so the tree is left as it was.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 345 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::GenBinTree< NodeType, Key, Compare >::root, and Aleph::BinTree_Operation< Node, Cmp >::search_or_insert().

Referenced by TEST().

◆ split()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::GenBinTree< NodeType, Key, Compare >::split ( const Key &  key,
GenBinTree< NodeType, Key, Compare > &  l,
GenBinTree< NodeType, Key, Compare > &  r 
)
inlinenoexcept

Split the tree according to a key.

split(key, l, r) splits the tree according to key. That is, if key is not present in the tree, then the tree is split in two trees l which contains the key lesser than key and r which contains the keys greater than key. If key is found in the tree, then the split is not done

Parameters
[in]keyfor splitting
[out]lresulting tree with the keys lesser than key
[out]rresulting tree with the keys greater than key
Returns
true if the tree was split. false otherwise
Exceptions
anythingthat the comparison or its copy throws; every comparison comes before the first change, so the three trees are left as they were.
Note
It is noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller.

Definition at line 369 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, l, r, Aleph::GenBinTree< NodeType, Key, Compare >::root, and Aleph::split_key_rec().

Referenced by TEST(), and TEST().

◆ split_dup()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::GenBinTree< NodeType, Key, Compare >::split_dup ( const Key &  key,
GenBinTree< NodeType, Key, Compare > &  l,
GenBinTree< NodeType, Key, Compare > &  r 
)
inlinenoexcept

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

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

Parameters
[in]keyfor splitting
[out]lresulting tree with the keys lesser than key
[out]rresulting tree with the keys greater or equal than key
Exceptions
anythingthat the comparison throws; every comparison comes before the first change, so the three trees are left as they were.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 390 of file tpl_binTree.H.

References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, l, r, and Aleph::GenBinTree< NodeType, Key, Compare >::root.

Referenced by TEST().

◆ swap()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::GenBinTree< NodeType, Key, Compare >::swap ( GenBinTree< 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 184 of file tpl_binTree.H.

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

Referenced by TEST().

◆ verify()

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

Return true if the tree is a consistent (correct) binary search tree.

Definition at line 283 of file tpl_binTree.H.

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

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

◆ verifyBin()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::GenBinTree< NodeType, Key, Compare >::verifyBin ( ) const
inline

Member Data Documentation

◆ cmp

◆ head

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

Definition at line 146 of file tpl_binTree.H.

◆ headNode

template<template< typename > class NodeType, typename Key , class Compare >
Node Aleph::GenBinTree< NodeType, Key, Compare >::headNode
private

The node.

Definition at line 145 of file tpl_binTree.H.

◆ root


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