Visualize a tree: Difference between revisions

m
(→‎JS Plain text: Updated the decorated-outline version with an option to prune out nodeless lines)
 
(43 intermediate revisions by 19 users not shown)
Line 22:
=={{header|11l}}==
{{trans|D}}
<langsyntaxhighlight lang="11l">T Node
String value
Node? left
Line 38:
 
V tree = Node(1, Node(2, Node(4, Node(7)), Node(5)), Node(3, Node(6, Node(8), Node(9))))
print(tree.tree_indent().join("\n"))</langsyntaxhighlight>
 
=={{header|Ada}}==
Prints a tree of the current directory.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Directories;
 
procedure Directory_Tree is
Line 77:
begin
Print_Tree(Ada.Directories.Current_Directory);
end Directory_Tree;</langsyntaxhighlight>
 
{{out}}
Line 90:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># outputs nested html tables to visualise a tree #
 
# mode representing nodes of the tree #
Line 192:
animals /:= fish +:= reptiles +:= mammals;
 
print( ( TOHTML animals ) )</langsyntaxhighlight>
{{out}}
<table border="1" cellspacing="4">
Line 268:
</tr>
</table>
 
=={{header|AppleScript}}==
 
Using UTF8 box-drawing characters in a monospaced font, with options for (1.) compacted vs vertically centered display, and (2.) retaining or pruning out nodeless lines of text.
 
{{Trans|Python}}
{{Trans|JavaScript}}
<syntaxhighlight lang="applescript">-- Vertically centered textual tree using UTF8 monospaced
-- box-drawing characters, with options for compacting
-- and pruning.
 
-- ┌── Gamma
-- ┌─ Beta ┼── Delta
-- │ └ Epsilon
-- Alpha ┼─ Zeta ───── Eta
-- │ ┌─── Iota
-- └ Theta ┼── Kappa
-- └─ Lambda
 
-- TESTS --------------------------------------------------
on run
set tree to Node(1, ¬
{Node(2, ¬
{Node(4, {Node(7, {})}), ¬
Node(5, {})}), ¬
Node(3, ¬
{Node(6, ¬
{Node(8, {}), Node(9, {})})})})
set tree2 to Node("Alpha", ¬
{Node("Beta", ¬
{Node("Gamma", {}), ¬
Node("Delta", {}), ¬
Node("Epsilon", {})}), ¬
Node("Zeta", {Node("Eta", {})}), ¬
Node("Theta", ¬
{Node("Iota", {}), Node("Kappa", {}), ¬
Node("Lambda", {})})})
set strTrees to unlines({"(NB – view in mono-spaced font)\n\n", ¬
"Compacted (not all parents vertically centered):\n", ¬
drawTree2(true, false, tree), ¬
"\nFully expanded and vertically centered:\n", ¬
drawTree2(false, false, tree2), ¬
"\nVertically centered, with nodeless lines pruned out:\n", ¬
drawTree2(false, true, tree2)})
set the clipboard to strTrees
strTrees
end run
 
 
-- drawTree2 :: Bool -> Bool -> Tree String -> String
on drawTree2(blnCompressed, blnPruned, tree)
-- Tree design and algorithm inspired by the Haskell snippet at:
-- https://doisinkidney.com/snippets/drawing-trees.html
script measured
on |λ|(t)
script go
on |λ|(x)
set s to " " & x & " "
Tuple(length of s, s)
end |λ|
end script
fmapTree(go, t)
end |λ|
end script
set measuredTree to |λ|(tree) of measured
script levelMax
on |λ|(a, level)
a & maximum(map(my fst, level))
end |λ|
end script
set levelWidths to foldl(levelMax, {}, ¬
init(levels(measuredTree)))
-- Lefts, Mid, Rights
script lmrFromStrings
on |λ|(xs)
set {ls, rs} to items 2 thru -2 of ¬
(splitAt((length of xs) div 2, xs) as list)
Tuple3(ls, item 1 of rs, rest of rs)
end |λ|
end script
script stringsFromLMR
on |λ|(lmr)
script add
on |λ|(a, x)
a & x
end |λ|
end script
foldl(add, {}, items 2 thru -2 of (lmr as list))
end |λ|
end script
script fghOverLMR
on |λ|(f, g, h)
script
property mg : mReturn(g)
on |λ|(lmr)
set {ls, m, rs} to items 2 thru -2 of (lmr as list)
Tuple3(map(f, ls), |λ|(m) of mg, map(h, rs))
end |λ|
end script
end |λ|
end script
script lmrBuild
on leftPad(n)
script
on |λ|(s)
replicateString(n, space) & s
end |λ|
end script
end leftPad
-- lmrBuild main
on |λ|(w, f)
script
property mf : mReturn(f)
on |λ|(wsTree)
set xs to nest of wsTree
set lng to length of xs
set {nChars, x} to items 2 thru -2 of ¬
((root of wsTree) as list)
set _x to replicateString(w - nChars, "─") & x
-- LEAF NODE ------------------------------------
if 0 = lng then
Tuple3({}, _x, {})
else if 1 = lng then
-- NODE WITH SINGLE CHILD ---------------------
set indented to leftPad(1 + w)
script lineLinked
on |λ|(z)
_x & "─" & z
end |λ|
end script
|λ|(|λ|(item 1 of xs) of mf) of ¬
(|λ|(indented, lineLinked, indented) of ¬
fghOverLMR)
else
-- NODE WITH CHILDREN -------------------------
script treeFix
on cFix(x)
script
on |λ|(xs)
x & xs
end |λ|
end script
end cFix
on |λ|(l, m, r)
compose(stringsFromLMR, ¬
|λ|(cFix(l), cFix(m), cFix(r)) of ¬
fghOverLMR)
end |λ|
end script
script linked
on |λ|(s)
set c to text 1 of s
set t to tail(s)
if "┌" = c then
_x & "┬" & t
else if "│" = c then
_x & "┤" & t
else if "├" = c then
_x & "┼" & t
else
_x & "┴" & t
end if
end |λ|
end script
set indented to leftPad(w)
set lmrs to map(f, xs)
if blnCompressed then
set sep to {}
else
set sep to {"│"}
end if
tell lmrFromStrings
set tupleLMR to |λ|(intercalate(sep, ¬
{|λ|(item 1 of lmrs) of ¬
(|λ|(" ", "┌", "│") of treeFix)} & ¬
map(|λ|("│", "├", "│") of treeFix, ¬
init(tail(lmrs))) & ¬
{|λ|(item -1 of lmrs) of ¬
(|λ|("│", "└", " ") of treeFix)}))
end tell
|λ|(tupleLMR) of ¬
(|λ|(indented, linked, indented) of fghOverLMR)
end if
end |λ|
end script
end |λ|
end script
set treeLines to |λ|(|λ|(measuredTree) of ¬
foldr(lmrBuild, 0, levelWidths)) of stringsFromLMR
if blnPruned then
script notEmpty
on |λ|(s)
script isData
on |λ|(c)
"│ " does not contain c
end |λ|
end script
any(isData, characters of s)
end |λ|
end script
set xs to filter(notEmpty, treeLines)
else
set xs to treeLines
end if
unlines(xs)
end drawTree2
 
 
-- GENERIC ------------------------------------------------
 
-- Node :: a -> [Tree a] -> Tree a
on Node(v, xs)
{type:"Node", root:v, nest:xs}
end Node
 
-- Tuple (,) :: a -> b -> (a, b)
on Tuple(a, b)
-- Constructor for a pair of values, possibly of two different types.
{type:"Tuple", |1|:a, |2|:b, length:2}
end Tuple
 
-- Tuple3 (,,) :: a -> b -> c -> (a, b, c)
on Tuple3(x, y, z)
{type:"Tuple3", |1|:x, |2|:y, |3|:z, length:3}
end Tuple3
 
-- Applied to a predicate and a list,
-- |any| returns true if at least one element of the
-- list satisfies the predicate.
-- any :: (a -> Bool) -> [a] -> Bool
on any(f, xs)
tell mReturn(f)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return true
end repeat
false
end tell
end any
 
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
on compose(f, g)
script
property mf : mReturn(f)
property mg : mReturn(g)
on |λ|(x)
|λ|(|λ|(x) of mg) of mf
end |λ|
end script
end compose
 
-- concat :: [[a]] -> [a]
-- concat :: [String] -> String
on concat(xs)
set lng to length of xs
if 0 < lng and string is class of (item 1 of xs) then
set acc to ""
else
set acc to {}
end if
repeat with i from 1 to lng
set acc to acc & item i of xs
end repeat
acc
end concat
 
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set lng to length of xs
set acc to {}
tell mReturn(f)
repeat with i from 1 to lng
set acc to acc & (|λ|(item i of xs, i, xs))
end repeat
end tell
return acc
end concatMap
 
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
 
