|
Aleph-w 3.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 &&) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< Comp >)=default | |
| Gen_Cartesian_Tree & | operator= (Gen_Cartesian_Tree &&) noexcept(std::is_nothrow_move_assignable_v< Array< T > > and std::is_nothrow_move_assignable_v< Comp >)=default |
| 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.
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_, Aleph::divide_and_conquer_partition_dp(), 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.
|
defaultnoexcept |
|
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::divide_and_conquer_partition_dp(), 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().
Value at node i.
| std::out_of_range | if i >= size(). |
Definition at line 314 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_2().
|
inline |
Height of the tree (longest root-to-leaf path).
| std::bad_alloc | if temporary DFS stack allocation fails. |
Definition at line 355 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), 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_.
|
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 399 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), 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_.
|
inlineconstexprnoexcept |
True if the tree is empty.
Definition at line 325 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::n_.
|
inline |
True if node i is a leaf (no children).
| std::out_of_range | if i >= size(). |
Definition at line 330 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_.
|
inline |
True if node i is the root.
Definition at line 338 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 284 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_.
|
default |
Copy assignment operator.
|
defaultnoexcept |
|
inline |
Parent of node i.
| std::out_of_range | if i >= size(). |
Definition at line 304 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_.
|
inline |
Right child of node i.
| std::out_of_range | if i >= size(). |
Definition at line 294 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_.
|
inlineconstexprnoexcept |
Index of the root node.
Definition at line 279 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::root_.
Referenced by scenario_1(), and TEST().
|
inlineconstexprnoexcept |
Number of nodes.
Definition at line 322 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::n_.
|
inlinenoexcept |
Swap with other in O(1).
Definition at line 448 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree< T, Comp >::comp_, Aleph::Gen_Cartesian_Tree< T, Comp >::data_, Aleph::divide_and_conquer_partition_dp(), 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_, and Aleph::Array< T >::swap().
Referenced by 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 439 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Cartesian_Tree< T, Comp >::data_, Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Cartesian_Tree< T, Comp >::n_.
Referenced by Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree(), and Aleph::Gen_Cartesian_Tree< T, Comp >::Gen_Cartesian_Tree().
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().
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().
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().
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().