Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binNodeGenerators.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
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
73#ifndef TPL_BINNODEGENERATORS_H
74#define TPL_BINNODEGENERATORS_H
75
76#include <ah-generator.H>
77#include <tpl_arrayStack.H>
78#include <tpl_binNode.H>
79#include <tpl_binNodeUtils.H>
80
81namespace Aleph
82{
83
110template <class Node>
112{
113 if (root == Node::NullPtr)
114 co_return;
115
116 for (BinNodeInfixIterator<Node> it(root); it.has_curr(); it.next_ne())
117 {
118 Node *n = it.get_curr_ne();
119 co_yield n;
120 }
121}
122
141template <class Node>
143{
144 if (root == Node::NullPtr)
145 co_return;
146
147 for (BinNodePrefixIterator<Node> it(root); it.has_curr(); it.next_ne())
148 {
149 Node *n = it.get_curr_ne();
150 co_yield n;
151 }
152}
153
171template <class Node>
173{
174 if (root == Node::NullPtr)
175 co_return;
176
179 ArrayStack<Node *> stack(Node::MaxHeight);
180 Node *curr = root;
181 Node *last = Node::NullPtr;
182
183 // Iterative post-order: descend left spines; yield a node only after its
184 // right subtree root was the last node yielded.
185 while (curr != Node::NullPtr or not stack.is_empty())
186 {
187 while (curr != Node::NullPtr)
188 {
189 stack.push(curr);
190 curr = LLINK(curr);
191 }
192
193 Node *peek = stack.top();
194 Node *right = RLINK(peek);
195 if (right != Node::NullPtr and last != right)
196 {
197 curr = right;
198 continue;
199 }
200
201 last = stack.pop();
202 Node *n = last;
203 co_yield n;
204 }
205}
206
207} // end namespace Aleph
208
209#endif // TPL_BINNODEGENERATORS_H
Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines.
WeightedDigraph::Node Node
Stack implemented with simple dynamic array and with bounds verification.
T & push(const T &data)
Push into stack a copy of data
T & top()
Return a modifiable reference to youngest element of stack (called the top)
bool is_empty() const noexcept
Return true if stack is empty.
T pop()
Extract the last more recently inserted element.
Inorder iterator on the nodes of a binary tree.
bool has_curr() const noexcept
Return true the iterator has current node.
Preorder iterator on the nodes of a binary tree.
bool has_curr() const noexcept
Return true if iterator has current node.
Lazy, single-pass sequence of T values produced by a coroutine.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
Aleph::Generator< Node * > lazy_in_order(Node *root)
Lazily traverse a binary tree in-order (left, node, right).
Aleph::Generator< Node * > lazy_post_order(Node *root)
Lazily traverse a binary tree post-order (left, right, node).
Aleph::Generator< Node * > lazy_pre_order(Node *root)
Lazily traverse a binary tree pre-order (node, left, right).
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
#define RLINK(i, n)
#define LLINK(i, n)
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Basic binary tree node definitions.