-- fmapTree :: (a -> b) -> Tree a -> Tree b
on fmapTree(f, tree)
script go
property g : |λ| of mReturn(f)
on |λ|(x)
set xs to nest of x
if xs ≠ {} then
set ys to map(go, xs)
else
set ys to xs
end if
Node(g(root of x), ys)
end |λ|
end script
|λ|(tree) of go
end fmapTree
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
-- foldr :: (a -> b -> b) -> b -> [a] -> b
on foldr(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to |λ|(item i of xs, v, i, xs)
end repeat
return v
end tell
end foldr
 
-- fst :: (a, b) -> a
on fst(tpl)
if class of tpl is record then
|1| of tpl
else
item 1 of tpl
end if
end fst
 
-- identity :: a -> a
on identity(x)
-- The argument unchanged.
x
end identity
 
-- init :: [a] -> [a]
-- init :: [String] -> [String]
on init(xs)
set blnString to class of xs = string
set lng to length of xs
if lng > 1 then
if blnString then
text 1 thru -2 of xs
else
items 1 thru -2 of xs
end if
else if lng > 0 then
if blnString then
""
else
{}
end if
else
missing value
end if
end init
 
-- intercalate :: [a] -> [[a]] -> [a]
-- intercalate :: String -> [String] -> String
on intercalate(sep, xs)
concat(intersperse(sep, xs))
end intercalate
 
-- intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3]
-- intersperse :: a -> [a] -> [a]
-- intersperse :: Char -> String -> String
on intersperse(sep, xs)
set lng to length of xs
if lng > 1 then
set acc to {item 1 of xs}
repeat with i from 2 to lng
set acc to acc & {sep, item i of xs}
end repeat
if class of xs is string then
concat(acc)
else
acc
end if
else
xs
end if
end intersperse
 
-- isNull :: [a] -> Bool
-- isNull :: String -> Bool
on isNull(xs)
if class of xs is string then
"" = xs
else
{} = xs
end if
end isNull
 
-- iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
on iterateUntil(p, f, x)
script
property mp : mReturn(p)'s |λ|
property mf : mReturn(f)'s |λ|
property lst : {x}
on |λ|(v)
repeat until mp(v)
set v to mf(v)
set end of lst to v
end repeat
return lst
end |λ|
end script
|λ|(x) of result
end iterateUntil
 
-- levels :: Tree a -> [[a]]
on levels(tree)
script nextLayer
on |λ|(xs)
script
on |λ|(x)
nest of x
end |λ|
end script
concatMap(result, xs)
end |λ|
end script
script roots
on |λ|(xs)
script
on |λ|(x)
root of x
end |λ|
end script
map(result, xs)
end |λ|
end script
map(roots, iterateUntil(my isNull, nextLayer, {tree}))
end levels
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- maximum :: Ord a => [a] -> a
on maximum(xs)
script
on |λ|(a, b)
if a is missing value or b > a then
b
else
a
end if
end |λ|
end script
foldl(result, missing value, xs)
end maximum
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
-- replicateString :: Int -> String -> String
on replicateString(n, s)
set out to ""
if n < 1 then return out
set dbl to s
repeat while (n > 1)
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicateString
 
-- snd :: (a, b) -> b
on snd(tpl)
if class of tpl is record then
|2| of tpl
else
item 2 of tpl
end if
end snd
 
-- splitAt :: Int -> [a] -> ([a], [a])
on splitAt(n, xs)
if n > 0 and n < length of xs then
if class of xs is text then
Tuple(items 1 thru n of xs as text, items (n + 1) thru -1 of xs as text)
else
Tuple(items 1 thru n of xs, items (n + 1) thru -1 of xs)
end if
else
if n < 1 then
Tuple({}, xs)
else
Tuple(xs, {})
end if
end if
end splitAt
 
-- tail :: [a] -> [a]
on tail(xs)
set blnText to text is class of xs
if blnText then
set unit to ""
else
set unit to {}
end if
set lng to length of xs
if 1 > lng then
missing value
else if 2 > lng then
unit
else
if blnText then
text 2 thru -1 of xs
else
rest of xs
end if
end if
end tail
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set str to xs as text
set my text item delimiters to dlm
str
end unlines</syntaxhighlight>
{{Out}}
<pre>(NB – view in mono-spaced font)
 
 
Compacted (not all parents vertically centered):
 
┌ 4 ─ 7
┌ 2 ┴ 5
1 ┤ ┌ 8
└ 3 ─ 6 ┴ 9
 
Fully expanded and vertically centered:
 
┌── Gamma
┌─ Beta ┼── Delta
│ │
│ └ Epsilon
Alpha ┼─ Zeta ───── Eta
│ ┌─── Iota
│ │
└ Theta ┼── Kappa
└─ Lambda
 
Vertically centered, with nodeless lines pruned out:
 
┌── Gamma
┌─ Beta ┼── Delta
│ └ Epsilon
Alpha ┼─ Zeta ───── Eta
│ ┌─── Iota
└ Theta ┼── Kappa
└─ Lambda </pre>
 
=={{header|Batch File}}==
PrintsDisplays a tree of the current directory.
<syntaxhighlight lang="batch dosfile">@tree %cd%</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
This creates a native Windows Tree View control:
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"WINLIB5"
ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 0 : QUIT
Line 330 ⟶ 944:
IF hnode% = 0 ERROR 100, "TVM_INSERTITEM failed"
SYS "InvalidateRect", hTree%, 0, 0
= hnode%</langsyntaxhighlight>
[[File:visualize_tree_bbc.gif]]
 
