|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Explicit Cartesian Tree built in O(n) with a monotonic stack. More...
#include <tpl_cartesian_tree.H>
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_ |
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:
Comp, i.e. !Comp(a[child], a[parent]) for every parent–child pair (min-heap by default; equality is allowed).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.
| T | element type. |
| Comp | binary comparator defining the heap property. |
Definition at line 119 of file tpl_cartesian_tree.H.
| 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.
|
inline |
Construct from an Array<T>.
| values | source array. |
| c | comparator (default-constructed). |
Definition at line 202 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build().
|
inline |
Construct from a std::vector<T>.
| values | source vector. |
| c | comparator. |
Definition at line 212 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::data_, Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), Aleph::Gen_Cartesian_Tree< T, Comp >::n_, and Aleph::Gen_Cartesian_Tree< T, Comp >::values().
|
inline |
Construct from an initializer_list<T>.
| il | initializer list. |
| c | comparator. |
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().
|
inline |
Construct from a DynList<T>.
| values | source list. |
| c | comparator. |
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().
|
inline |
Construct with num elements all equal to init.
| num | number of elements. |
| init | initial value for every position. |
| c | comparator. |
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_.
|
default |
Copy constructor.
|
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.
| [in,out] | other | tree to move from. |
Definition at line 275 of file tpl_cartesian_tree.H.
|
inlineprivate |
Build the Cartesian Tree using a monotonic stack in O(n).
Definition at line 136 of file tpl_cartesian_tree.H.
References Aleph::and, Aleph::Gen_Cartesian_Tree< T, Comp >::comp_, Aleph::Array< T >::create(), 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 >::NONE, Aleph::Gen_Cartesian_Tree< T, Comp >::parent_, Aleph::Gen_Cartesian_Tree< T, Comp >::right_, and Aleph::Gen_Cartesian_Tree< T, Comp >::root_.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build().
|
inline |
Value at node i.
| std::out_of_range | if i >= size(). |
Definition at line 333 of file tpl_cartesian_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Cartesian_Tree< T, Comp >::data_, and Aleph::Gen_Cartesian_Tree< T, Comp >::n_.
Referenced by scenario_1(), scenario_2(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().
|
inline |
Height of the tree (longest root-to-leaf path).
| std::bad_alloc | if temporary DFS stack allocation fails. |
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().
|
inlineprivate |
Initialize arrays of size n with NONE and build.
Definition at line 180 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::build(), 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 >::parent_, and Aleph::Gen_Cartesian_Tree< T, Comp >::right_.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), and Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree().
|
inline |
Inorder traversal of the tree.
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>. | std::bad_alloc | if result/stack allocation fails. |
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().
|
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().
|
inline |
True if node i is a leaf (no children).
| std::out_of_range | if i >= size(). |
Definition at line 349 of file tpl_cartesian_tree.H.
References ah_out_of_range_error_if, Aleph::and, Aleph::Gen_Cartesian_Tree< T, Comp >::left_, Aleph::Gen_Cartesian_Tree< T, Comp >::n_, Aleph::Gen_Cartesian_Tree< T, Comp >::NONE, and Aleph::Gen_Cartesian_Tree< T, Comp >::right_.
Referenced by scenario_1(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().
|
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_.
|
inline |
Left child of node i.
| std::out_of_range | if i >= size(). |
Definition at line 303 of file tpl_cartesian_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Cartesian_Tree< T, Comp >::left_, and Aleph::Gen_Cartesian_Tree< T, Comp >::n_.
Referenced by scenario_1(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().
|
default |
Copy assignment operator.
|
inlinenoexcept |
Exchange this tree with another, which is left with the previous nodes of this one.
| [in,out] | other | tree to move from. |
Definition at line 289 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::swap().
|
inline |
Parent of node i.
| std::out_of_range | if i >= size(). |
Definition at line 323 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 >::parent_.
Referenced by scenario_1(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().
|
inline |
Right child of node i.
| std::out_of_range | if i >= size(). |
Definition at line 313 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 >::right_.
Referenced by scenario_1(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().
|
inlineconstexprnoexcept |
Index of the root node.
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().
|
inlineconstexprnoexcept |
Number of nodes.
Definition at line 341 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::n_.
Referenced by scenario_1(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Cartesian_Tree< int > >::use().
|
inlinenoexcept |
Swap with other in O(1).
| [in,out] | other | tree to exchange with. |
| what | swapping 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().
|
inline |
Copy of the original data array.
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>. | std::bad_alloc | if array allocation fails. |
| Any | exception thrown by T copy assignment during element copy. |
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().
|
private |
Definition at line 133 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::build(), and Aleph::Gen_Cartesian_Tree< T, Comp >::swap().
|
private |
Definition at line 126 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::data_at(), Aleph::Gen_Cartesian_Tree< T, Comp >::swap(), and Aleph::Gen_Cartesian_Tree< T, Comp >::values().
|
private |
Definition at line 128 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::height(), Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), Aleph::Gen_Cartesian_Tree< T, Comp >::inorder(), Aleph::Gen_Cartesian_Tree< T, Comp >::is_leaf(), Aleph::Gen_Cartesian_Tree< T, Comp >::left_child(), and Aleph::Gen_Cartesian_Tree< T, Comp >::swap().
|
private |
Definition at line 131 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::data_at(), Aleph::Gen_Cartesian_Tree< T, Comp >::height(), Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), Aleph::Gen_Cartesian_Tree< T, Comp >::inorder(), Aleph::Gen_Cartesian_Tree< T, Comp >::is_empty(), Aleph::Gen_Cartesian_Tree< T, Comp >::is_leaf(), Aleph::Gen_Cartesian_Tree< T, Comp >::is_root(), Aleph::Gen_Cartesian_Tree< T, Comp >::left_child(), Aleph::Gen_Cartesian_Tree< T, Comp >::parent_of(), Aleph::Gen_Cartesian_Tree< T, Comp >::right_child(), Aleph::Gen_Cartesian_Tree< T, Comp >::size(), Aleph::Gen_Cartesian_Tree< T, Comp >::swap(), and Aleph::Gen_Cartesian_Tree< T, Comp >::values().
|
staticconstexpr |
Sentinel value for absent parent/child links.
Definition at line 123 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::height(), Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), Aleph::Gen_Cartesian_Tree< T, Comp >::inorder(), and Aleph::Gen_Cartesian_Tree< T, Comp >::is_leaf().
|
private |
Definition at line 127 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), Aleph::Gen_Cartesian_Tree< T, Comp >::parent_of(), and Aleph::Gen_Cartesian_Tree< T, Comp >::swap().
|
private |
Definition at line 129 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::height(), Aleph::Gen_Cartesian_Tree< T, Comp >::init_and_build(), Aleph::Gen_Cartesian_Tree< T, Comp >::inorder(), Aleph::Gen_Cartesian_Tree< T, Comp >::is_leaf(), Aleph::Gen_Cartesian_Tree< T, Comp >::right_child(), and Aleph::Gen_Cartesian_Tree< T, Comp >::swap().
|
private |
Definition at line 130 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::build(), Aleph::Gen_Cartesian_Tree< T, Comp >::height(), Aleph::Gen_Cartesian_Tree< T, Comp >::inorder(), Aleph::Gen_Cartesian_Tree< T, Comp >::is_root(), Aleph::Gen_Cartesian_Tree< T, Comp >::root(), and Aleph::Gen_Cartesian_Tree< T, Comp >::swap().