Visualize a tree: Difference between revisions

→‎Tcl: Added implementation
(→‎Tcl: Added implementation)
Line 309:
8)
>>> </lang>
 
=={{header|Tcl}}==
{{tcllib|struct::tree}}
<lang tcl>package require struct::tree
 
proc visualize_tree {tree {nameattr name}} {
set path {}
$tree walk [$tree rootname] -order both {mode node} {
if {$mode eq "enter"} {
set s ""
foreach p $path {
append s [expr {[$tree next $p] eq "" ? " " : "\u2502 "}]
}
lappend path $node
append s [expr {
[$tree next $node] eq "" ? "\u2514\u2500" : "\u251c\u2500"
}]
if {[$tree keyexists $node $nameattr]} {
set name [$tree get $node $nameattr]
} else {
# No node name attribute; use the raw name
set name $node
}
puts "$s$name"
} else {
set path [lrange $path 0 end-1]
}
}
}</lang>
Demonstrating:
<lang tcl># Sample tree to demonstrate with
struct::tree t deserialize {root {} {} a 0 {} d 3 {} e 3 {} f 9 {} b 0 {} c 0 {}}
visualize_tree t</lang>
{{out}}
<pre>
└─root
├─a
│ ├─d
│ └─e
│ └─f
├─b
└─c
</pre>
Anonymous user