=={{header|C}}==
Print a simple tree to standard output:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 379 ⟶ 993:
tree(n, 0);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 399 ⟶ 1,013:
`--1
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <functional>
#include <iostream>
#include <random>
 
class BinarySearchTree {
private:
struct Node {
int key;
Node *left, *right;
 
Node(int k) : key(k), left(nullptr), right(nullptr) {
//empty
}
} *root;
 
public:
BinarySearchTree() : root(nullptr) {
// empty
}
 
bool insert(int key) {
if (root == nullptr) {
root = new Node(key);
} else {
auto n = root;
Node *parent;
while (true) {
if (n->key == key) {
return false;
}
 
parent = n;
 
bool goLeft = key < n->key;
n = goLeft ? n->left : n->right;
 
if (n == nullptr) {
if (goLeft) {
parent->left = new Node(key);
} else {
parent->right = new Node(key);
}
break;
}
}
}
return true;
}
 
friend std::ostream &operator<<(std::ostream &, const BinarySearchTree &);
};
 
template<typename T>
void display(std::ostream &os, const T *n) {
if (n != nullptr) {
os << "Node(";
 
display(os, n->left);
 
os << ',' << n->key << ',';
 
display(os, n->right);
 
os << ")";
} else {
os << '-';
}
}
 
std::ostream &operator<<(std::ostream &os, const BinarySearchTree &bst) {
display(os, bst.root);
return os;
}
 
int main() {
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, 200);
auto rng = std::bind(distribution, generator);
 
BinarySearchTree tree;
 
tree.insert(100);
for (size_t i = 0; i < 20; i++) {
tree.insert(rng());
}
std::cout << tree << '\n';
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>Node(Node(Node(Node(Node(-,4,-),17,Node(-,22,-)),28,-),30,Node(Node(-,36,-),48,Node(Node(Node(-,77,-),81,-),91,-))),100,Node(Node(Node(Node(-,117,-),125,Node(Node(-,131,-),151,Node(-,158,Node(-,163,-)))),176,Node(Node(-,192,-),193,-)),200,-))</pre>
 
=={{header|C sharp}}==
<syntaxhighlight lang="csharp">using System;
 
public static class VisualizeTree
{
public static void Main() {
"A".t(
"B0".t(
"C1",
"C2".t(
"D".t("E1", "E2", "E3")),
"C3".t(
"F1",
"F2",
"F3".t("G"),
"F4".t("H1", "H2"))),
"B1".t(
"K1",
"K2".t(
"L1".t("M"),
"L2",
"L3"),
"K3")
).Print();
}
 
private static Tree t(this string value, params Tree[] children) => new Tree(value, children);
 
private static void Print(this Tree tree) => tree.Print(true, "");
 
private static void Print(this Tree tree, bool last, string prefix) {
(string current, string next) = last
? (prefix + "└─" + tree.Value, prefix + " ")
: (prefix + "├─" + tree.Value, prefix + "| ");
Console.WriteLine(current[2..]);
for (int c = 0; c < tree.Children.Length; c++) {
tree.Children[c].Print(c == tree.Children.Length - 1, next);
}
}
 
class Tree
{
public Tree(string value, params Tree[] children) => (Value, Children) = (value, children);
public static implicit operator Tree(string value) => new Tree(value);
public string Value { get; }
public Tree[] Children { get; }
}
}</syntaxhighlight>
{{out}}
<pre>
A
├─B0
| ├─C1
| ├─C2
| | └─D
| | ├─E1
| | ├─E2
| | └─E3
| └─C3
| ├─F1
| ├─F2
| ├─F3
| | └─G
| └─F4
| ├─H1
| └─H2
└─B1
├─K1
├─K2
| ├─L1
| | └─M
| ├─L2
| └─L3
└─K3</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(use 'vijual)
 
(draw-tree [[:A] [:B] [:C [:D [:E] [:F]] [:G]]])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 425 ⟶ 1,208:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun visualize (tree)
(labels
((rprint (list)
Line 456 ⟶ 1,239:
branches))
(terpri)))))))
(vis-h tree '("| "))))</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="lisp">CL-USER> (visualize '(a b c ((d (e ((() ()))) f)) (g)))
A
|
Line 489 ⟶ 1,272:
G
NIL</langsyntaxhighlight>
or
<syntaxhighlight lang="lisp">(use-package :iterate)
(defun print-tree (tree value-function children-function)
(labels
((do-print-tree (tree prefix)
(format t "~a~%" (funcall value-function tree))
(iter
(with children = (funcall children-function tree))
(for child = (pop children))
(while child)
(if children
(progn (format t "~a├─ " prefix)
(do-print-tree child (format nil "~a│ " prefix)))
(progn (format t "~a└─ " prefix)
(do-print-tree child (format nil "~a " prefix)))))))
(do-print-tree tree "")))
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="lisp">CL-USER>(print-tree '(a
(aa
(aaa
(aaaa)
(aaab
(aaaba)
(aaabb))
(aaac)))
(ab)
(ac
(aca)
(acb)
(acc)))
#'car #'cdr)
A
├─ AA
│ └─ AAA
│ ├─ AAAA
│ ├─ AAAB
│ │ ├─ AAABA
│ │ └─ AAABB
│ └─ AAAC
├─ AB
└─ AC
├─ ACA
├─ ACB
└─ ACC
</syntaxhighlight>
 
=={{header|D}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="d">import std.stdio, std.conv, std.algorithm, std.array;
 
struct Node(T) { T value; Node* left, right; }
Line 512 ⟶ 1,342:
const tree = N(1, N(2, N(4, N(7)), N(5)), N(3, N(6, N(8), N(9))));
writefln("%-(%s\n%)", tree.treeIndent);
}</langsyntaxhighlight>
{{out}}
<pre>--1
Line 533 ⟶ 1,363:
| `-- (null)
`-- (null)</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Visualize_a_tree;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
type
TNode = record
_label: string;
children: TArray<Integer>;
end;
 
TTree = TArray<TNode>;
 
TTreeHelper = record helper for TTree
procedure AddNode(lb: string; chl: TArray<Integer> = []);
end;
 
procedure Vis(t: TTree);
 
procedure f(n: Integer; pre: string);
begin
var ch := t[n].children;
if Length(ch) = 0 then
begin
Writeln('-', t[n]._label);
exit;
end;
 
writeln('+', t[n]._label);
var last := Length(ch) - 1;
for var c in copy(ch, 0, last) do
begin
write(pre, '+-');
f(c, pre + '¦ ');
end;
write(pre, '+-');
f(ch[last], pre + ' ');
end;
 
begin
if Length(t) = 0 then
begin
writeln('<empty>');
exit;
end;
 
f(0, '');
end;
 
{ TTreeHelper }
 
procedure TTreeHelper.AddNode(lb: string; chl: TArray<Integer> = []);
begin
SetLength(self, Length(self) + 1);
with self[High(self)] do
begin
_label := lb;
if Length(chl) > 0 then
children := copy(chl, 0, length(chl));
end;
end;
 
var
Tree: TTree;
 
begin
Tree.AddNode('root', [1, 2, 3]);
Tree.AddNode('ei', [4, 5]);
Tree.AddNode('bee');
Tree.AddNode('si');
Tree.AddNode('dee');
Tree.AddNode('y', [6]);
Tree.AddNode('eff');
 
Vis(Tree);
 
{$IFNDEF UNIX} readln; {$ENDIF}
end.</syntaxhighlight>
{{out}}
<pre>┐root
├─┐ei
│ ├──dee
│ └─┐y
│ └──eff
├──bee
└──si</pre>
 
=={{header|Elena}}==
ELENA 45.x0 :
<langsyntaxhighlight lang="elena">import system'routines;
import extensions;
 
Line 598 ⟶ 1,522:
console.writeTree(tree).readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 610 ⟶ 1,534:
| +---d
|
+---be
</pre>
 
Line 628 ⟶ 1,552:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">type tree =
| T of string * tree list
 
Line 658 ⟶ 1,582:
 
visualize example prefNone
|> Seq.iter (printfn "%s")</langsyntaxhighlight>
{{out}}
<pre>root
Line 670 ⟶ 1,594:
=={{header|Factor}}==
Factor's prettyprinter does this by default with any nested sequences and/or tuples. There are dynamic variables that can be altered to change the prettyprinter's default behavior. The most interesting are <code>tab-size</code> and <code>margin</code> for customizing the look of a tree. For smaller trees, it's best to change <code>margin</code> from its default of <code>64</code> to something low, perhaps <code>10</code>.
<langsyntaxhighlight lang="factor">USE: literals
 
CONSTANT: mammals { "mammals" { "deer" "gorilla" "dolphin" } }
CONSTANT: reptiles { "reptiles" { "turtle" "lizard" "snake" } }
 
{ "animals" ${ mammals reptiles } } dup . 10 margin set .</langsyntaxhighlight>
{{out}}
<pre>
Line 708 ⟶ 1,632:
</pre>
An example showcasing tuples by displaying an AVL tree:
<langsyntaxhighlight lang="factor">USE: trees.avl
AVL{ { 1 2 } { 9 19 } { 3 4 } { 5 6 } } .</langsyntaxhighlight>
{{out}}
<pre>
Line 747 ⟶ 1,671:
=={{header|Fōrmulæ}}==
 
In [http{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Visualize_a_tree this] page you can see the solution of this task.}}
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
Fōrmulæ is a homoiconic language using expression trees as its fundamental structure, so managing trees is very natural.
 
'''Creating trees from expressions'''
 
The ToTree expression creates a tree from a given expression. see the following examples:
 
[[File:Fōrmulæ - Visualize a tree 01.png]]
 
[[File:Fōrmulæ - Visualize a tree 02.png]]
 
Be aware the the expression will be first reduced, like the following:
 
[[File:Fōrmulæ - Visualize a tree 03.png]]
 
[[File:Fōrmulæ - Visualize a tree 04.png]]
 
It order to avoid the reduction, the use of the Protect expression is recommended:
 
[[File:Fōrmulæ - Visualize a tree 05.png]]
 
[[File:Fōrmulæ - Visualize a tree 06.png]]
 
In the following example, it is shown that a matrix is a list containing (same cardinality) sublists:
 
[[File:Fōrmulæ - Visualize a tree 07.png]]
 
[[File:Fōrmulæ - Visualize a tree 08.png]]
 
'''Creating trees programatically'''
 
The tree, like any other expression in Fōrmulæ is a first-class citizen of the language. Fōrmulæ also supports high-order functions, so trees can be set as parameters of functions, or retrieved from functions.
 
The following example shows a function that produces a Fibonacci tree, a tree that shows the calls of a function that recursively calculates Fibonacci numbers:
 
[[File:Fōrmulæ - Fibonacci sequence 03.png]]
 
[[File:Fōrmulæ - Fibonacci sequence 04.png]]
 
[[File:Fōrmulæ - Fibonacci sequence 05.png]]
 
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<syntaxhighlight lang="freebasic">Dim Shared As Ubyte colores(4) => {7,13,14,3,2}
 
Sub showTree(n As Integer, A As String)
Dim As Integer i, co = 0, b = 1, col
Dim As String cs = Left(A, 1)
If cs = "" Then Exit Sub
Select Case cs
Case "["
co += 1 : showTree(n + 1, Right(A, Len(A) - 1))
Exit Select
Case "]"
co -= 1 : showTree(n - 1, Right(A, Len(A) - 1))
Exit Select
Case Else
For i = 2 To n
Print " ";
co = n
Next i
Color colores(co) : Print !"\&hc0-"; cs
showTree(n, Right(A, Len(A) - 1))
Exit Select
End Select
End Sub
 
Cls
showTree(0, "[1[2[3][4[5][6]][7]][8[9]]]")
Print !"\n\n\n"
showTree(0, "[1[2[3[4]]][5[6][7[8][9]]]]")
Sleep</syntaxhighlight>
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
===JSON===
Not the most economical output, but at least json.MarshalIndent is in the Go standard library. Note that the definition of Node has nothing JSON specific about it; it's an ordinary struct.
<langsyntaxhighlight Golang="go">package main
 
import (
Line 784 ⟶ 1,781:
}
fmt.Println(string(b))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 821 ⟶ 1,818:
===TOML===
It works in this case, but TOML wasn't really designed for this and encoders may have trouble with general trees. Empty trees and nils for example might be problematic depending on your data structures and limitations of your TOML encoder. YMMV.
<langsyntaxhighlight lang="go">package main
 
import (
Line 851 ⟶ 1,848:
log.Fatal(err)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 879 ⟶ 1,876:
===Unicode===
A non-library solution, more like a number of other solutions on this page, and with more compact output. The tree representation here uses integer indexes rather than pointers, which is efficient for representation and computation. A serialization format like JSON or TOML wouldn't see it as a hierarchical structure, but the code here knows to interpret the child ints as node indexes.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 924 ⟶ 1,921:
}
f(0, "")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 938 ⟶ 1,935:
=={{header|Haskell}}==
Tree borrowed from [[Tree traversal]]:
<langsyntaxhighlight lang="haskell">data Tree a = Empty | Node { value :: a, left :: Tree a, right :: Tree a }
deriving (Show, Eq)
 
Line 952 ⟶ 1,949:
ls = treeIndent$left t
 
main = mapM_ putStrLn $ treeIndent tree</langsyntaxhighlight>
{{out}}
<pre>
Line 980 ⟶ 1,977:
We can ''fmap show'' over our tree of integers to derive a tree of strings, and apply `drawTree` to that.
 
<langsyntaxhighlight lang="haskell">import Data.Tree (Tree(..), drawTree)
 
tree :: Tree Int
Line 991 ⟶ 1,988:
 
main :: IO ()
main = (putStrLn . drawTree . fmap show) tree</langsyntaxhighlight>
{{Out}}
<pre>1
Line 1,014 ⟶ 2,011:
 
The following works in both languages.
<langsyntaxhighlight lang="unicon">procedure main(A)
showTree("", " -", [1, [2,[3],[4,[5],[6]],[7,[11]]], [8,[9,[10]]] ])
write()
Line 1,027 ⟶ 2,024:
showTree(prefix, "`-", A[*A])
}
end</langsyntaxhighlight>
 
