Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Cartesian_Tree< T, Comp > Class Template Reference

Explicit Cartesian Tree built in O(n) with a monotonic stack. More...

#include <tpl_cartesian_tree.H>

Inheritance diagram for Aleph::Gen_Cartesian_Tree< T, Comp >:
[legend]
Collaboration diagram for Aleph::Gen_Cartesian_Tree< T, Comp >:
[legend]

Public Types

using Item_Type = T
 The type of the element stored in the tree.
 

Public Member Functions

 Gen_Cartesian_Tree (const Array< T > &values, Comp c=Comp())
 Construct from an Array<T>.
 
 Gen_Cartesian_Tree (const std::vector< T > &values, Comp c=Comp())
 Construct from a std::vector<T>.
 
 Gen_Cartesian_Tree (std::initializer_list< T > il, Comp c=Comp())
 Construct from an initializer_list<T>.
 
 Gen_Cartesian_Tree (const DynList< T > &values, Comp c=Comp())
 Construct from a DynList<T>.
 
 Gen_Cartesian_Tree (const size_t num, const T &init, Comp c=Comp())
 Construct with num elements all equal to init.
 
 Gen_Cartesian_Tree (const Gen_Cartesian_Tree &)=default
 Copy constructor.
 
Gen_Cartesian_Tree & operator= (const Gen_Cartesian_Tree &)=default
 Copy assignment operator.
 
 Gen_Cartesian_Tree (Gen_Cartesian_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< Comp >)
 Take the nodes of another tree, which is left empty.
 
Gen_Cartesian_Tree & operator= (Gen_Cartesian_Tree &&other) noexcept(noexcept(swap(other)))
 Exchange this tree with another, which is left with the previous nodes of this one.
 
constexpr size_t root () const noexcept
 Index of the root node.
 
size_t left_child (const size_t i) const
 Left child of node i.
 
size_t right_child (const size_t i) const
 Right child of node i.
 
size_t parent_of (const size_t i) const
 Parent of node i.
 
const T & data_at (const size_t i) const
 Value at node i.
 
constexpr size_t size () const noexcept
 Number of nodes.
 
constexpr bool is_empty () const noexcept
 True if the tree is empty.
 
bool is_leaf (const size_t i) const
 True if node i is a leaf (no children).
 
bool is_root (const size_t i) const
 True if node i is the root.
 
size_t height () const
 Height of the tree (longest root-to-leaf path).
 
Array< size_t > inorder () const
 Inorder traversal of the tree.
 
Array< T > values () const
 Copy of the original data array.
 
void swap (Gen_Cartesian_Tree &other) noexcept(noexcept(data_.swap(other.data_)) &&noexcept(parent_.swap(other.parent_)) &&noexcept(left_.swap(other.left_)) &&noexcept(right_.swap(other.right_)) &&noexcept(std::swap(root_, other.root_)) &&noexcept(std::swap(n_, other.n_)) &&std::is_nothrow_swappable_v< Comp >)
 Swap with other in O(1).
 

Static Public Attributes

static constexpr size_t NONE = static_cast<size_t>(-1)
 Sentinel value for absent parent/child links.
 

Private Member Functions

void build ()
 Build the Cartesian Tree using a monotonic stack in O(n).
 
void init_and_build ()
 Initialize arrays of size n with NONE and build.
 

Private Attributes

Array< T > data_
 
Array< size_t > parent_
 
Array< size_t > left_
 
Array< size_t > right_
 
size_t root_
 
size_t n_
 
Comp comp_
 

Detailed Description

template<typename T, class Comp>
requires StrictWeakOrder<Comp, T>
class Aleph::Gen_Cartesian_Tree< T, Comp >

Explicit Cartesian Tree built in O(n) with a monotonic stack.

A Cartesian Tree over an array a[0..n-1] is a binary tree where:

  • The heap property holds: no child is strictly better than its parent under Comp, i.e. !Comp(a[child], a[parent]) for every parent–child pair (min-heap by default; equality is allowed).
  • The inorder traversal reproduces the original array order 0, 1, 2, ..., n-1.

The tree is represented implicitly with three parallel arrays (parent, left, right) indexed by position in the original array. A sentinel value NONE = size_t(-1) marks absent links.

Template Parameters
Telement type.
Compbinary comparator defining the heap property.
Example
Cartesian_Tree<int> ct = {3, 2, 6, 1, 9};
// Root is at index 3 (value 1, the minimum)
assert(ct.root() == 3);
assert(ct.data_at(ct.root()) == 1);
// Inorder traversal = {0, 1, 2, 3, 4}
auto io = ct.inorder();
for (size_t i = 0; i < io.size(); ++i)
assert(io(i) == i);
Array< size_t > inorder() const
Inorder traversal of the tree.
constexpr size_t root() const noexcept
Index of the root node.
const T & data_at(const size_t i) const
Value at node i.
Cartesian Tree for min-heap (range minimum).

Definition at line 119 of file tpl_cartesian_tree.H.

Member Typedef Documentation

