Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lazy_tree_traversal_example.cc
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
53#include <iostream>
54#include <vector>
55
56#include <print_rule.H>
57#include <tpl_binNode.H>
59#include <tpl_binNodeUtils.H>
60
61using namespace Aleph;
62
63namespace
64{
65// Owns every node allocated for the example tree and frees them on exit.
66struct Node_Pool
67{
68 std::vector<BinNode<int> *> allocated;
69
70 BinNode<int> *make(int k)
71 {
72 auto *p = new BinNode<int>(k);
73 allocated.push_back(p);
74 return p;
75 }
76
78 {
79 for (BinNode<int> *p : allocated)
80 delete p;
81 }
82};
83
84// Builds a complete, perfectly balanced BST over the ascending range
85// [lo, hi] by recursively picking the midpoint as the root — the classic
86// sorted-array-to-balanced-BST construction. Over 1..15 this produces:
87//
88// 8
89// 4 12
90// 2 6 10 14
91// 1 3 5 7 9 11 13 15
92BinNode<int> *build_balanced_bst(Node_Pool &pool, int lo, int hi)
93{
94 if (lo > hi)
96 const int mid = lo + (hi - lo) / 2;
97 BinNode<int> *node = pool.make(mid);
98 node->getL() = build_balanced_bst(pool, lo, mid - 1);
99 node->getR() = build_balanced_bst(pool, mid + 1, hi);
100 return node;
101}
102
104{
105 std::cout << "[1] in-order / pre-order / post-order, driven lazily\n";
106 print_rule();
107
108 std::cout << "in-order: ";
110 std::cout << KEY(n) << " ";
111 std::cout << "\n";
112
113 std::cout << "pre-order: ";
115 std::cout << KEY(n) << " ";
116 std::cout << "\n";
117
118 std::cout << "post-order: ";
120 std::cout << KEY(n) << " ";
121 std::cout << "\n\n";
122}
123
125{
126 std::cout << "[2] Early stop: callback contract vs plain break\n";
127 print_rule();
128
129 constexpr int target = 5;
130
131 // --- Eager for_each: its callback returns void, so this helper cannot stop
132 // before the end of the traversal.
133 int for_each_visits = 0;
134 bool for_each_found = false;
136 {
138 if (KEY(n) == target)
139 for_each_found = true;
140 // No return value here: this helper is intentionally unconditional.
141 });
142 std::cout << "Eager for_each_in_order searching for " << target << ":\n";
143 std::cout << " found=" << std::boolalpha << for_each_found
144 << ", nodes visited=" << for_each_visits << " (always all 15)\n";
145
146 // --- Eager traverse: iterator-based traversal can stop early too, but the
147 // stop condition is encoded in the callback's boolean return value.
148 int traverse_visits = 0;
149 bool traverse_found = false;
151 {
153 if (KEY(n) == target)
154 {
155 traverse_found = true;
156 return false;
157 }
158 return true;
159 });
160 std::cout << "Eager infix_traverse searching for " << target << ":\n";
161 std::cout << " found=" << traverse_found
162 << ", nodes visited=" << traverse_visits << " (stopped by false)\n";
163
164 // --- Lazy: a plain `break` stops the coroutine at the point of interest.
165 int lazy_visits = 0;
166 bool lazy_found = false;
168 {
169 ++lazy_visits;
170 if (KEY(n) == target)
171 {
172 lazy_found = true;
173 break;
174 }
175 }
176 std::cout << "Lazy lazy_in_order searching for " << target << ":\n";
177 std::cout << " found=" << lazy_found
178 << ", nodes visited=" << lazy_visits << " (stopped as soon as found)\n\n";
179
180 std::cout << "In-order visits 1,2,3,4,5 before reaching the target 5. Both\n"
181 "infix_traverse and lazy_in_order avoid the remaining nodes;\n"
182 "the lazy version reads as ordinary iteration-with-break and\n"
183 "can be piped through other Generator stages.\n\n";
184}
185} // namespace
186
187int main()
188{
189 std::cout << "\n=== Lazy binary tree traversal ===\n\n";
190
191 Node_Pool pool;
192 BinNode<int> *root = build_balanced_bst(pool, 1, 15);
193
196
197 std::cout << "Done.\n";
198 return 0;
199}
@ KEY
Definition btreepic.C:169
Node for binary search tree.
Conjunto de nodos a reusarse.
__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:4071
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
void for_each_in_order(Node *root, Op &&op)
Execute an operation in order sense for each node of tree.
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).
bool infix_traverse(Node *root, Op op)
Traverse a tree in inorder via its iterator and performs a conditioned operation on each item.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
STL namespace.
static int * k
Lazy (coroutine-based) traversals of binary trees.
Utility functions for binary tree operations.
Basic binary tree node definitions.