Output:
Line 1,063 ⟶ 2,060:
Or, adapted to the [[Tree_traversal#J:_Alternate_implementation|parent index]] representation of a tree (which allows different nodes to share labels and may also be more convenient for other reasons):
 
<langsyntaxhighlight Jlang="j">BOXC=: 9!:6 '' NB. box drawing characters
EW =: {: BOXC NB. east-west
 
Line 1,106 ⟶ 2,103:
 
extend=: 3 : '(+./\"1 (y=EW) *. *./\."1 y e.'' '',EW)}y,:EW'
</syntaxhighlight>
</lang>
 
Example use:
 
<langsyntaxhighlight lang="j"> (i.10) showtree _,}.p:inv i.10
┌─ 6
┌─ 1 ─── 3 ─┴─ 7
│ ┌─ 8
─ 0 ─┤ ┌─ 4 ─┴─ 9
└─ 2 ─┴─ 5 </langsyntaxhighlight>
 
For comparison purposes, here's the node labels along with (in the following row) the index of their parent node:
 
<syntaxhighlight lang="j"> (i.10),: _,}.p:inv i.10
0 1 2 3 4 5 6 7 8 9
_ 0 0 1 2 2 3 3 4 4</syntaxhighlight>
 
Also, note that labels can be arbitrary. Here, we used numeric labels because they were concise and convenient. But, for example, we can replace the label '0' with 'george' and the label '4' with 'fred':
 
<syntaxhighlight lang="j"> ((<'george') 0} (<'fred') 4} ":each i.10)showtree _,}.p:inv i.10
┌─ 6
┌─ 1 ─── 3 ────┴─ 7
│ ┌─ 8
─ george ─┤ ┌─ fred ─┴─ 9
└─ 2 ─┴─ 5 </syntaxhighlight>
 
=={{header|Java}}==
Minimalist BST that can do nothing except print itself to stdout.
<langsyntaxhighlight lang="java">public class VisualizeTree {
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
Line 1,197 ⟶ 2,209:
}
}
}</langsyntaxhighlight>
 
<pre> 100
Line 1,212 ⟶ 2,224:
===HTML===
Javascript wrapped in HTML5 document. ''Should'' work in modern browsers.
<langsyntaxhighlight lang="html"><!doctype html>
<html id="doc">
<head><meta charset="utf-8"/>
Line 1,294 ⟶ 2,306:
<div id="tree"><a href="javascript:dom_tree('doc')">click me</a></div>
</body>
</html></langsyntaxhighlight>
 
===Plain text===
====Vertically centered tree====
{{Trans|Python}} (Functional version)
<syntaxhighlight lang="javascript">(() => {
'use strict';
 
// UTF8 character-drawn tree, with options for compacting vs
// centering parents, and for pruning out nodeless lines.
 
const example = `
┌ Epsilon
┌─ Beta ┼─── Zeta
│ └──── Eta
Alpha ┼ Gamma ─── Theta
│ ┌─── Iota
└ Delta ┼── Kappa
└─ Lambda`
 
// drawTree2 :: Bool -> Bool -> Tree String -> String
const drawTree2 = blnCompact => blnPruned => tree => {
// Tree design and algorithm inspired by the Haskell snippet at:
// https://doisinkidney.com/snippets/drawing-trees.html
const
// Lefts, Middle, Rights
lmrFromStrings = xs => {
const [ls, rs] = Array.from(splitAt(
Math.floor(xs.length / 2),
xs
));
return Tuple3(ls, rs[0], rs.slice(1));
},
stringsFromLMR = lmr =>
Array.from(lmr).reduce((a, x) => a.concat(x), []),
fghOverLMR = (f, g, h) => lmr => {
const [ls, m, rs] = Array.from(lmr);
return Tuple3(ls.map(f), g(m), rs.map(h));
};
 
const lmrBuild = (f, w) => wsTree => {
const
leftPad = n => s => ' '.repeat(n) + s,
xs = wsTree.nest,
lng = xs.length,
[nChars, x] = Array.from(wsTree.root);
 
// LEAF NODE --------------------------------------
return 0 === lng ? (
Tuple3([], '─'.repeat(w - nChars) + x, [])
 
// NODE WITH SINGLE CHILD -------------------------
) : 1 === lng ? (() => {
const indented = leftPad(1 + w);
return fghOverLMR(
indented,
z => '─'.repeat(w - nChars) + x + '─' + z,
indented
)(f(xs[0]));
 
// NODE WITH CHILDREN -----------------------------
})() : (() => {
const
cFix = x => xs => x + xs,
treeFix = (l, m, r) => compose(
stringsFromLMR,
fghOverLMR(cFix(l), cFix(m), cFix(r))
),
_x = '─'.repeat(w - nChars) + x,
indented = leftPad(w),
lmrs = xs.map(f);
return fghOverLMR(
indented,
s => _x + ({
'┌': '┬',
'├': '┼',
'│': '┤',
'└': '┴'
})[s[0]] + s.slice(1),
indented
)(lmrFromStrings(
intercalate(
blnCompact ? [] : ['│'],
[treeFix(' ', '┌', '│')(lmrs[0])]
.concat(init(lmrs.slice(1)).map(
treeFix('│', '├', '│')
))
.concat([treeFix('│', '└', ' ')(
lmrs[lmrs.length - 1]
)])
)
));
})();
};
const
measuredTree = fmapTree(
v => {
const s = ' ' + v + ' ';
return Tuple(s.length, s)
}, tree
),
levelWidths = init(levels(measuredTree))
.reduce(
(a, level) => a.concat(maximum(level.map(fst))),
[]
),
treeLines = stringsFromLMR(
levelWidths.reduceRight(
lmrBuild, x => x
)(measuredTree)
);
return unlines(
blnPruned ? (
treeLines.filter(
s => s.split('')
.some(c => !' │'.includes(c))
)
) : treeLines
);
};
 
// TESTS ----------------------------------------------
const main = () => {
 
// tree :: Tree String
const tree = Node(
'Alpha', [
Node('Beta', [
Node('Epsilon', []),
Node('Zeta', []),
Node('Eta', [])
]),
Node('Gamma', [Node('Theta', [])]),
Node('Delta', [
Node('Iota', []),
Node('Kappa', []),
Node('Lambda', [])
])
]);
 
// tree2 :: Tree Int
const tree2 = Node(
1,
[
Node(2, [
Node(4, []),
Node(5, [Node(7, [])])
]),
Node(3, [
Node(6, [
Node(8, []),
Node(9, [])
])
])
]
);
 
// strTrees :: String
const strTrees = ([
'Compacted (parents not all vertically centered):',
drawTree2(true)(false)(tree2),
'Fully expanded, with vertical centering:',
drawTree2(false)(false)(tree),
'Vertically centered, with nodeless lines pruned out:',
drawTree2(false)(true)(tree),
].join('\n\n'));
 
return (
console.log(strTrees),
strTrees
);
};
 
// GENERIC FUNCTIONS ----------------------------------
 
// Node :: a -> [Tree a] -> Tree a
const Node = (v, xs) => ({
type: 'Node',
root: v, // any type of value (consistent across tree)
nest: xs || []
});
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = (a, b) => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// Tuple3 (,,) :: a -> b -> c -> (a, b, c)
const Tuple3 = (a, b, c) => ({
type: 'Tuple3',
'0': a,
'1': b,
'2': c,
length: 3
});
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (f, g) => x => f(g(x));
 
// concat :: [[a]] -> [a]
// concat :: [String] -> String
const concat = xs =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs[0] ? (
[]
) : '';
return unit.concat.apply(unit, xs);
})() : [];
 