◆ Item_Type

template<typename T , class Comp >
using Aleph::Gen_Cartesian_Tree< T, Comp >::Item_Type = T

The type of the element stored in the tree.

Definition at line 196 of file tpl_cartesian_tree.H.

Constructor & Destructor Documentation

◆ Gen_Cartesian_Tree() [1/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( const Array< T > &  values,
Comp  c = Comp() 
)
inline

Construct from an Array<T>.

Parameters
valuessource array.
ccomparator (default-constructed).

Definition at line 202 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build().

◆ Gen_Cartesian_Tree() [2/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( const std::vector< T > &  values,
Comp  c = Comp() 
)
inline

◆ Gen_Cartesian_Tree() [3/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( std::initializer_list< T >  il,
Comp  c = Comp() 
)
inline

Construct from an initializer_list<T>.

Parameters
ilinitializer list.
ccomparator.

Definition at line 225 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::data_, and Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build().

◆ Gen_Cartesian_Tree() [4/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( const DynList< T > &  values,
Comp  c = Comp() 
)
inline

Construct from a DynList<T>.

Parameters
valuessource list.
ccomparator.

Definition at line 239 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::data_, Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), and Aleph::Gen_Cartesian_Tree< T, Comp >::values().

◆ Gen_Cartesian_Tree() [5/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( const size_t  num,
const T &  init,
Comp  c = Comp() 
)
inline

Construct with num elements all equal to init.

Parameters
numnumber of elements.
initinitial value for every position.
ccomparator.

Definition at line 254 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::data_, Aleph::init, Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), and Aleph::Gen_Cartesian_Tree< T, Comp >::n_.

◆ Gen_Cartesian_Tree() [6/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( const Gen_Cartesian_Tree< T, Comp > &  )
default

Copy constructor.

◆ Gen_Cartesian_Tree() [7/7]

template<typename T , class Comp >
Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree ( Gen_Cartesian_Tree< T, Comp > &&  other)
inlinenoexcept

Take the nodes of another tree, which is left empty.

The implicit move took the arrays but copied the root and the size: the moved-from tree then read nodes that it no longer had.

Parameters
[in,out]othertree to move from.

Definition at line 275 of file tpl_cartesian_tree.H.

Member Function Documentation

◆ build()

◆ data_at()

template<typename T , class Comp >
const T & Aleph::Gen_Cartesian_Tree< T, Comp >::data_at ( const size_t  i) const
inline

◆ height()

template<typename T , class Comp >
size_t Aleph::Gen_Cartesian_Tree< T, Comp >::height ( ) const
inline

Height of the tree (longest root-to-leaf path).

Note
This function takes no parameters.
Returns
Height in number of nodes on the longest root-to-leaf path (0 if empty, 1 for a single-node tree), returned by value.
Exceptions
std::bad_allocif temporary DFS stack allocation fails.
Note
No bounds-related exceptions are thrown (this method receives no indices).
Complexity: O(n) time and O(n) auxiliary space (explicit stack).

Definition at line 374 of file tpl_cartesian_tree.H.

References Aleph::Array< T >::create(), Aleph::Gen_Cartesian_Tree< T, Comp >::left_, Aleph::Gen_Cartesian_Tree< T, Comp >::n_, Aleph::Gen_Cartesian_Tree< T, Comp >::NONE, Aleph::Gen_Cartesian_Tree< T, Comp >::right_, and Aleph::Gen_Cartesian_Tree< T, Comp >::root_.

Referenced by scenario_1(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().

◆ init_and_build()

◆ inorder()

template<typename T , class Comp >
Array< size_t > Aleph::Gen_Cartesian_Tree< T, Comp >::inorder ( ) const
inline

Inorder traversal of the tree.

Note
This function takes no parameters.
Returns
A new Array<size_t> with node indices in inorder, returned by value (ownership transferred to the caller). For a correctly built Cartesian Tree, this sequence is {0, 1, ..., n-1}. If the tree is empty, returns an empty Array<size_t>.
Exceptions
std::bad_allocif result/stack allocation fails.
Note
No bounds-related exceptions are thrown (this method receives no indices).
Complexity: O(n) time and O(n) auxiliary space (result array + traversal stack).

Definition at line 418 of file tpl_cartesian_tree.H.

References Aleph::Array< T >::create(), Aleph::Gen_Cartesian_Tree< T, Comp >::left_, Aleph::Gen_Cartesian_Tree< T, Comp >::n_, Aleph::Gen_Cartesian_Tree< T, Comp >::NONE, Aleph::Gen_Cartesian_Tree< T, Comp >::right_, and Aleph::Gen_Cartesian_Tree< T, Comp >::root_.

Referenced by scenario_1(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().

◆ is_empty()

template<typename T , class Comp >
constexpr bool Aleph::Gen_Cartesian_Tree< T, Comp >::is_empty ( ) const
inlineconstexprnoexcept

True if the tree is empty.

Definition at line 344 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::n_.

Referenced by Moved_From_Case< Cartesian_Tree< int > >::is_empty(), TEST(), and TEST().

◆ is_leaf()

template<typename T , class Comp >
bool Aleph::Gen_Cartesian_Tree< T, Comp >::is_leaf ( const size_t  i) const
inline

◆ is_root()

template<typename T , class Comp >
bool Aleph::Gen_Cartesian_Tree< T, Comp >::is_root ( const size_t  i) const
inline

True if node i is the root.

Definition at line 357 of file tpl_cartesian_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Cartesian_Tree< T, Comp >::n_, and Aleph::Gen_Cartesian_Tree< T, Comp >::root_.

Referenced by TEST(), and TEST().

◆ left_child()

template<typename T , class Comp >
size_t Aleph::Gen_Cartesian_Tree< T, Comp >::left_child ( const size_t  i) const
inline

◆ operator=() [1/2]

template<typename T , class Comp >
Gen_Cartesian_Tree & Aleph::Gen_Cartesian_Tree< T, Comp >::operator= ( const Gen_Cartesian_Tree< T, Comp > &  )
default

Copy assignment operator.

◆ operator=() [2/2]

template<typename T , class Comp >
Gen_Cartesian_Tree & Aleph::Gen_Cartesian_Tree< T, Comp >::operator= ( Gen_Cartesian_Tree< T, Comp > &&  other)
inlinenoexcept

Exchange this tree with another, which is left with the previous nodes of this one.

Parameters
[in,out]othertree to move from.
Returns
this tree.

Definition at line 289 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::swap().

◆ parent_of()

template<typename T , class Comp >
size_t Aleph::Gen_Cartesian_Tree< T, Comp >::parent_of ( const size_t  i) const
inline

◆ right_child()

template<typename T , class Comp >
size_t Aleph::Gen_Cartesian_Tree< T, Comp >::right_child ( const size_t  i) const
inline

◆ root()

template<typename T , class Comp >
constexpr size_t Aleph::Gen_Cartesian_Tree< T, Comp >::root ( ) const
inlineconstexprnoexcept

Index of the root node.

Returns
root index, or NONE if empty.

Definition at line 298 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::root_.

Referenced by Moved_From_Case< Cartesian_Tree< int > >::is_empty(), scenario_1(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ size()

template<typename T , class Comp >
constexpr size_t Aleph::Gen_Cartesian_Tree< T, Comp >::size ( ) const
inlineconstexprnoexcept

◆ swap()

template<typename T , class Comp >
void Aleph::Gen_Cartesian_Tree< T, Comp >::swap ( Gen_Cartesian_Tree< T, Comp > &  other)
inlinenoexcept

Swap with other in O(1).

Parameters
[in,out]othertree to exchange with.
Exceptions
whatswapping the comparisons throws. They are exchanged first, so that, if that swap throws, nothing else is exchanged, and both trees are left as they were if it left its arguments as they were.

Definition at line 473 of file tpl_cartesian_tree.H.

References Aleph::Gen_Cartesian_Tree< T, Comp >::comp_, Aleph::Gen_Cartesian_Tree< T, Comp >::data_, Aleph::Gen_Cartesian_Tree< T, Comp >::left_, Aleph::Gen_Cartesian_Tree< T, Comp >::n_, Aleph::Gen_Cartesian_Tree< T, Comp >::parent_, Aleph::Gen_Cartesian_Tree< T, Comp >::right_, Aleph::Gen_Cartesian_Tree< T, Comp >::root_, Aleph::Array< T >::swap(), and Aleph::Gen_Cartesian_Tree< T, Comp >::swap().

Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::operator=(), Aleph::Gen_Cartesian_Tree< T, Comp >::swap(), and TEST().

◆ values()

template<typename T , class Comp >
Array< T > Aleph::Gen_Cartesian_Tree< T, Comp >::values ( ) const
inline

Copy of the original data array.

Note
This function takes no parameters.
Returns
A new Array<T> with all stored values in original index order, returned by value (ownership transferred to the caller). If the tree is empty, returns an empty Array<T>.
Exceptions
std::bad_allocif array allocation fails.
Anyexception thrown by T copy assignment during element copy.
Note
No bounds-related exceptions are thrown (this method receives no indices).
Complexity: O(n) time and O(n) auxiliary space.

Definition at line 458 of file tpl_cartesian_tree.H.

References Aleph::Array< T >::create(), Aleph::Gen_Cartesian_Tree< T, Comp >::data_, and Aleph::Gen_Cartesian_Tree< T, Comp >::n_.

Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), and Moved_From_Case< Cartesian_Tree< int > >::use().

Member Data Documentation

◆ comp_

template<typename T , class Comp >
Comp Aleph::Gen_Cartesian_Tree< T, Comp >::comp_
private

◆ data_

◆ left_

◆ n_

◆ NONE

template<typename T , class Comp >
constexpr size_t Aleph::Gen_Cartesian_Tree< T, Comp >::NONE = static_cast<size_t>(-1)
staticconstexpr

◆ parent_

◆ right_

◆ root_


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