Skip to content

Commit 17fa72b

Browse files
committed
graph_tools : printing tree
1 parent 76e267d commit 17fa72b

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

module/move/graphs_tools/src/tree_print.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,66 @@ mod private
3737
{
3838

3939
/// Print directed graph as a tree.
40-
fn _print_as_tree< 'w >( &self, write : &'w mut ( dyn core::fmt::Write + 'w ), node : Self::NodeId ) -> fmt::Result
40+
fn _print_as_tree< 'w >( &'g self, write : &'w mut ( dyn core::fmt::Write + 'w ), node_id : Self::NodeId ) -> fmt::Result
4141
{
42-
// let node = self.node_ref( node );
43-
write.write_fmt( format_args!( "{:?}", node ) )
42+
#![ allow( non_upper_case_globals ) ]
43+
use iter_tools::Itertools;
44+
const _up_down : &str = "│ ";
45+
const _up_down_right : &str = "├─ ";
46+
const _left_right : &str = "─";
47+
const _down_right : &str = "┌─";
48+
49+
let mut visited = collection_tools::HashSet::new();
50+
let mut stack = collection_tools::Vec::new();
51+
52+
let prefix = | level : isize |
53+
{
54+
let left = if level > 0
55+
{
56+
std::iter::repeat( _up_down ).take( ( level - 1 ) as usize ).join( " " )
57+
}
58+
else
59+
{
60+
String::new()
61+
};
62+
let right = if level > 0
63+
{
64+
_up_down_right
65+
}
66+
else
67+
{
68+
&String::new()
69+
};
70+
return format!( "{}{}", left, right );
71+
};
72+
73+
let push = | stack : &mut collection_tools::Vec< ( Self::NodeId, isize ) >, node_id, level |
74+
{
75+
println!( "push {:?} level:{}", node_id, level );
76+
stack.push( ( node_id, level ) );
77+
};
78+
79+
push( &mut stack, node_id, 0 );
80+
81+
while let Some( ( node_id, level ) ) = stack.pop()
82+
{
83+
84+
if visited.insert( node_id )
85+
{
86+
write.write_fmt( format_args!( "{}{:?}\n", prefix( level ), node_id ) )?;
87+
88+
for child_id in self.node_out_nodes( node_id ).rev()
89+
{
90+
push( &mut stack, child_id, level + 1 );
91+
}
92+
}
93+
}
94+
95+
return Ok( () )
4496
}
4597

4698
/// Print directed graph as a tree.
47-
fn print_as_tree< 'w >( &self, node : Self::NodeId ) -> String
99+
fn print_as_tree< 'w >( &'g self, node : Self::NodeId ) -> String
48100
{
49101
// let node = self.node_ref( node );
50102
let mut result = String::new();

module/move/graphs_tools/tests/inc/graph/map_of_nodes.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,27 @@ impl Graph
157157
graph
158158
}
159159

160+
pub fn triplet_with_double_legs() -> Self
161+
{
162+
163+
// Create nodes
164+
let mut node0 = Node::new( 0 );
165+
let mut node1 = Node::new( 1 );
166+
let node2 = Node::new( 2 );
167+
let mut node3 = Node::new( 3 );
168+
let node4 = Node::new( 4 );
169+
let node5 = Node::new( 5 );
170+
let node6 = Node::new( 6 );
171+
let node7 = Node::new( 7 );
172+
173+
node0.children_add([ &node1, &node2, &node3 ]);
174+
node1.children_add([ &node4, &node5 ]);
175+
node3.children_add([ &node6, &node7 ]);
176+
177+
let mut graph = Self::default();
178+
graph.nodes_add([ node0, node1, node2, node3, node4, node5, node6, node7 ]);
179+
180+
graph
181+
}
182+
160183
}

module/move/graphs_tools/tests/inc/tree_print_test.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@ use graph::map_of_nodes::
1111
fn _print_as_tree()
1212
{
1313
use the_module::tree_print::GraphDirectedPrintAsTree;
14-
let graph = Graph::duplet();
14+
let graph = Graph::duplet_assymetric();
1515

1616
let mut got = String::new();
17-
let r = graph._print_as_tree( &mut got, 1.into() );
17+
let r = graph._print_as_tree( &mut got, 0.into() );
1818
let exp = "node::1";
1919
assert_eq!( got, exp );
2020
assert!( r.is_ok() );
2121

2222
}
23+
24+
//
25+
26+
#[ test ]
27+
fn print_as_tree()
28+
{
29+
use the_module::tree_print::GraphDirectedPrintAsTree;
30+
let graph = Graph::triplet_with_double_legs();
31+
32+
let got = graph.print_as_tree( 0.into() );
33+
let exp = "node::1";
34+
println!( "{}", got );
35+
assert_eq!( got, exp );
36+
37+
}

0 commit comments

Comments
 (0)