// fmapTree :: (a -> b) -> Tree a -> Tree b
const fmapTree = (f, tree) => {
const go = node => Node(
f(node.root),
node.nest.map(go)
);
return go(tree);
};
 
// fst :: (a, b) -> a
const fst = tpl => tpl[0];
 
// identity :: a -> a
const identity = x => x;
 
// init :: [a] -> [a]
const init = xs =>
0 < xs.length ? (
xs.slice(0, -1)
) : undefined;
 
// intercalate :: [a] -> [[a]] -> [a]
// intercalate :: String -> [String] -> String
const intercalate = (sep, xs) =>
0 < xs.length && 'string' === typeof sep &&
'string' === typeof xs[0] ? (
xs.join(sep)
) : concat(intersperse(sep, xs));
 
// intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3]
 
// intersperse :: a -> [a] -> [a]
// intersperse :: Char -> String -> String
const intersperse = (sep, xs) => {
const bln = 'string' === typeof xs;
return xs.length > 1 ? (
(bln ? concat : x => x)(
(bln ? (
xs.split('')
) : xs)
.slice(1)
.reduce((a, x) => a.concat([sep, x]), [xs[0]])
)) : xs;
};
 
// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
const iterateUntil = (p, f, x) => {
const vs = [x];
let h = x;
while (!p(h))(h = f(h), vs.push(h));
return vs;
};
 
// Returns Infinity over objects without finite length.
// This enables zip and zipWith to choose the shorter
// argument when one is non-finite, like cycle, repeat etc
 
// length :: [a] -> Int
const length = xs =>
(Array.isArray(xs) || 'string' === typeof xs) ? (
xs.length
) : Infinity;
 
// levels :: Tree a -> [[a]]
const levels = tree =>
iterateUntil(
xs => 1 > xs.length,
ys => [].concat(...ys.map(nest)),
[tree]
).map(xs => xs.map(root));
 
// maximum :: Ord a => [a] -> a
const maximum = xs =>
0 < xs.length ? (
xs.slice(1).reduce((a, x) => x > a ? x : a, xs[0])
) : undefined;
 
// nest :: Tree a -> [a]
const nest = tree => tree.nest;
 
// root :: Tree a -> a
const root = tree => tree.root;
 
// splitAt :: Int -> [a] -> ([a], [a])
const splitAt = (n, xs) =>
Tuple(xs.slice(0, n), xs.slice(n));
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>Compacted (parents not all vertically centered):
 
┌ 4
┌ 2 ┴ 5 ─ 7
1 ┤ ┌ 8
└ 3 ─ 6 ┴ 9
 
Fully expanded, with vertical centering:
 
┌ Epsilon
┌─ Beta ┼─── Zeta
│ │
│ └──── Eta
Alpha ┼ Gamma ─── Theta
│ ┌─── Iota
│ │
└ Delta ┼── Kappa
└─ Lambda
 
Vertically centered, with nodeless lines pruned out:
 
┌ Epsilon
┌─ Beta ┼─── Zeta
│ └──── Eta
Alpha ┼ Gamma ─── Theta
│ ┌─── Iota
└ Delta ┼── Kappa
└─ Lambda</pre>
 
====Decorated outline====
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,366 ⟶ 2,714:
};
 
// GENERIC FUNCTIONS ----------------------------------
 
Line 1,428 ⟶ 2,775:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Alpha
Line 1,463 ⟶ 2,810:
└─ Lambda</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
In this entry, a tree $t is represented by an array [root, child1,
child2 ...] where root must be present and cannot be an array, and
the children, which may also be trees, may all be absent.
 
The tree [0, 1, 2, 3] is displayed as:
<pre>
0
├─ 1
├─ 2
└─ 3
</pre>
 
<syntaxhighlight lang="jq"># Input: an array representing a tree
# Output: a stream of strings representing the tree
# In this implementation, empty arrays in the tree are simply ignored.
def printTree:
 
def tidy:
sub("└─$"; " ")
| sub("├─$"; "| ") ;
 
# Input: a string prefix
def print($tree):
if $tree|type != "array" then . + ($tree|tostring)
else # ignore empty arrays
($tree | map(select(if type == "array" then length>0 else true end))) as $tree
| if $tree|length == 0 then empty
elif $tree|length == 1
then print($tree[0])
else print($tree[0]),
($tree[1:] as $children
| tidy as $p
| ($p + ( "├─" | print($children[:-1][]))),
($p + ( "└─" | print($children[-1]))) )
end
end ;
 
. as $tree
| "" | print($tree) ;</syntaxhighlight>
'''Examples'''
<syntaxhighlight lang="jq">def bigTree:
["a",
["aa",
["aaa",
["aaaa"],
["aaab",
["aaaba"],
["aaabb"]],
["aaac"]]],
["ab"],
["ac",
["aca"],
["acb"],
["acc"]]] ;
 
[0, 1, 2, 3],
[1,[2,3,[4, 5, [6,7,8], 9, [10,11]]]],
bigTree
| ("Tree with array representation:\n\(.)\n",
printTree,
"")</syntaxhighlight>
{{out}}
<pre>
Tree with array representation:
[0,1,2,3]
 
0
├─1
├─2
└─3
 
Tree with array representation:
[1,[2,3,[4,5,[6,7,8],9,[10,11]]]]
 
1
└─2
├─3
└─4
├─5
├─6
| ├─7
| └─8
├─9
└─10
└─11
 
Tree with array representation:
["a",["aa",["aaa",["aaaa"],["aaab",["aaaba"],["aaabb"]],["aaac"]]],["ab"],["ac",["aca"],["acb"],["acc"]]]
 
a
├─aa
| └─aaa
| ├─aaaa
| ├─aaab
| | ├─aaaba
| | └─aaabb
| └─aaac
├─ab
└─ac
├─aca
├─acb
└─acc
</pre>
 
=={{header|Julia}}==
Run from Julia REPL.
<langsyntaxhighlight Julialang="julia">using Gadfly, LightGraphs, GraphPlot
 
gx = kronecker(5, 12, 0.57, 0.19, 0.19)
gplot(gx)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import java.util.Random
Line 1,518 ⟶ 2,973:
val n = 8
tree(n, null)
}</langsyntaxhighlight>
 
