44 for (
int i = 0; i < depth; ++i)
50 while (child !=
nullptr)
53 child = child->get_right_sibling();
67 while (child !=
nullptr)
79 cout <<
"=== Graph to Tree Conversion: Educational Examples ===\n\n";
85 cout <<
"--- Example 1: Converting an Acyclic Graph (Tree) ---\n\n";
90 cout <<
"Building a tree-structured graph:\n";
91 cout <<
" A <- root\n";
113 cout <<
"This graph is acyclic (a tree with n-1 = 5 edges)\n\n";
122 cout <<
"Converting to Tree_Node structure...\n";
128 cout <<
"Result (Tree_Node hierarchy):\n";
131 cout <<
"\nKEY POINT: graph_to_tree_node() only works on acyclic graphs!\n";
132 cout <<
" If your graph has cycles, it will throw domain_error.\n\n";
141 cout <<
"--- Example 2: Graph with Cycles (Two-Step Process) ---\n\n";
146 cout <<
"Building a graph WITH cycles:\n";
149 cout <<
" B---C <- B-C creates a cycle A-B-C-A\n";
151 cout <<
" D---E <- D-E creates another cycle\n\n";
169 cout <<
"This graph has cycles (6 edges > 4 needed for tree)\n\n";
172 cout <<
"Step 1: Extract DFS spanning tree...\n";
177 cout <<
"(Exactly n-1 = 4 edges, as expected for a tree)\n\n";
181 for (GT::Node_Iterator it(spanning_tree); it.has_curr(); it.next())
183 if (it.get_curr()->get_info() ==
"A")
191 cout <<
"Step 2: Convert spanning tree to Tree_Node...\n";
202 cout <<
"Result (Tree_Node hierarchy):\n";
205 cout <<
"\nNOTE: The spanning tree removed the cycle-creating edges.\n\n";
214 cout <<
"--- Example 3: BFS vs DFS Spanning Trees ---\n\n";
219 cout <<
"Building a grid graph:\n";
220 cout <<
" 0---1---2\n";
222 cout <<
" 3---4---5\n\n";
226 for (
int i = 0; i < 6; ++i)
250 for (GT::Node_Iterator it(
dfs_tree); it.has_curr(); it.next())
251 if (it.get_curr()->get_info() == 0)
253 root = it.get_curr();
260 cout <<
"DFS Spanning Tree (tends to be DEEP):\n";
272 for (GT::Node_Iterator it(
bfs_tree); it.has_curr(); it.next())
273 if (it.get_curr()->get_info() == 0)
275 root = it.get_curr();
282 cout <<
"BFS Spanning Tree (tends to be SHALLOW):\n";
289 cout <<
"DFS: Explores one path deeply before backtracking.\n";
290 cout <<
"BFS: Explores all neighbors at distance k before k+1.\n";
291 cout <<
" BFS tree gives shortest paths from root!\n\n";
298 cout <<
"--- Example 4: Functor Class API ---\n\n";
310 cout <<
"Simple tree: A -> {B, C}\n\n";
321 cout <<
"Converted using Graph_To_Tree_Node functor:\n";
324 cout <<
"\nThe functor class allows storing arc filters.\n\n";
333 cout <<
"--- Example 5: Generate ntreepic Specification ---\n\n";
358 cout <<
"Tree structure:\n";
361 cout <<
"\nntreepic specification (for visualization):\n";
362 cout <<
"-------------------------------------------\n";
364 cout <<
"-------------------------------------------\n";
365 cout <<
"\nThis output can be used with ntreepic to generate LaTeX.\n\n";
370 cout <<
"=== SUMMARY ===\n\n";
371 cout <<
"1. Graph_To_Tree_Node: Converts ACYCLIC graphs only\n";
372 cout <<
" - Input must be a tree (no cycles)\n";
373 cout <<
" - Throws domain_error if cycles detected\n\n";
374 cout <<
"2. For graphs WITH cycles:\n";
375 cout <<
" a) Extract spanning tree: find_depth_first_spanning_tree()\n";
376 cout <<
" or find_breadth_first_spanning_tree()\n";
377 cout <<
" b) Convert spanning tree: Graph_To_Tree_Node()(tree, root)\n\n";
378 cout <<
"3. DFS vs BFS spanning trees:\n";
379 cout <<
" - DFS: Deep, narrow trees\n";
380 cout <<
" - BFS: Shallow, wide trees (shortest paths from root)\n\n";
381 cout <<
"4. Tree_Node can be visualized with generate_tree() + ntreepic\n\n";
Functor class to convert a tree graph to Tree_Node structure.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
Tree visualization and output generation.
__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)
Convert spanning tree graphs to Tree_Node structures.
void delete_tree(Tree_Node< T > *node)
void print_tree(Tree_Node< T > *node, int depth=0)
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
General tree (n-ary tree) node.