Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_cartesian_tree.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
68# ifndef TPL_CARTESIAN_TREE_H
69# define TPL_CARTESIAN_TREE_H
70
71# include <cassert>
72# include <cstddef>
73# include <concepts>
74# include <initializer_list>
75# include <type_traits>
76# include <utility>
77# include <vector>
78# include <tpl_array.H>
79# include <tpl_dynList.H>
80# include <tpl_sparse_table.H>
81# include <ahFunction.H>
82# include <ah-errors.H>
83# include <ah-concepts.H>
84
85namespace Aleph
86{
117 template <typename T, class Comp>
118 requires StrictWeakOrder<Comp, T>
120 {
121 public:
123 static constexpr size_t NONE = static_cast<size_t>(-1);
124
125 private:
126 Array<T> data_; // original values
127 Array<size_t> parent_; // parent[i] = parent of node i
128 Array<size_t> left_; // left[i] = left child of node i
129 Array<size_t> right_; // right[i] = right child of node i
130 size_t root_; // index of the root node
131 size_t n_; // number of elements
132
133 Comp comp_;
134
136 void build()
137 {
138 if (n_ == 0)
139 {
140 root_ = NONE;
141 return;
142 }
143
144 // Stack stored in a plain array — at most n elements
145 auto stk = Array<size_t>::create(n_);
146 size_t top = 0; // stack size (top points to next free slot)
147
148 for (size_t i = 0; i < n_; ++i)
149 {
150 size_t last_popped = NONE;
151
152 // Pop while i is strictly better than stack top (stable on ties)
153 while (top > 0 and comp_(data_(i), data_(stk(top - 1))))
154 {
155 last_popped = stk(top - 1);
156 --top;
157 }
158
159 // The last popped node becomes the left child of i
160 left_(i) = last_popped;
161 if (last_popped != NONE)
162 parent_(last_popped) = i;
163
164 // i becomes right child of the current stack top
165 if (top > 0)
166 {
167 right_(stk(top - 1)) = i;
168 parent_(i) = stk(top - 1);
169 }
170 else
171 parent_(i) = NONE;
172
173 stk(top++) = i;
174 }
175
176 root_ = stk(0); // the bottom of the stack is the root
177 }
178
181 {
185 for (size_t i = 0; i < n_; ++i)
186 {
187 parent_(i) = NONE;
188 left_(i) = NONE;
189 right_(i) = NONE;
190 }
191 build();
192 }
193
194 public:
196 using Item_Type = T;
197
202 Gen_Cartesian_Tree(const Array<T> & values, Comp c = Comp())
203 : data_(values), root_(NONE), n_(values.size()), comp_(c)
204 {
206 }
207
212 Gen_Cartesian_Tree(const std::vector<T> & values, Comp c = Comp())
213 : data_(Array<T>::create(values.size())), root_(NONE),
214 n_(values.size()), comp_(c)
215 {
216 for (size_t i = 0; i < n_; ++i)
217 data_(i) = values[i];
219 }
220
225 Gen_Cartesian_Tree(std::initializer_list<T> il, Comp c = Comp())
226 : data_(Array<T>::create(il.size())), root_(NONE),
227 n_(il.size()), comp_(c)
228 {
229 size_t i = 0;
230 for (auto it = il.begin(); it != il.end(); ++it)
231 data_(i++) = *it;
233 }
234
239 Gen_Cartesian_Tree(const DynList<T> & values, Comp c = Comp())
240 : data_(Array<T>::create(values.size())), root_(NONE),
241 n_(values.size()), comp_(c)
242 {
243 size_t i = 0;
244 for (auto it = values.get_it(); it.has_curr(); it.next_ne())
245 data_(i++) = it.get_curr();
247 }
248
254 Gen_Cartesian_Tree(const size_t num, const T & init, Comp c = Comp())
255 : data_(Array<T>::create(num)), root_(NONE), n_(num), comp_(c)
256 {
257 for (size_t i = 0; i < n_; ++i)
258 data_(i) = init;
260 }
261
264
267
276 std::is_nothrow_move_constructible_v<Array<T>> and
277 std::is_nothrow_move_constructible_v<Comp>)
278 : data_(std::move(other.data_)), parent_(std::move(other.parent_)),
279 left_(std::move(other.left_)), right_(std::move(other.right_)),
280 root_(std::exchange(other.root_, NONE)), n_(std::exchange(other.n_, 0)),
281 comp_(std::move(other.comp_))
282 {}
283
289 Gen_Cartesian_Tree &operator=(Gen_Cartesian_Tree && other) noexcept(noexcept(swap(other)))
290 {
291 swap(other);
292 return *this;
293 }
294
298 [[nodiscard]] constexpr size_t root() const noexcept { return root_; }
299
303 size_t left_child(const size_t i) const
304 {
306 << "Gen_Cartesian_Tree::left_child: i=" << i << " >= n=" << n_;
307 return left_(i);
308 }
309
313 size_t right_child(const size_t i) const
314 {
316 << "Gen_Cartesian_Tree::right_child: i=" << i << " >= n=" << n_;
317 return right_(i);
318 }
319
323 size_t parent_of(const size_t i) const
324 {
326 << "Gen_Cartesian_Tree::parent_of: i=" << i << " >= n=" << n_;
327 return parent_(i);
328 }
329
333 const T &data_at(const size_t i) const
334 {
336 << "Gen_Cartesian_Tree::data_at: i=" << i << " >= n=" << n_;
337 return data_(i);
338 }
339
341 [[nodiscard]] constexpr size_t size() const noexcept { return n_; }
342
344 [[nodiscard]] constexpr bool is_empty() const noexcept { return n_ == 0; }
345
349 bool is_leaf(const size_t i) const
350 {
352 << "Gen_Cartesian_Tree::is_leaf: i=" << i << " >= n=" << n_;
353 return left_(i) == NONE and right_(i) == NONE;
354 }
355
357 bool is_root(const size_t i) const
358 {
360 << "Gen_Cartesian_Tree::is_root: i=" << i << " >= n=" << n_;
361 return i == root_;
362 }
363
374 [[nodiscard]] size_t height() const
375 {
376 if (n_ == 0)
377 return 0;
378
379 size_t max_depth = 0;
380
381 // DFS with explicit stack: (node, depth)
382 struct Entry
383 {
384 size_t node;
385 size_t depth;
386 };
387 auto stk = Array<Entry>::create(n_);
388 size_t top = 0;
389 stk(top++) = {root_, 1};
390
391 while (top > 0)
392 {
393 auto [node, depth] = stk(--top);
394 if (depth > max_depth)
395 max_depth = depth;
396 if (right_(node) != NONE)
397 stk(top++) = {right_(node), depth + 1};
398 if (left_(node) != NONE)
399 stk(top++) = {left_(node), depth + 1};
400 }
401
402 return max_depth;
403 }
404
418 [[nodiscard]] Array<size_t> inorder() const
419 {
420 if (n_ == 0)
421 return {};
422
423 auto result = Array<size_t>::create(n_);
424 size_t idx = 0;
425
426 // Iterative inorder with explicit stack
427 auto stk = Array<size_t>::create(n_);
428 size_t top = 0;
429 size_t curr = root_;
430
431 while (curr != NONE or top > 0)
432 {
433 while (curr != NONE)
434 {
435 stk(top++) = curr;
436 curr = left_(curr);
437 }
438 curr = stk(--top);
439 result(idx++) = curr;
440 curr = right_(curr);
441 }
442
443 return result;
444 }
445
458 [[nodiscard]] Array<T> values() const
459 {
460 auto ret = Array<T>::create(n_);
461 for (size_t i = 0; i < n_; ++i)
462 ret(i) = data_(i);
463 return ret;
464 }
465
474 noexcept(noexcept(data_.swap(other.data_)) &&
475 noexcept(parent_.swap(other.parent_)) &&
476 noexcept(left_.swap(other.left_)) &&
477 noexcept(right_.swap(other.right_)) &&
478 noexcept(std::swap(root_, other.root_)) &&
479 noexcept(std::swap(n_, other.n_)) &&
480 std::is_nothrow_swappable_v<Comp>)
481 {
482 // the swap that the specification evaluates: that of Comp found by
483 // its arguments, else std::swap(); first, since it may throw, so that
484 // Gen_Euler_Tour_LCA, which swaps this tree before its tour, keeps
485 // the tour of its tree
486 using std::swap;
487 swap(comp_, other.comp_);
488 data_.swap(other.data_);
489 parent_.swap(other.parent_);
490 left_.swap(other.left_);
491 right_.swap(other.right_);
492 std::swap(root_, other.root_);
493 std::swap(n_, other.n_);
494 }
495 };
496
501 template <std::totally_ordered T>
503 : public Gen_Cartesian_Tree<T, Aleph::less<T>>
504 {
506 using Base::Base;
507 };
508
513 template <std::totally_ordered T>
515 : public Gen_Cartesian_Tree<T, Aleph::greater<T>>
516 {
518 using Base::Base;
519 };
520
521
522 // ================================================================
523 // Gen_Euler_Tour_LCA — LCA in O(1) via Euler Tour + Sparse Table
524 // ================================================================
525
549 template <typename T, class Comp>
552 {
555 {
556 size_t depth;
557 size_t node;
558 };
559
562 {
563 constexpr DepthEntry operator()(const DepthEntry & a,
564 const DepthEntry & b) const noexcept
565 {
566 return a.depth <= b.depth ? a : b;
567 }
568 };
569
571 Array<size_t> euler_; // euler tour: 2n-1 node indices
572 Array<size_t> depth_arr_; // depth at each euler tour position
573 Array<size_t> first_; // first_[node] = first position in euler
574 Array<size_t> node_depth_; // depth of each node in the tree
577
580 {
581 const size_t n = tree_.size();
582 if (n == 0)
583 return;
584
585 constexpr size_t NONE = Gen_Cartesian_Tree<T, Comp>::NONE;
586
587 euler_size_ = 2 * n - 1;
592
593 for (size_t i = 0; i < n; ++i)
594 first_(i) = NONE;
595
596 // Stack entries: (node, phase, depth, cached children)
597 // phase 0: first visit — record euler, push left child
598 // phase 1: back from left — if left existed, record euler;
599 // push right child
600 // phase 2: back from right — if right existed, record euler;
601 // pop
602 struct Frame
603 {
604 size_t node;
605 int phase;
606 size_t depth;
607 size_t left;
608 size_t right;
609 };
610 auto make_frame = [this](const size_t node, const size_t depth) -> Frame
611 {
612 return {node, 0, depth, tree_.left_child(node), tree_.right_child(node)};
613 };
614 auto stk = Array<Frame>::create(n);
615 size_t top = 0;
616 size_t eidx = 0;
617
618 stk(top++) = make_frame(tree_.root(), 0);
619
620 while (top > 0)
621 {
622 if (auto & frame = stk(top - 1); frame.phase == 0)
623 {
624 // First visit — always record
625 euler_(eidx) = frame.node;
626 depth_arr_(eidx) = frame.depth;
627 node_depth_(frame.node) = frame.depth;
628 first_(frame.node) = eidx;
629 ++eidx;
630 frame.phase = 1;
631
632 if (frame.left != NONE)
633 stk(top++) = make_frame(frame.left, frame.depth + 1);
634 }
635 else if (frame.phase == 1)
636 {
637 // Back from left subtree
638 if (frame.left != NONE)
639 {
640 // We actually returned from left child — record again
641 euler_(eidx) = frame.node;
642 depth_arr_(eidx) = frame.depth;
643 ++eidx;
644 }
645 frame.phase = 2;
646
647 if (frame.right != NONE)
648 stk(top++) = make_frame(frame.right, frame.depth + 1);
649 }
650 else
651 {
652 // Back from right subtree
653 if (frame.right != NONE)
654 {
655 // We actually returned from right child — record again
656 euler_(eidx) = frame.node;
657 depth_arr_(eidx) = frame.depth;
658 ++eidx;
659 }
660 --top;
661 }
662 }
663
664 // Euler tour length is exactly 2*n-1 for any non-empty binary tree.
665 assert(eidx == euler_size_);
666 }
667
670 {
671 if (euler_size_ == 0)
672 return;
673
675 for (size_t i = 0; i < euler_size_; ++i)
676 entries(i) = {depth_arr_(i), euler_(i)};
677
679 }
680
681 public:
686 Gen_Euler_Tour_LCA(const Array<T> & values, Comp c = Comp())
687 : tree_(values, c), sparse_(0, DepthEntry{0, 0}), euler_size_(0)
688 {
691 }
692
694 Gen_Euler_Tour_LCA(const std::vector<T> & values, Comp c = Comp())
695 : tree_(values, c), sparse_(0, DepthEntry{0, 0}), euler_size_(0)
696 {
699 }
700
702 Gen_Euler_Tour_LCA(std::initializer_list<T> il, Comp c = Comp())
703 : tree_(il, c), sparse_(0, DepthEntry{0, 0}), euler_size_(0)
704 {
707 }
708
710 Gen_Euler_Tour_LCA(const DynList<T> & values, Comp c = Comp())
711 : tree_(values, c), sparse_(0, DepthEntry{0, 0}), euler_size_(0)
712 {
715 }
716
718 Gen_Euler_Tour_LCA(const size_t num, const T & init, Comp c = Comp())
719 : tree_(num, init, c), sparse_(0, DepthEntry{0, 0}), euler_size_(0)
720 {
723 }
724
728 Gen_Euler_Tour_LCA(const Gen_Euler_Tour_LCA & other) = default;
729
740 std::is_nothrow_move_constructible_v<Gen_Cartesian_Tree<T, Comp>>)
741 : tree_(std::move(other.tree_)), euler_(std::move(other.euler_)),
742 depth_arr_(std::move(other.depth_arr_)), first_(std::move(other.first_)),
743 node_depth_(std::move(other.node_depth_)), sparse_(std::move(other.sparse_)),
744 euler_size_(std::exchange(other.euler_size_, 0))
745 {}
746
752
758 Gen_Euler_Tour_LCA & operator=(Gen_Euler_Tour_LCA && other) noexcept(noexcept(swap(other)))
759 {
760 swap(other);
761 return *this;
762 }
763
772 void swap(Gen_Euler_Tour_LCA & other) noexcept(std::is_nothrow_swappable_v<Comp>)
773 {
774 tree_.swap(other.tree_);
775 euler_.swap(other.euler_);
776 depth_arr_.swap(other.depth_arr_);
777 first_.swap(other.first_);
778 node_depth_.swap(other.node_depth_);
779 sparse_.swap(other.sparse_);
780 std::swap(euler_size_, other.euler_size_);
781 }
782
791 size_t lca(const size_t u, const size_t v) const
792 {
793 const size_t n = tree_.size();
795 << "Gen_Euler_Tour_LCA::lca: u=" << u << " >= n=" << n;
797 << "Gen_Euler_Tour_LCA::lca: v=" << v << " >= n=" << n;
798
799 size_t l = first_(u);
800 size_t r = first_(v);
801 if (l > r)
802 std::swap(l, r);
803
804 return sparse_.query(l, r).node;
805 }
806
810 size_t depth_of(const size_t u) const
811 {
813 << "Gen_Euler_Tour_LCA::depth_of: u=" << u << " >= n=" << tree_.size();
814 return node_depth_(u);
815 }
816
821 size_t distance(const size_t u, const size_t v) const
822 {
823 const size_t ancestor = lca(u, v);
824 return node_depth_(u) + node_depth_(v) - 2 * node_depth_(ancestor);
825 }
826
828 [[nodiscard]] constexpr size_t size() const noexcept
829 {
830 return tree_.size();
831 }
832
834 [[nodiscard]] constexpr bool is_empty() const noexcept
835 {
836 return tree_.is_empty();
837 }
838
840 const Gen_Cartesian_Tree<T, Comp> &tree() const noexcept
841 {
842 return tree_;
843 }
844
846 [[nodiscard]] Array<size_t> euler_tour() const
847 {
849 for (size_t i = 0; i < euler_size_; ++i)
850 ret(i) = euler_(i);
851 return ret;
852 }
853
855 [[nodiscard]] size_t euler_tour_size() const noexcept
856 {
857 return euler_size_;
858 }
859 };
860
865 template <std::totally_ordered T>
867 : public Gen_Euler_Tour_LCA<T, Aleph::less<T>>
868 {
870 using Base::Base;
871 };
872
877 template <std::totally_ordered T>
879 : public Gen_Euler_Tour_LCA<T, Aleph::greater<T>>
880 {
882 using Base::Base;
883 };
884
885
886 // ================================================================
887 // Gen_Cartesian_Tree_RMQ — O(1) RMQ via Cartesian Tree + LCA
888 // ================================================================
889
915 template <typename T, class Comp>
918 {
920
921 public:
923 using Item_Type = T;
924
926 Gen_Cartesian_Tree_RMQ(const Array<T> & values, Comp c = Comp())
927 : lca_(values, c) {}
928
930 Gen_Cartesian_Tree_RMQ(const std::vector<T> & values, Comp c = Comp())
931 : lca_(values, c) {}
932
934 Gen_Cartesian_Tree_RMQ(std::initializer_list<T> il, Comp c = Comp())
935 : lca_(il, c) {}
936
938 Gen_Cartesian_Tree_RMQ(const DynList<T> & values, Comp c = Comp())
939 : lca_(values, c) {}
940
942 Gen_Cartesian_Tree_RMQ(const size_t num, const T & init,
943 Comp c = Comp())
944 : lca_(num, init, c) {}
945
955 T query(const size_t l, const size_t r) const
956 {
958 << "Gen_Cartesian_Tree_RMQ::query: l=" << l << " > r=" << r;
960 << "Gen_Cartesian_Tree_RMQ::query: r=" << r << " >= n=" << lca_.size();
961
962 return lca_.tree().data_at(lca_.lca(l, r));
963 }
964
974 size_t query_idx(const size_t l, const size_t r) const
975 {
977 << "Gen_Cartesian_Tree_RMQ::query_idx: l=" << l << " > r=" << r;
979 << "Gen_Cartesian_Tree_RMQ::query_idx: r=" << r << " >= n=" << lca_.size();
980
981 return lca_.lca(l, r);
982 }
983
990 T get(const size_t i) const
991 {
992 return lca_.tree().data_at(i);
993 }
994
996 [[nodiscard]] constexpr size_t size() const noexcept
997 {
998 return lca_.size();
999 }
1000
1002 [[nodiscard]] constexpr bool is_empty() const noexcept
1003 {
1004 return lca_.is_empty();
1005 }
1006
1008 [[nodiscard]] Array<T> values() const
1009 {
1010 return lca_.tree().values();
1011 }
1012
1015 {
1016 return lca_;
1017 }
1018 };
1019
1024 template <std::totally_ordered T>
1026 : public Gen_Cartesian_Tree_RMQ<T, Aleph::less<T>>
1027 {
1029 using Base::Base;
1030 };
1031
1036 template <std::totally_ordered T>
1038 : public Gen_Cartesian_Tree_RMQ<T, Aleph::greater<T>>
1039 {
1041 using Base::Base;
1042 };
1043} // end namespace Aleph
1044
1045# endif /* TPL_CARTESIAN_TREE_H */
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
Standard functor implementations and comparison objects.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
O(1) static range queries via the Cartesian Tree reduction.
constexpr size_t size() const noexcept
Number of elements.
T get(const size_t i) const
Retrieve the value at position i in O(1).
const Gen_Euler_Tour_LCA< T, Comp > & lca_engine() const noexcept
Const reference to the internal LCA engine.
Array< T > values() const
Copy of the original values.
Gen_Cartesian_Tree_RMQ(const DynList< T > &values, Comp c=Comp())
Construct from a DynList<T>.
Gen_Cartesian_Tree_RMQ(const size_t num, const T &init, Comp c=Comp())
Construct with num elements all equal to init.
Gen_Cartesian_Tree_RMQ(const std::vector< T > &values, Comp c=Comp())
Construct from a std::vector<T>.
Gen_Cartesian_Tree_RMQ(std::initializer_list< T > il, Comp c=Comp())
Construct from an initializer_list<T>.
Gen_Cartesian_Tree_RMQ(const Array< T > &values, Comp c=Comp())
Construct from an Array<T>.
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).
Gen_Euler_Tour_LCA< T, Comp > lca_
T Item_Type
The type of the element.
size_t query_idx(const size_t l, const size_t r) const
Return the index of the optimal element in a[l..r].
constexpr bool is_empty() const noexcept
True if empty.
Explicit Cartesian Tree built in O(n) with a monotonic stack.
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).
Gen_Cartesian_Tree(std::initializer_list< T > il, Comp c=Comp())
Construct from an initializer_list<T>.
Array< size_t > inorder() const
Inorder traversal of the tree.
T Item_Type
The type of the element stored in the tree.
constexpr size_t root() const noexcept
Index of the root node.
static constexpr size_t NONE
Sentinel value for absent parent/child links.
Array< T > values() const
Copy of the original data array.
const T & data_at(const size_t i) const
Value at node i.
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.
size_t left_child(const size_t i) const
Left child of node i.
void init_and_build()
Initialize arrays of size n with NONE and build.
bool is_leaf(const size_t i) const
True if node i is a leaf (no children).
Gen_Cartesian_Tree(const std::vector< T > &values, Comp c=Comp())
Construct from a std::vector<T>.
Gen_Cartesian_Tree(const Gen_Cartesian_Tree &)=default
Copy constructor.
constexpr size_t size() const noexcept
Number of nodes.
Gen_Cartesian_Tree & operator=(const Gen_Cartesian_Tree &)=default
Copy assignment operator.
size_t parent_of(const size_t i) const
Parent of node i.
Gen_Cartesian_Tree(const Array< T > &values, Comp c=Comp())
Construct from an Array<T>.
Gen_Cartesian_Tree(const size_t num, const T &init, Comp c=Comp())
Construct with num elements all equal to init.
size_t right_child(const size_t i) const
Right child of node i.
void build()
Build the Cartesian Tree using a monotonic stack in O(n).
bool is_root(const size_t i) const
True if node i is the root.
constexpr bool is_empty() const noexcept
True if the tree is empty.
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(const DynList< T > &values, Comp c=Comp())
Construct from a DynList<T>.
size_t height() const
Height of the tree (longest root-to-leaf path).
Lowest Common Ancestor queries in O(1) via Euler Tour reduction to RMQ on a Cartesian Tree.
Gen_Euler_Tour_LCA(Gen_Euler_Tour_LCA &&other) noexcept(std::is_nothrow_move_constructible_v< Gen_Cartesian_Tree< T, Comp > >)
Take the tree and the tour of another engine, which is left empty.
Gen_Euler_Tour_LCA(std::initializer_list< T > il, Comp c=Comp())
Construct from an initializer_list<T>.
size_t euler_tour_size() const noexcept
Size of the Euler Tour (should be 2n-1 for n > 0).
void swap(Gen_Euler_Tour_LCA &other) noexcept(std::is_nothrow_swappable_v< Comp >)
Exchange in O(1) the tree and the tour of this engine and another.
Gen_Euler_Tour_LCA(const Array< T > &values, Comp c=Comp())
Construct from an Array<T>.
constexpr size_t size() const noexcept
Number of nodes.
Gen_Euler_Tour_LCA & operator=(Gen_Euler_Tour_LCA &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous tree and tour of this one.
void build_sparse_table()
Build the Sparse Table over the depth entries.
Gen_Euler_Tour_LCA(const std::vector< T > &values, Comp c=Comp())
Construct from a std::vector<T>.
const Gen_Cartesian_Tree< T, Comp > & tree() const noexcept
Const reference to the internal Cartesian Tree.
size_t depth_of(const size_t u) const
Depth of node u in the Cartesian Tree.
Gen_Euler_Tour_LCA(const Gen_Euler_Tour_LCA &other)=default
Copy the tree and the tour of another engine.
Gen_Euler_Tour_LCA(const size_t num, const T &init, Comp c=Comp())
Construct with num elements all equal to init.
Gen_Euler_Tour_LCA(const DynList< T > &values, Comp c=Comp())
Construct from a DynList<T>.
constexpr bool is_empty() const noexcept
True if empty.
Gen_Euler_Tour_LCA & operator=(const Gen_Euler_Tour_LCA &other)=default
Copy the tree and the tour of another engine.
size_t lca(const size_t u, const size_t v) const
Lowest Common Ancestor of nodes u and v in O(1).
size_t distance(const size_t u, const size_t v) const
Distance between nodes u and v in the tree.
Gen_Cartesian_Tree< T, Comp > tree_
Array< size_t > euler_tour() const
Copy of the Euler Tour array (for inspection/debugging).
Gen_Sparse_Table< DepthEntry, DepthMinOp > sparse_
void build_euler_tour()
Build Euler Tour iteratively using an explicit stack with phases.
Sparse Table over an arbitrary associative and idempotent binary operation.
Strict weak ordering constraint for BST comparators.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
static std::atomic< bool > init
Definition hash-fct.C:54
STL namespace.
Range minimum queries via Cartesian Tree.
Range maximum queries via Cartesian Tree.
Cartesian Tree for min-heap (range minimum).
LCA on a min-heap Cartesian Tree.
Entry for the depth-based Sparse Table: (depth, euler_node_index).
Min-op on DepthEntry: compares by depth.
constexpr DepthEntry operator()(const DepthEntry &a, const DepthEntry &b) const noexcept
Cartesian Tree for max-heap (range maximum).
LCA on a max-heap Cartesian Tree.
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Alias for htlist.H (DynList implementation).
Sparse Table for static range queries in O(1).
DynList< int > l