Sample output (unlike the C entry, should be different each time it's run):
Line 1,541 ⟶ 2,996:
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">-- parent script "TreeItem"
-- (minimal implementation with direct property access)
 
Line 1,565 ⟶ 3,020:
me.printTree(c, indent&" ")
end repeat
end</langsyntaxhighlight>
Usage:
<langsyntaxhighlight lang="lingo">-- create a tree
root = script("TreeItem").new("root")
a = script("TreeItem").new("a")
Line 1,583 ⟶ 3,038:
 
-- print the tree
root.printTree()</langsyntaxhighlight>
 
{{Out}}
Line 1,596 ⟶ 3,051:
</pre>
 
=={{header|MathematicaLua}}==
{{trans|C#}}
<syntaxhighlight lang="lua">function makeTree(v,ac)
if type(ac) == "table" then
return {value=v,children=ac}
else
return {value=v}
end
end
 
function printTree(t,last,prefix)
if last == nil then
printTree(t, false, '')
else
local current = ''
local next = ''
 
if last then
current = prefix .. '\\-' .. t.value
next = prefix .. ' '
else
current = prefix .. '|-' .. t.value
next = prefix .. '| '
end
 
print(current:sub(3))
if t.children ~= nil then
for k,v in pairs(t.children) do
printTree(v, k == #t.children, next)
end
end
end
end
 
printTree(
makeTree('A', {
makeTree('B0', {
makeTree('C1'),
makeTree('C2', {
makeTree('D', {
makeTree('E1'),
makeTree('E2'),
makeTree('E3')
})
}),
makeTree('C3', {
makeTree('F1'),
makeTree('F2'),
makeTree('F3', {makeTree('G')}),
makeTree('F4', {
makeTree('H1'),
makeTree('H2')
})
})
}),
makeTree('B1',{
makeTree('K1'),
makeTree('K2', {
makeTree('L1', {makeTree('M')}),
makeTree('L2'),
makeTree('L3')
}),
makeTree('K3')
})
})
)</syntaxhighlight>
{{out}}
<pre>A
|-B0
| |-C1
| |-C2
| | \-D
| | |-E1
| | |-E2
| | \-E3
| \-C3
| |-F1
| |-F2
| |-F3
| | \-G
| \-F4
| |-H1
| \-H2
\-B1
|-K1
|-K2
| |-L1
| | \-M
| |-L2
| \-L3
\-K3</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">T := GraphTheory:-Graph([1, 2, 3, 4, 5], {{1, 2}, {2, 3}, {2, 4}, {4, 5}}):
GraphTheory:-DrawGraph(T, style = tree);</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
=== Tree graph ===
Make a tree graph. In Mathematica, '''\[DirectedEdge]''' will appear as an arrow in the code.
 
<langsyntaxhighlight Mathematicalang="mathematica">edges = {1 \[DirectedEdge] 2, 1 \[DirectedEdge] 3, 2 \[DirectedEdge] 4, 2 \[DirectedEdge] 5,
3 \[DirectedEdge] 6, 4 \[DirectedEdge] 7};
t = TreeGraph[edges, GraphStyle -> "VintageDiagram"]</langsyntaxhighlight>
 
[[File:Tree.jpg]]
Line 1,609 ⟶ 3,159:
Show the syntactical structure of the above code. '''Defer''' is added to impede '''TreeGraph''' from becoming a graphical object.
 
<langsyntaxhighlight Mathematicalang="mathematica">TreeForm[Defer@
TreeGraph[{1 \[DirectedEdge] 2, 1 \[DirectedEdge] 3, 2 \[DirectedEdge] 4, 2 \[DirectedEdge] 5,
3 \[DirectedEdge] 6, 4 \[DirectedEdge] 7}, VertexLabels -> "Name"]]</langsyntaxhighlight>
 
[[File:syntax.jpg]]
Line 1,619 ⟶ 3,169:
Here's another way to display a tree. The triangles open/close when clicked on.
 
<langsyntaxhighlight Mathematicalang="mathematica">OpenerView[{1, Column@{OpenerView[{2, Column@{OpenerView[{4, 7}, True], 5}}, True],
OpenerView[{3, OpenerView[{TraditionalForm[Cos[x]], Plot[Cos[x], {x, 0, 10}, ImageSize -> 150]},
True]}, True]}}, True]</langsyntaxhighlight>
 
[[File:opener.jpg]]
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">load(graphs)$
 
g: random_tree(10)$
Line 1,633 ⟶ 3,183:
true
 
draw_graph(g)$</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="nim">import strutils
 
type
Node[T] = ref TNode[T]object
TNode[T] = object
data: T
left, right: Node[T]
Line 1,661 ⟶ 3,210:
let tree = 1.n(2.n(4.n(7.n),5.n),3.n(6.n(8.n,9.n)))
 
echo tree.indent.join("\n")</langsyntaxhighlight>
 
{{out}}
<pre>--1
|--2
| |--4
| | |--7
| | | |-- (null)
| | | `-- (null)
| | `-- (null)
| `--5
| |-- (null)
| `-- (null)
`--3
|--6
| |--8
| | |-- (null)
| | `-- (null)
| `--9
| |-- (null)
| `-- (null)
`-- (null)</pre>
 
=={{header|Perl}}==
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 1,715 ⟶ 3,285:
my $tree = 'a(b0(c1,c2(d(ef,gh)),c3(i1,i2,i3(jj),i4(kk,m))),b1(C1,C2(D1(E),D2,D3),C3))';
my $parsed = [parse($tree)];
output($parsed);</langsyntaxhighlight>
{{out}}
<pre> a
Line 1,740 ⟶ 3,310:
│ └─D3
└─C3</pre>
 
=={{header|Perl 6}}==
 
<lang perl6>sub visualize-tree($tree, &label, &children,
:$indent = '',
:@mid = ('├─', '│ '),
:@end = ('└─', ' '),
) {
sub visit($node, *@pre) {
| gather {
take @pre[0] ~ label($node);
my @children := children($node);
my $end = @children.end;
for @children.kv -> $_, $child {
when $end { take visit($child, (@pre[1] X~ @end)) }
default { take visit($child, (@pre[1] X~ @mid)) }
}
}
}
visit($tree, $indent xx 2);
}
 
# example tree built up of pairs
my $tree = root=>[a=>[a1=>[a11=>[]]],b=>[b1=>[b11=>[]],b2=>[],b3=>[]]];
 
.map({.join("\n")}).join("\n").say for visualize-tree($tree, *.key, *.value.list);</lang>
 
{{out}}
<pre>root
├─a
│ └─a1
│ └─a11
└─b
├─b1
│ └─b11
├─b2
└─b3</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function rand_tree(integer low, integer high)
<span style="color: #000080;font-style:italic;">--
for i=1 to 2 do
-- demo\rosetta\VisualiseTree.exw
integer v = rand(high-low+1)-1+low
--</span>
if v!=low
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
and v!=high then
return {v,rand_tree(low,v),rand_tree(v,high)}
<span style="color: #000080;font-style:italic;">-- To the theme tune of the Milk Tray Ad iyrt,
end if
-- All because the Windows console hates utf8:</span>
end for
<span style="color: #008080;">constant</span> <span style="color: #000000;">TL</span> <span style="color: #0000FF;">=</span> '\<span style="color: #000000;">#DA</span>'<span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- aka '┌'</span>
return 0
<span style="color: #000000;">VT</span> <span style="color: #0000FF;">=</span> '\<span style="color: #000000;">#B3</span>'<span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- aka '│'</span>
end function
<span style="color: #000000;">BL</span> <span style="color: #0000FF;">=</span> '\<span style="color: #000000;">#C0</span>'<span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- aka '└'</span>
 
<span style="color: #000000;">HZ</span> <span style="color: #0000FF;">=</span> '\<span style="color: #000000;">#C4</span>'<span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- aka '─'</span>
object tree = rand_tree(0,20) -- (can be 0, ~1% chance)
<span style="color: #000000;">HS</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\#C4"</span> <span style="color: #000080;font-style:italic;">-- (string version of HZ)</span>
 
constant Horizontal = #C4,
<span style="color: #008080;">function</span> <span style="color: #000000;">w1252_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Horizontals = "\#C4",
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #008080;">then</span>
TopLeft = #DA,
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span> <span style="color: #000000;">TL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">VT</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">BL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">HZ</span><span style="color: #0000FF;">},</span>
Vertical = #B3,
<span style="color: #0000FF;">{</span><span style="color: #008000;">"┌"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"│"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"└"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"─"</span><span style="color: #0000FF;">})</span>
BtmLeft = #C0
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
procedure visualise_tree(object tree, string root=Horizontals)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
if atom(tree) then
<span style="color: #000080;font-style:italic;">--&lt;/hates utf8&gt;</span>
puts(1,"<empty>\n")
else
<span style="color: #008080;">procedure</span> <span style="color: #000000;">visualise_tree</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">root</span><span style="color: #0000FF;">=</span><span style="color: #000000;">HS</span><span style="color: #0000FF;">)</span>
object {v,l,r} = tree
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
integer g = root[$]
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;empty&gt;\n"</span><span style="color: #0000FF;">)</span>
if sequence(l) then
<span style="color: #008080;">else</span>
root[$] = iff(g=TopLeft or g=Horizontal?' ':Vertical)
<span style="color: #004080;">object</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tree</span>
visualise_tree(l,root&TopLeft)
<span style="color: #004080;">integer</span> <span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">root</span><span style="color: #0000FF;">[$]</span>
end if
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
root[$] = g
<span style="color: #000000;">root</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">=</span><span style="color: #000000;">TL</span> <span style="color: #008080;">or</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">=</span><span style="color: #000000;">HZ</span><span style="color: #0000FF;">?</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">:</span><span style="color: #000000;">VT</span><span style="color: #0000FF;">)</span>
puts(1,root)
<span style="color: #000000;">visualise_tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">root</span><span style="color: #0000FF;">&</span><span style="color: #000000;">TL</span><span style="color: #0000FF;">)</span>
?v
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if sequence(r) then
<span style="color: #000000;">root</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">g</span>
root[$] = iff(g=TopLeft?Vertical:' ')
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">w1252_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">),</span><span style="color: #000000;">v</span><span style="color: #0000FF;">})</span>
visualise_tree(r,root&BtmLeft)
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">root</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">=</span><span style="color: #000000;">TL</span><span style="color: #0000FF;">?</span><span style="color: #000000;">VT</span><span style="color: #0000FF;">:</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">visualise_tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">root</span><span style="color: #0000FF;">&</span><span style="color: #000000;">BL</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
visualise_tree(tree)</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rand_tree</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">low</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">high</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">high</span><span style="color: #0000FF;">-</span><span style="color: #000000;">low</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">low</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">low</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">high</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rand_tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">low</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">),</span><span style="color: #000000;">rand_tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">high</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">tree</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rand_tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (can be 0, &lt;1% chance)</span>
<span style="color: #000000;">visualise_tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--pp(tree,{pp_Nest,10})</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,838 ⟶ 3,390:
</pre>
A much simpler but less aesthetically pleasing way is just
<!--<syntaxhighlight lang="phix">-->
<lang Phix>pp(tree,{pp_Nest,10})</lang>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,858 ⟶ 3,412:
'view' is a built-in function in PicoLisp.
 
<langsyntaxhighlight PicoLisplang="picolisp">(view '(1 (2 (3 (4) (5) (6 (7))) (8 (9)) (10)) (11 (12) (13))))</langsyntaxhighlight>
 
Output:
Line 1,892 ⟶ 3,446:
===XPCE===
XPCE is the SWI-Prolog native GUI library.
<langsyntaxhighlight lang="prolog">% direction may be horizontal/vertical/list
display_tree(Direction) :-
sformat(A, 'Display tree ~w', [Direction]),
Line 1,911 ⟶ 3,465:
send(D, display, T),
send(D, open).
</syntaxhighlight>
</lang>
[[File:display_tree.png|900px]]
 
=={{header|Python}}==
===Library module===
Line 1,918 ⟶ 3,473:
 
If you set the presumed width of the output to 1 then pprint will print each level of a nested tuple (which is Pythons obvious method of creating a tree), on a separate line:
<langsyntaxhighlight lang="python">Python 3.2.3 (default, May 3 2012, 15:54:42)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
Line 1,965 ⟶ 3,520:
7,
8)
>>> </langsyntaxhighlight>
 
pprint (and print), prints Pythons standard container types in a format that is valid python so Python could parse its output:
<langsyntaxhighlight lang="python">>>> tree = "a",("b0",("c1","c2",("d",("ef","gh")),"c3",("i1","i2","i3",("jj"),"i4",("kk","m"))),"b1",("C1","C2",("D1",("E"),"D2","D3"),"C3"))
>>> pprint(tree, width=1)
('a',
Line 2,018 ⟶ 3,573:
>>> tree == copypasteoutput
True
>>> </langsyntaxhighlight>
 
pprints width parameter allows it to fold some structure to better fit the page:
<langsyntaxhighlight lang="python">>>> pprint(tree, width=60)
('a',
('b0',
Line 2,031 ⟶ 3,586:
'b1',
('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3')))
>>> </langsyntaxhighlight>
 
pprint works with with a mix of nested container types. Here we create a tree from both lists and tuples:
<langsyntaxhighlight lang="python">>>> mixedtree = ['a', ('b0', ('c1', 'c2', ['d', ('ef', 'gh')], 'c3', ('i1', 'i2',
... 'i3', 'jj', 'i4', ['kk', 'm'])), 'b1', ('C1', 'C2', ('D1', 'E',
... 'D2', 'D3'), 'C3'))]
Line 2,071 ⟶ 3,626:
'b1',
('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3'))]
>>> </langsyntaxhighlight>
 
 
===Functional composition===
====Vertically centered parents====
 
Using the same tree structure (including tree node constructor and accessors) as in the [[Tree_traversal|Tree Traversal]] task, and centering parent nodes vertically:
 
{{Works with|Python|3}}
<langsyntaxhighlight lang="python">'''Textually visualized tree, with vertically-centered parent nodes'''
 
from functools import reduce
Line 2,110 ⟶ 3,665:
def measured(x):
'''Value of a tree node
tupled with string length.
'''
s = ' ' + str(x) + ' '
return (len(s), s)
 
# lmrFromStrings :: [String] -> ([String], String, [String])
def lmrFromStrings(xs):
'''(Lefts, midMid, Rights).'''
i = len(xs) // 2
(ls, rs) = (xs[0:i], xs[i:])
return (ls, rs[0], rs[1:])
 
# stringsFromLMR :: ([String], String, [String]) -> [String]
Line 2,146 ⟶ 3,701:
def leftPad(n):
return lambda s: (' ' * n) + s
 
# cfix :: Char -> String -> String
def cfix(x):
return lambda xs: x + xs
 
# treeFix :: (Char, Char, Char) -> ([String], String, [String])
# -> [String]
def treeFix(l, m, r):
def cfix(x):
return lambda xs: x + xs
return compose(stringsFromLMR)(
fghOverLMR(cfix(l), cfix(m), cfix(r))
Line 2,223 ⟶ 3,776:
)
)
return [
 
# lineContainsNode :: String ->s Boolfor s in treeLines
def lineContainsNode if any(c not in '│ ' for c in s):
] if return any(c not inblnCompact '│and 'blnPruned) forelse c in s)treeLines
 
return filter(lineContainsNode, treeLines) if (
not blnCompact and blnPruned
) else treeLines
 
return lambda blnPruned: (
Line 2,279 ⟶ 3,828:
])
 
print(
print('Fully compacted (parents not centered):')
print( '\n\n'.join([
'Fully compacted (parents not all centered):',
drawTree2(True)(False)(
tree1drawTree2(True)(False)(
) tree1
)),
'Expanded with vertically centered parents:',
 
drawTree2(False)(False)(
print('\nExpanded with vertically centered parents:\n')
tree2
print((
drawTree2(False )(False)(,
'Centered parents with nodeless lines pruned out:',
tree2
drawTree2(False)(True)(
tree2
))
)
 
])
print('\nCentered parents with nodeless lines pruned out:\n')
print(()
drawTree2(False)(True)(
tree2
)
))
 
 
Line 2,419 ⟶ 3,964:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Fully compacted (parents not all centered):
 
┌ 4 ─ 7
┌ 2 ┴ 5
Line 2,460 ⟶ 4,006:
└─ Lambda </pre>
 
====Simple decorated-outline tree====
Or using the same data structures, but generating a simpler (decorated outline) visualisation:
{{Works with|Python|3}}
<langsyntaxhighlight lang="python">'''Visualize a tree'''
 
from itertools import (chain, repeat, starmap)
from operator import (add)
 
Line 2,482 ⟶ 4,028:
add,
zip(
chain([first], +repeat(other, listlen(xs) - 1)),
repeat(other, len(xs) - 1)
),
xs
)
Line 2,537 ⟶ 4,081:
value of some kind to a list of zero or
more child trees.'''
return lambda xs: {'type': 'Node', 'root': v, 'nest': xs}
 
 
Line 2,554 ⟶ 4,098:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>1
Line 2,575 ⟶ 4,119:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket/base
 
Line 2,589 ⟶ 4,133:
 
(visualize '(1 (2 (3 (4) (5) (6 (7))) (8 (9)) (10)) (11 (12) (13))))
</syntaxhighlight>
</lang>
 
Output:
Line 2,619 ⟶ 4,163:
\-13
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>sub visualize-tree($tree, &label, &children,
:$indent = '',
:@mid = ('├─', '│ '),
:@end = ('└─', ' '),
) {
sub visit($node, *@pre) {
| gather {
take @pre[0] ~ label($node);
my @children := children($node);
my $end = @children.end;
for @children.kv -> $_, $child {
when $end { take visit($child, (@pre[1] X~ @end)) }
default { take visit($child, (@pre[1] X~ @mid)) }
}
}
}
visit($tree, $indent xx 2);
}
 
# example tree built up of pairs
my $tree = root=>[a=>[a1=>[a11=>[]]],b=>[b1=>[b11=>[]],b2=>[],b3=>[]]];
 
.map({.join("\n")}).join("\n").say for visualize-tree($tree, *.key, *.value.list);</syntaxhighlight>
 
{{out}}
<pre>root
├─a
│ └─a1
│ └─a11
└─b
├─b1
│ └─b11
├─b2
└─b3</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 10.05.2014 Walter Pachl using the tree and the output format of C
**********************************************************************/
Line 2,692 ⟶ 4,274:
node.a.0level=node.father.0level+1
Return
</syntaxhighlight>
</lang>
{{out}}
<pre>R
Line 2,712 ⟶ 4,294:
=={{header|Ruby}}==
Modifying [[Tree_traversal#Ruby]] by adding somewhere after the line
<syntaxhighlight lang="ruby">
<lang Ruby>
root = BinaryTreeNode.from_array [1, [2, [4, 7], [5]], [3, [6, [8], [9]]]]
</syntaxhighlight>
</lang>
the lines
<syntaxhighlight lang="ruby">
<lang Ruby>
require 'pp'
pp root
</syntaxhighlight>
</lang>
will produce:
{{out}}
Line 2,742 ⟶ 4,324:
@value=1>
</pre>
<syntaxhighlight lang="ruby">
<lang Ruby>
def ptree(tree,indent=" ")
case tree
Line 2,755 ⟶ 4,337:
end
ptree [1,2,3,[4,5,6,[7,8,9]],3,[22,33]]
</syntaxhighlight>
</lang>
will produce:
{{out}}
Line 2,775 ⟶ 4,357:
=={{header|Rust}}==
Console visualization of binary trees translated from parts of [http://rosettacode.org/wiki/AVL_tree/C the C AVL tree solution].
<syntaxhighlight lang="rust">
<lang Rust>
extern crate rustc_serialize;
extern crate term_painter;
Line 2,942 ⟶ 4,524:
println!("{}", tree);
}
</syntaxhighlight>
</lang>
{{out}}
[[File:Visualize_a_tree-rust-1.png]]
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">func visualize_tree(tree, label, children,
indent = '',
mids = ['├─', '│ '],
Line 2,968 ⟶ 4,550:
 
var tree = 'root':['a':['a1':['a11':[]]],'b':['b1':['b11':[]],'b2':[],'b3':[]]]
say visualize_tree(tree, { .first }, { .second }).flatten.join("\n")</langsyntaxhighlight>
{{out}}
<pre>
Line 2,984 ⟶ 4,566:
=={{header|Tcl}}==
{{tcllib|struct::tree}}
<langsyntaxhighlight lang="tcl">package require struct::tree
 
proc visualize_tree {tree {nameattr name}} {
Line 3,009 ⟶ 4,591:
}
}
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight 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</langsyntaxhighlight>
{{out}}
<pre>
Line 3,023 ⟶ 4,605:
├─b
└─c
</pre>
 
=={{header|Wren}}==
{{trans|C}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="wren">import "./dynamic" for Struct
import "random" for Random
 
var Stem = Struct.create("Stem", ["str", "next"])
 
var SDOWN = " |"
var SLAST = " `"
var SNONE = " "
 
var rand = Random.new()
 
var tree // recursive
tree = Fn.new { |root, head|
var col = Stem.new(null, null)
var tail = head
while (tail) {
System.write(tail.str)
if (!tail.next) break
tail = tail.next
}
System.print("--%(root)")
if (root <= 1) return
if (tail && tail.str == SLAST) tail.str = SNONE
if (!tail) {
tail = head = col
} else {
tail.next = col
}
while (root != 0) { // make a tree by doing something random
var r = 1 + rand.int(root)
root = root - r
col.str = (root != 0) ? SDOWN : SLAST
tree.call(r, head)
}
tail.next = null
}
 
var n = 8
tree.call(n, null)</syntaxhighlight>
 
{{out}}
Sample output:
<pre>
--8
|--7
| |--6
| | |--4
| | | |--1
| | | `--3
| | | |--1
| | | `--2
| | | |--1
| | | `--1
| | `--2
| | `--2
| | |--1
| | `--1
| `--1
`--1
</pre>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">clear screen
 
dim colore$(1)
Line 3,058 ⟶ 4,704:
end switch
end sub
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
Line 3,079 ⟶ 4,725:
It does this with data that looks like:
L("Network.TCPServerSocket","File","ZKLShell.Granny","Int","startup","Utils.Inspector","Thread.Straw","Ref","Utils.Argh" ...)
<langsyntaxhighlight lang="zkl">fcn vaultDir(out=Console){
const INDENT=" ";
space:=""; lastPath:=L();
Line 3,099 ⟶ 4,745:
"" // so startup has something to display
}
</syntaxhighlight>
</lang>
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
<?php
 
function printTree( array $tree , string $key = "." , string $stack = "" , $first = TRUE , $firstPadding = NULL )
{
if ( gettype($tree) == "array" )
{
if($firstPadding === NULL) $firstPadding = ( count($tree)>1 ) ? "│ " : " " ;
echo $key . PHP_EOL ;
foreach ($tree as $key => $value) {
if ($key === array_key_last($tree)){
echo (($first) ? "" : $firstPadding ) . $stack . "└── ";
$padding = " ";
if($first) $firstPadding = " ";
}
else {
echo (($first) ? "" : $firstPadding ) . $stack . "├── ";
$padding = "│ ";
}
if( is_array($value) )printTree( $value , $key , $stack . (($first) ? "" : $padding ) , FALSE , $firstPadding );
else echo $key . " -> " . $value . PHP_EOL;
}
}
else echo $tree . PHP_EOL;
}
 
 
 
// ---------------------------------------TESTING FUNCTION-------------------------------------
 
 
$sample_array_1 =
[
0 => [
'item_id' => 6,
'price' => "2311.00",
'qty' => 12,
'discount' => 0
],
1 => [
'item_id' => 7,
'price' => "1231.00",
'qty' => 1,
'discount' => 12
],
2 => [
'item_id' => 8,
'price' => "123896.00",
'qty' => 0,
'discount' => 24
]
];
$sample_array_2 = array(
array(
"name"=>"John",
"lastname"=>"Doe",
"country"=>"Japan",
"nationality"=>"Japanese",
"job"=>"web developer",
"hobbies"=>array(
"sports"=>"soccer",
"others"=>array(
"freetime"=>"watching Tv"
)
)
 
)
);
$sample_array_3 = [
"root" => [
"first_depth_node1" =>[
"second_depth_node1",
"second_depth_node2" => [
"third_depth_node1" ,
"third_depth_node2" ,
"third_depth_node3" => [
"fourth_depth_node1",
"fourth_depth_node2",
]
],
"second_depth_node3",
] ,
"first_depth_node2" => [
"second_depth_node4" => [ "third_depth_node3" => [1]]
]
]
];
$sample_array_4 = [];
$sample_array_5 = ["1"];
$sample_array_5 = ["1"];
$sample_array_6 = [
"T",
"Ta",
"Tad",
"Tada",
"Tadav",
"Tadavo",
"Tadavom",
"Tadavomn",
"Tadavomni",
"Tadavomnis",
"TadavomnisT",
];
 
 
printTree($sample_array_1);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_2);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_3);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_4);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_5);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_6);
 
 
?>
</syntaxhighlight>
 
{{out}}
<pre>
.
├── 0
│ ├── item_id -> 6
│ ├── price -> 2311.00
│ ├── qty -> 12
│ └── discount -> 0
├── 1
│ ├── item_id -> 7
│ ├── price -> 1231.00
│ ├── qty -> 1
│ └── discount -> 12
└── 2
├── item_id -> 8
├── price -> 123896.00
├── qty -> 0
└── discount -> 24
 
------------------------------
.
└── 0
├── name -> John
├── lastname -> Doe
├── country -> Japan
├── nationality -> Japanese
├── job -> web developer
└── hobbies
├── sports -> soccer
└── others
└── freetime -> watching Tv
 
------------------------------
.
└── root
├── first_depth_node1
│ ├── 0 -> second_depth_node1
│ ├── second_depth_node2
│ │ ├── 0 -> third_depth_node1
│ │ ├── 1 -> third_depth_node2
│ │ └── third_depth_node3
│ │ ├── 0 -> fourth_depth_node1
│ │ └── 1 -> fourth_depth_node2
│ └── 1 -> second_depth_node3
└── first_depth_node2
└── second_depth_node4
└── third_depth_node3
└── 0 -> 1
 
------------------------------
.
 
------------------------------
.
└── 0 -> 1
 
------------------------------
.
├── 0 -> T
├── 1 -> Ta
├── 2 -> Tad
├── 3 -> Tada
├── 4 -> Tadav
├── 5 -> Tadavo
├── 6 -> Tadavom
├── 7 -> Tadavomn
├── 8 -> Tadavomni
├── 9 -> Tadavomnis
└── 10 -> TadavomnisT
</pre>
2,120

edits