Visualize a tree: Difference between revisions
(→Functional composition: Added preamble link.) |
m (→Functional composition: (Tidying layout)) |
||
Line 2,121: | Line 2,121: | ||
# TEST ---------------------------------------------------- |
# TEST ---------------------------------------------------- |
||
# main :: IO () |
|||
def main(): |
def main(): |
||
Revision as of 14:41, 9 March 2019
You are encouraged to solve this task according to the task description, using any language you may know.
A tree structure (i.e. a rooted, connected acyclic graph) is often used in programming.
It's often helpful to visually examine such a structure.
There are many ways to represent trees to a reader, such as:
- indented text (à la unix
tree
command) - nested HTML tables
- hierarchical GUI widgets
- 2D or 3D images
- etc.
- indented text (à la unix
- Task
Write a program to produce a visual representation of some tree.
The content of the tree doesn't matter, nor does the output format, the only requirement being that the output is human friendly.
Make do with the vague term "friendly" the best you can.
11l
<lang 11l>T Node
String value Node? left Node? right
F (value, Node? left = N, Node? right = N) .value = String(value) .left = left .right = right
F tree_indent() -> [String] V tr = I .right != N {.right.tree_indent()} E [‘-- (null)’] R [‘--’(.value)] [+] (I .left != N {.left.tree_indent()} E [‘-- (null)’]).map(a -> ‘ |’a) [+] [‘ `’tr[0]] + tr[1..].map(a -> ‘ ’a)
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"))</lang>
Ada
Prints a tree of the current directory. <lang Ada>with Ada.Text_IO, Ada.Directories;
procedure Directory_Tree is
procedure Print_Tree(Current: String; Indention: Natural := 0) is function Spaces(N: Natural) return String is
(if N= 0 then "" else " " & Spaces(N-1));
use Ada.Directories; Search: Search_Type; Found: Directory_Entry_Type; begin Start_Search(Search, Current, ""); while More_Entries(Search) loop
Get_Next_Entry(Search, Found); declare Name: String := Simple_Name(Found); Dir: Boolean := Kind(Found) = Directory; begin if Name(Name'First) /= '.' then
-- skip all files who's names start with ".", namely "." and ".."
Ada.Text_IO.Put_Line(Spaces(2*Indention) & Simple_Name(Found) & (if Dir then " (dir)" else "")); if Dir then Print_Tree(Full_Name(Found), Indention + 1); end if; end if; end;
end loop; end Print_Tree;
begin
Print_Tree(Ada.Directories.Current_Directory);
end Directory_Tree;</lang>
- Output:
outer (dir) inner (dir) innermost (dir) file another file some
ALGOL 68
<lang algol68># outputs nested html tables to visualise a tree #
- mode representing nodes of the tree #
MODE NODE = STRUCT( STRING value, REF NODE child, REF NODE sibling ); REF NODE nil node = NIL;
- tags etc. #
STRING table = "
" , elbat = "" , tr = "" , rt = "" , td = "<td style=""text-align: center; vertical-align: top; """ , dt = "" , nbsp = " " ; CHAR nl = REPR 10;
- returns the number of child elements of tree #
OP CHILDCOUNT = ( REF NODE tree )INT: BEGIN INT result := 0; REF NODE child := child OF tree; WHILE REF NODE( child ) ISNT nil node DO result +:= 1; child := sibling OF child OD; result END # CHILDCOUNT # ;
- generates nested HTML tables from the tree #
OP TOHTML = ( REF NODE tree )STRING: IF tree IS nil node THEN # no node # "" ELSE # hae at least one node # STRING result := ""; INT child count = CHILDCOUNT tree; result +:= table + nl + tr + nl + td + " colspan=""" + whole( IF child count < 1 THEN 1 ELSE child count FI, 0 ) + """>" + nbsp + value OF tree + nbsp + dt + nl + rt + nl ; IF child count > 0 THEN # the node has branches # REF NODE child := child OF tree; INT child number := 1; INT mid child = ( child count + 1 ) OVER 2; child := child OF tree; result +:= tr + nl; WHILE child ISNT nil node DO result +:= td + ">" + nl + IF CHILDCOUNT child < 1 THEN nbsp + value OF child + nbsp ELSE TOHTML child FI + dt + nl; child := sibling OF child OD; result +:= rt + nl FI; result +:= elbat + nl FI # TOHTML # ;
- test the tree visualisation #
- returns a new node with the specified value and no child or siblings #
PROC new node = ( STRING value )REF NODE: HEAP NODE := NODE( value, nil node, nil node );
- appends a sibling node to the node n, returns the sibling #
OP +:= = ( REF NODE n, REF NODE sibling node )REF NODE: BEGIN REF NODE sibling := n; WHILE REF NODE( sibling OF sibling ) ISNT nil node DO sibling := sibling OF sibling OD; sibling OF sibling := sibling node END # +:= # ;
- appends a new sibling node to the node n, returns the sibling #
OP +:= = ( REF NODE n, STRING sibling value )REF NODE: n +:= new node( sibling value );
- adds a child node to the node n, returns the child #
OP /:= = ( REF NODE n, REF NODE child node )REF NODE: child OF n := child node;
- adda a new child node to the node n, returns the child #
OP /:= = ( REF NODE n, STRING child value )REF NODE: n /:= new node( child value ); NODE animals := new node( "animals" ); NODE fish := new node( "fish" ); NODE reptiles := new node( "reptiles" ); NODE mammals := new node( "mammals" ); NODE primates := new node( "primates" ); NODE sharks := new node( "sharks" ); sharks /:= "great-white" +:= "hammer-head"; fish /:= "cod" +:= sharks +:= "piranha"; reptiles /:= "iguana" +:= "brontosaurus"; primates /:= "gorilla" +:= "lemur"; mammals /:= "sloth" +:= "horse" +:= "bison" +:= primates; animals /:= fish +:= reptiles +:= mammals; print( ( TOHTML animals ) )</lang>
- Output:
animals | ||||||||||||||||||||||||||||
|
|
|
Batch File
Prints a tree of the current directory. <lang dos>@tree %cd%</lang>
BBC BASIC
This creates a native Windows Tree View control: <lang bbcbasic> INSTALL @lib$+"WINLIB5"
ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 0 : QUIT REM!WC Windows constants: TVI_SORT = -65533 TVIF_TEXT = 1 TVM_INSERTITEM = 4352 TVS_HASBUTTONS = 1 TVS_HASLINES = 2 TVS_LINESATROOT = 4 REM. TV_INSERTSTRUCT DIM tvi{hParent%, \ \ hInsertAfter%, \ \ mask%, \ \ hItem%, \ \ state%, \ \ stateMask%, \ \ pszText%, \ \ cchTextMax%, \ \ iImage%, \ \ iSelectedImage%,\ \ cChildren%, \ \ lParam% \ \ } SYS "InitCommonControls" hTree% = FN_createwindow("SysTreeView32", "", 0, 0, @vdu.tr%, @vdu.tb%, 0, \ \ TVS_HASLINES OR TVS_HASBUTTONS OR TVS_LINESATROOT, 0) hroot% = FNinsertnode(0, "Root") hchild1% = FNinsertnode(hroot%, "Child 1") hchild2% = FNinsertnode(hroot%, "Child 2") hchild11% = FNinsertnode(hchild1%, "Grandchild 1") hchild12% = FNinsertnode(hchild1%, "Grandchild 2") hchild21% = FNinsertnode(hchild2%, "Grandchild 3") hchild22% = FNinsertnode(hchild2%, "Grandchild 4") REPEAT WAIT 1 UNTIL FALSE END DEF FNinsertnode(hparent%, text$) LOCAL hnode% text$ += CHR$0 tvi.hParent% = hparent% tvi.hInsertAfter% = TVI_SORT tvi.mask% = TVIF_TEXT tvi.pszText% = !^text$ SYS "SendMessage", hTree%, TVM_INSERTITEM, 0, tvi{} TO hnode% IF hnode% = 0 ERROR 100, "TVM_INSERTITEM failed" SYS "InvalidateRect", hTree%, 0, 0 = hnode%</lang>
C
Print a simple tree to standard output: <lang c>#include <stdio.h>
- include <stdlib.h>
typedef struct stem_t *stem; struct stem_t { const char *str; stem next; };
void tree(int root, stem head) { static const char *sdown = " |", *slast = " `", *snone = " "; struct stem_t col = {0, 0}, *tail;
for (tail = head; tail; tail = tail->next) { printf("%s", tail->str); if (!tail->next) break; }
printf("--%d\n", root);
if (root <= 1) return;
if (tail && tail->str == slast) tail->str = snone;
if (!tail) tail = head = &col; else tail->next = &col;
while (root) { // make a tree by doing something random int r = 1 + (rand() % root); root -= r; col.str = root ? sdown : slast;
tree(r, head); }
tail->next = 0; }
int main(int c, char**v) { int n; if (c < 2 || (n = atoi(v[1])) < 0) n = 8;
tree(n, 0); return 0; }</lang>
- Output:
--8 `--8 |--7 | |--3 | | |--2 | | | `--2 | | | `--2 | | | |--1 | | | `--1 | | `--1 | |--2 | | |--1 | | `--1 | |--1 | `--1 `--1
Clojure
<lang clojure>(use 'vijual)
(draw-tree [[:A] [:B] [:C [:D [:E] [:F]] [:G]]]) </lang>
- Output:
+---+ +---+ +---+ | A | | B | | C | +---+ +---+ +-+-+ | +-----+ | | +-+-+ +-+-+ | D | | G | +-+-+ +---+ | +--+--+ | | +-+-+ +-+-+ | E | | F | +---+ +---+
Common Lisp
<lang lisp>(defun visualize (tree)
(labels ((rprint (list) (mapc #'princ (reverse list))) (vis-h (tree branches) (let ((len (length tree))) (loop for item in tree for idx from 1 to len do (cond ((listp item) (rprint (cdr branches)) (princ "+---+") (let ((next (cons "| " (if (= idx len) (cons " " (cdr branches)) branches)))) (terpri) (rprint (if (null item) (cdr next) next)) (terpri) (vis-h item next))) (t (rprint (cdr branches)) (princ item) (terpri) (rprint (if (= idx len) (cdr branches) branches)) (terpri))))))) (vis-h tree '("| "))))</lang>
- Output:
<lang lisp>CL-USER> (visualize '(a b c ((d (e ((() ()))) f)) (g))) A | B | C | +---+ | | | +---+ | | | D | | | +---+ | | | | | E | | | | | +---+ | | | | | +---+ | | | | | +---+ | | | | | +---+ | | | F | +---+
| G
NIL</lang>
D
<lang d>import std.stdio, std.conv, std.algorithm, std.array;
struct Node(T) { T value; Node* left, right; }
string[] treeIndent(T)(in Node!T* t) pure nothrow @safe {
if (!t) return ["-- (null)"]; const tr = t.right.treeIndent; return "--" ~ t.value.text ~ t.left.treeIndent.map!q{" |" ~ a}.array ~ (" `" ~ tr[0]) ~ tr[1 .. $].map!q{" " ~ a}.array;
}
void main () {
static N(T)(T v, Node!T* l=null, Node!T* r=null) { return new Node!T(v, l, r); }
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);
}</lang>
- Output:
--1 |--2 | |--4 | | |--7 | | | |-- (null) | | | `-- (null) | | `-- (null) | `--5 | |-- (null) | `-- (null) `--3 |--6 | |--8 | | |-- (null) | | `-- (null) | `--9 | |-- (null) | `-- (null) `-- (null)
Elena
ELENA 3.4 : <lang elena>import system'routines. import extensions.
class Node {
object theValue. object theChildren. constructor name : value children:children [ theValue := value. theChildren := (children ?? Array min) toArray. ] constructor name : value <= name:value children:nil. constructor children:children <= name:emptyLiteral children:children. constructor name : value child:child <= name:value children(Array single:child). get = theValue. children = theChildren.
}
extension treeOp {
writeTree(node,prefix) [ var children := node children. var length := children length. children zip(RangeEnumerator from:1 to:length) forEach(:child:index) [ self printLine(prefix,"|"). self printLine(prefix,"+---",child get). var nodeLine := prefix + (index==length)iif(" ","| "). self writeTree(child,nodeLine). ]. ^ self. ] writeTree(node) = self~treeOp writeTree(node,"").
}
public program [
var tree := Node children: ( Node name:"a" children: ( Node name:"b" child:(Node name:"c"), Node name:"d" ), Node name:"e" ). console writeTree(tree); readChar.
]</lang>
- Output:
| +---a | | | +---b | | | | | +---c | | | +---d | +---b
Erlang
Until real code shows up, I follow the lead of Python and print tuples with a width of 1.
- Output:
9> io:fwrite("~1p", [{1, 2, {30, 40}, {{500, 600}, 70}}]). {1, 2, {30, 40}, {{500, 600}, 70}}
F#
<lang fsharp>type tree =
| T of string * tree list
let prefMid = seq { yield "├─"; while true do yield "│ " } let prefEnd = seq { yield "└─"; while true do yield " " } let prefNone = seq { while true do yield "" }
let c2 x y = Seq.map2 (fun u v -> String.concat "" [u; v]) x y
let rec visualize (T(label, children)) pre =
seq { yield (Seq.head pre) + label if children <> [] then let preRest = Seq.skip 1 pre let last = Seq.last (List.toSeq children) for e in children do if e = last then yield! visualize e (c2 preRest prefEnd) else yield! visualize e (c2 preRest prefMid) }
let example =
T ("root", [T ("a", [T ("a1", [T ("a11", []); T ("a12", []) ]) ]); T ("b", [T ("b1", []) ]) ])
visualize example prefNone |> Seq.iter (printfn "%s")</lang>
- Output:
root ├─a │ └─a1 │ ├─a11 │ └─a12 └─b └─b1
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 tab-size
and margin
for customizing the look of a tree. For smaller trees, it's best to change margin
from its default of 64
to something low, perhaps 10
.
<lang factor>USE: literals
CONSTANT: mammals { "mammals" { "deer" "gorilla" "dolphin" } } CONSTANT: reptiles { "reptiles" { "turtle" "lizard" "snake" } }
{ "animals" ${ mammals reptiles } } dup . 10 margin set .</lang>
- Output:
{ "animals" { { "mammals" { "deer" "gorilla" "dolphin" } } { "reptiles" { "turtle" "lizard" "snake" } } } } { "animals" { { "mammals" { "deer" "gorilla" "dolphin" } } { "reptiles" { "turtle" "lizard" "snake" } } } }
An example showcasing tuples by displaying an AVL tree: <lang factor>USE: trees.avl AVL{ { 1 2 } { 9 19 } { 3 4 } { 5 6 } } .</lang>
- Output:
T{ avl { root T{ avl-node { key 3 } { value 4 } { left T{ avl-node { key 1 } { value 2 } { balance 0 } } } { right T{ avl-node { key 9 } { value 19 } { left T{ avl-node { key 5 } { value 6 } { balance 0 } } } { balance -1 } } } { balance 1 } } } { count 4 } }
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. <lang Go>package main
import (
"encoding/json" "fmt" "log"
)
type Node struct {
Name string Children []*Node
}
func main() {
tree := &Node{"root", []*Node{ &Node{"a", []*Node{ &Node{"d", nil}, &Node{"e", []*Node{ &Node{"f", nil}, }}}}, &Node{"b", nil}, &Node{"c", nil}, }} b, err := json.MarshalIndent(tree, "", " ") if err != nil { log.Fatal(err) } fmt.Println(string(b))
}</lang>
- Output:
{ "Name": "root", "Children": [ { "Name": "a", "Children": [ { "Name": "d", "Children": null }, { "Name": "e", "Children": [ { "Name": "f", "Children": null } ] } ] }, { "Name": "b", "Children": null }, { "Name": "c", "Children": null } ] }
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. <lang go>package main
import (
"log" "os"
"github.com/BurntSushi/toml"
)
type Node struct {
Name string Children []*Node
}
func main() {
tree := &Node{"root", []*Node{ &Node{"a", []*Node{ &Node{"d", nil}, &Node{"e", []*Node{ &Node{"f", nil}, }}}}, &Node{"b", nil}, &Node{"c", nil}, }} enc := toml.NewEncoder(os.Stdout) enc.Indent = " " err := enc.Encode(tree) if err != nil { log.Fatal(err) }
}</lang>
- Output:
Name = "root" [[Children]] Name = "a" [[Children.Children]] Name = "d" [[Children.Children]] Name = "e" [[Children.Children.Children]] Name = "f" [[Children]] Name = "b" [[Children]] Name = "c"
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. <lang go>package main
import "fmt"
type tree []node
type node struct {
label string children []int // indexes into tree
}
func main() {
vis(tree{ 0: node{"root", []int{1, 2, 3}}, 1: node{"ei", []int{4, 5}}, 2: node{"bee", nil}, 3: node{"si", nil}, 4: node{"dee", nil}, 5: node{"y", []int{6}}, 6: node{"eff", nil}, })
}
func vis(t tree) {
if len(t) == 0 { fmt.Println("<empty>") return } var f func(int, string) f = func(n int, pre string) { ch := t[n].children if len(ch) == 0 { fmt.Println("╴", t[n].label) return } fmt.Println("┐", t[n].label) last := len(ch) - 1 for _, ch := range ch[:last] { fmt.Print(pre, "├─") f(ch, pre+"│ ") } fmt.Print(pre, "└─") f(ch[last], pre+" ") } f(0, "")
}</lang>
- Output:
┐ root ├─┐ ei │ ├─╴ dee │ └─┐ y │ └─╴ eff ├─╴ bee └─╴ si
Haskell
Tree borrowed from Tree traversal: <lang haskell>data Tree a = Empty | Node { value :: a, left :: Tree a, right :: Tree a } deriving (Show, Eq)
tree = Node 1 (Node 2 (Node 4 (Node 7 Empty Empty) Empty) (Node 5 Empty Empty)) (Node 3 (Node 6 (Node 8 Empty Empty) (Node 9 Empty Empty)) Empty)
treeIndent Empty = ["-- (nil)"] treeIndent t = ["--" ++ show (value t)] ++ map (" |"++) ls ++ (" `" ++ r):map (" "++) rs where (r:rs) = treeIndent$right t ls = treeIndent$left t
main = mapM_ putStrLn $ treeIndent tree</lang>
- Output:
--1 |--2 | |--4 | | |--7 | | | |-- (nil) | | | `-- (nil) | | `-- (nil) | `--5 | |-- (nil) | `-- (nil) `--3 |--6 | |--8 | | |-- (nil) | | `-- (nil) | `--9 | |-- (nil) | `-- (nil) `-- (nil)
Icon and Unicon
The following works in both languages. <lang unicon>procedure main(A)
showTree("", " -", [1, [2,[3],[4,[5],[6]],[7,[11]]], [8,[9,[10]]] ]) write() showTree("", " -", [1, [2,[3,[4]]], [5,[6],[7,[8],[9]],[10]] ])
end
procedure showTree(prefix, lastc, A)
write(prefix, lastc, "--", A[1]) if *A > 1 then { prefix ||:= if prefix[-1] == "|" then " " else " " every showTree(prefix||"|", "-", !A[2:2 < *A]) showTree(prefix, "`-", A[*A]) }
end</lang>
Output:
->tree ---1 |---2 | |---3 | |---4 | | |---5 | | `---6 | `---7 | `---11 `---8 `---9 `---10 ---1 |---2 | `---3 | `---4 `---5 |---6 |---7 | |---8 | `---9 `---10 ->
J
See: j:Essays/Tree Display for tree represented as label pairs.
Or, adapted to the parent index representation of a tree (which allows different nodes to share labels and may also be more convenient for other reasons):
<lang J>BOXC=: 9!:6 NB. box drawing characters EW =: {: BOXC NB. east-west
showtree=: 4 : 0
NB. y is parent index for each node (non-indices for root nodes) NB. x is label for each node t=. (<EW,' ') ,@<@,:@,&":&.> x NB. tree fragments c=. |:(#~ e./@|:);(~.,"0&.>(</. i.@#)) y while. +./ b=. ({.c)*.//.-.e.~/c do. i=. b#~.{.c NB. parents whose children are leaves j=. </./(({.c)e.i)#"1 c NB. leaves grouped by parents t=. a: (;j)}t i}~ (i{t) subtree&.> j{&.><t c=. (-.({.c)e.i)#"1 c NB. prune edges to leaves end. ;([: ,.&.>/ extend&.>)&> t -. a:
)
subtree=: 4 : 0
p=. EW={."1 s=. >{.t=. graft y (<(>{.x) root p),(<(connect p),.s),}.t
)
graft=: 3 : 0
n=. (-~ >./) #&> y f=. i.@(,&0)@#&.>@{.&.> y ,&.>/ y ,&> n$&.>f
)
connect=: 3 : 0
b=. (+./\ *. +./\.) y c=. (b+2*y){' ',9 3 3{BOXC NB. │ NS ├ E c=. (0{BOXC) (b i. 1)}c NB. ┌ NW c=. (6{BOXC) (b i: 1)}c NB. └ SW j=. (b i. 1)+<.-:+/b EW&(j})^:(1=+/b) c j}~ ((0 3 6 9{BOXC)i.j{c){1 4 7 5{BOXC
)
root=: 4 : 0
j=. k+<.-:1+(y i: 1)-k=. y i. 1 (-j)|.(#y){.x,.,:' ',EW
)
extend=: 3 : '(+./\"1 (y=EW) *. *./\."1 y e. ,EW)}y,:EW' </lang>
Example use:
<lang j> (i.10) showtree _,}.p:inv i.10
┌─ 6 ┌─ 1 ─── 3 ─┴─ 7 │ ┌─ 8
─ 0 ─┤ ┌─ 4 ─┴─ 9
└─ 2 ─┴─ 5 </lang>
Java
Minimalist BST that can do nothing except print itself to stdout. <lang java>public class VisualizeTree {
public static void main(String[] args) { BinarySearchTree tree = new BinarySearchTree();
tree.insert(100); for (int i = 0; i < 20; i++) tree.insert((int) (Math.random() * 200)); tree.display(); }
}
class BinarySearchTree {
private Node root;
private class Node { private int key; private Node left, right;
Node(int k) { key = k; } }
public boolean insert(int key) { if (root == null) root = new Node(key); else { Node n = root; Node parent; while (true) { if (n.key == key) return false;
parent = n;
boolean goLeft = key < n.key; n = goLeft ? n.left : n.right;
if (n == null) { if (goLeft) { parent.left = new Node(key); } else { parent.right = new Node(key); } break; } } } return true; }
public void display() { final int height = 5, width = 64;
int len = width * height * 2 + 2; StringBuilder sb = new StringBuilder(len); for (int i = 1; i <= len; i++) sb.append(i < len - 2 && i % width == 0 ? "\n" : ' ');
displayR(sb, width / 2, 1, width / 4, width, root, " "); System.out.println(sb); }
private void displayR(StringBuilder sb, int c, int r, int d, int w, Node n, String edge) { if (n != null) { displayR(sb, c - d, r + 2, d / 2, w, n.left, " /");
String s = String.valueOf(n.key); int idx1 = r * w + c - (s.length() + 1) / 2; int idx2 = idx1 + s.length(); int idx3 = idx1 - w; if (idx2 < sb.length()) sb.replace(idx1, idx2, s).replace(idx3, idx3 + 2, edge);
displayR(sb, c + d, r + 2, d / 2, w, n.right, "\\ "); } }
}</lang>
100 / \ 49 106 / \ / \ 44 94 105 152 / \ / / \ 26 47 61 109 178 / \ / \ \ / 12 33 51 88 119 159
JavaScript
HTML
Javascript wrapped in HTML5 document. Should work in modern browsers. <lang html><!doctype html> <html id="doc">
<head><meta charset="utf-8"/> <title>Stuff</title> <script type="application/javascript">
function gid(id) { return document.getElementById(id); }
function ce(tag, cls, parent_node) { var e = document.createElement(tag); e.className = cls; if (parent_node) parent_node.appendChild(e); return e; }
function dom_tree(id) { gid('tree').textContent = ""; gid('tree').appendChild(mktree(gid(id), null)); }
function mktree(e, p) { var t = ce("div", "tree", p); var tog = ce("span", "toggle", t); var h = ce("span", "tag", t);
if (e.tagName === undefined) { h.textContent = "#Text"; var txt = e.textContent; if (txt.length > 0 && txt.match(/\S/)) { h = ce("div", "txt", t); h.textContent = txt; } return t; }
tog.textContent = "−"; tog.onclick = function () { clicked(tog); } h.textContent = e.nodeName;
var l = e.childNodes; for (var i = 0; i != l.length; i++) mktree(l[i], t); return t; }
function clicked(e) { var is_on = e.textContent == "−"; e.textContent = is_on ? "+" : "−"; e.parentNode.className = is_on ? "tree-hide" : "tree"; }
</script> <style> #tree { white-space: pre; font-family: monospace; border: 1px solid } .tree > .tree-hide, .tree > .tree
{ margin-left: 2em; border-left: 1px dotted rgba(0,0,0,.2)}
.tree-hide > .tree, .tree-hide > .tree-hide { display: none } .tag { color: navy } .tree-hide > .tag { color: maroon } .txt { color: gray; padding: 0 .5em; margin: 0 .5em 0 2em; border: 1px dotted rgba(0,0,0,.1) } .toggle { display: inline-block; width: 2em; text-align: center } </style> </head> <body> <article>
Headline
Blah blah
More headline
Something something
<section>
Nested section
Somethin somethin list:
- Apples
- Oranges
- Cetera Fruits
</section> </article>
</body>
</html></lang>
Plain text
<lang JavaScript>(() => {
'use strict';
const main = () => drawTree(dctTree);
// draw :: Tree String -> [String] const draw = node => { // shift :: String -> String -> [String] -> [String] const shift = (first, other, xs) => zipWith( append, cons(first, replicate(xs.length - 1, other)), xs ); // drawSubTrees :: [Tree String] -> [String] const drawSubTrees = xs => { const lng = xs.length; return 0 < lng ? ( 1 < lng ? append( cons( '│', shift('├─ ', '│ ', draw(xs[0])) ), drawSubTrees(xs.slice(1)) ) : cons('│', shift('└─ ', ' ', draw(xs[0]))) ) : []; }; return append( lines(node.root.toString()), drawSubTrees(node.nest) ); };
// drawTree :: Tree String -> String const drawTree = tree => unlines(draw(tree));
const dctTree = { "type": "Node", "root": "alpha", "nest": [{ "type": "Node", "root": "beta", "nest": [{ "type": "Node", "root": "gamma", "nest": [] }, { "type": "Node", "root": "epsilon", "nest": [] }, { "type": "Node", "root": "eta", "nest": [] } ] }, { "type": "Node", "root": "iota", "nest": [{ "type": "Node", "root": "kappa", "nest": [] }, { "type": "Node", "root": "mu", "nest": [] }, { "type": "Node", "root": "xi", "nest": [] } ] }, { "type": "Node", "root": "pi", "nest": [{ "type": "Node", "root": "rho", "nest": [] }, { "type": "Node", "root": "tau", "nest": [] }, { "type": "Node", "root": "phi", "nest": [] } ] }, { "type": "Node", "root": "psi", "nest": [] } ] };
// GENERIC FUNCTIONS ----------------------------
// append (++) :: [a] -> [a] -> [a] // append (++) :: String -> String -> String const append = (xs, ys) => xs.concat(ys);
// cons :: a -> [a] -> [a] const cons = (x, xs) => Array.isArray(xs) ? ( [x].concat(xs) ) : (x + xs);
// 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 => xs.length || Infinity;
// lines :: String -> [String] const lines = s => s.split(/[\r\n]/);
// replicate :: Int -> a -> [a] const replicate = (n, x) => Array.from({ length: n }, () => x);
// shift :: Int -> [a] -> [a] const shift = (n, xs) => { const lng = length(xs); return Infinity > lng ? ( take(lng, drop(n, cycle(xs))) ) : (drop(n, xs), xs); };
// take :: Int -> [a] -> [a] // take :: Int -> String -> String const take = (n, xs) => xs.constructor.constructor.name !== 'GeneratorFunction' ? ( xs.slice(0, n) ) : [].concat.apply([], Array.from({ length: n }, () => { const x = xs.next(); return x.done ? [] : [x.value]; }));
// unlines :: [String] -> String const unlines = xs => xs.join('\n');
// Use of `take` and `length` here allows zipping with non-finite lists // i.e. generators like cycle, repeat, iterate.
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] const zipWith = (f, xs, ys) => { const lng = Math.min(length(xs), length(ys)), as = take(lng, xs), bs = take(lng, ys); return Array.from({ length: lng }, (_, i) => f(as[i], bs[i], i)); };
// MAIN --- const strTree = main(); return ( // console.log(strTree), strTree );
})();</lang>
- Output:
alpha │ ├─ beta │ │ │ ├─ gamma │ │ │ ├─ epsilon │ │ │ └─ eta │ ├─ iota │ │ │ ├─ kappa │ │ │ ├─ mu │ │ │ └─ xi │ ├─ pi │ │ │ ├─ rho │ │ │ ├─ tau │ │ │ └─ phi │ └─ psi
Julia
Run from Julia REPL. <lang Julia>using Gadfly, LightGraphs, GraphPlot
gx = kronecker(5, 12, 0.57, 0.19, 0.19) gplot(gx) </lang>
Kotlin
<lang scala>// version 1.2.0
import java.util.Random
class Stem(var str: String? = null, var next: Stem? = null)
const val SDOWN = " |" const val SLAST = " `" const val SNONE = " "
val rand = Random()
fun tree(root: Int, head: Stem?) {
val col = Stem() var head2 = head var tail = head while (tail != null) { print(tail.str) if (tail.next == null) break tail = tail.next } println("--$root") if (root <= 1) return if (tail != null && tail.str == SLAST) tail.str = SNONE if (tail == null) { head2 = col tail = head2 } else { tail.next = col } var root2 = root while (root2 != 0) { // make a tree by doing something random val r = 1 + rand.nextInt(root2) root2 -= r col.str = if (root2 != 0) SDOWN else SLAST tree(r, head2) } tail.next = null
}
fun main(args: Array<String>) {
val n = 8 tree(n, null)
}</lang>
Sample output (unlike the C entry, should be different each time it's run):
--8 |--7 | |--6 | | |--5 | | | |--3 | | | | |--2 | | | | | |--1 | | | | | `--1 | | | | `--1 | | | `--2 | | | `--2 | | | |--1 | | | `--1 | | `--1 | `--1 `--1
Lingo
<lang lingo>-- parent script "TreeItem" -- (minimal implementation with direct property access)
property name property children
on new (me, itemName)
me.name = itemName me.children = [] return me
end
on addChild (me, child)
me.children.add(child)
end
-- print a tree on printTree (me, treeItem, indent)
if voidP(treeItem) then treeItem = me if voidP(indent) then indent = "" put indent&treeItem.name repeat with c in treeItem.children me.printTree(c, indent&" ") end repeat
end</lang> Usage: <lang lingo>-- create a tree root = script("TreeItem").new("root") a = script("TreeItem").new("a") root.addChild(a) b = script("TreeItem").new("b") root.addChild(b) a1 = script("TreeItem").new("a1") a.addChild(a1) a11 = script("TreeItem").new("a11") a1.addChild(a11) a12 = script("TreeItem").new("a12") a1.addChild(a12) b1 = script("TreeItem").new("b1") b.addChild(b1)
-- print the tree root.printTree()</lang>
- Output:
-- "root" -- " a" -- " a1" -- " a11" -- " a12" -- " b" -- " b1"
Mathematica
Tree graph
Make a tree graph. In Mathematica, \[DirectedEdge] will appear as an arrow in the code.
<lang 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"]</lang>
Show the syntactical structure of the above code. Defer is added to impede TreeGraph from becoming a graphical object.
<lang Mathematica>TreeForm[Defer@
TreeGraph[{1 \[DirectedEdge] 2, 1 \[DirectedEdge] 3, 2 \[DirectedEdge] 4, 2 \[DirectedEdge] 5, 3 \[DirectedEdge] 6, 4 \[DirectedEdge] 7}, VertexLabels -> "Name"]]</lang>
Opener view
Here's another way to display a tree. The triangles open/close when clicked on.
<lang 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]</lang>
Maxima
<lang maxima>load(graphs)$
g: random_tree(10)$
is_tree(g); true
draw_graph(g)$</lang>
Nim
<lang nim>import strutils
type
Node[T] = ref TNode[T] TNode[T] = object data: T left, right: Node[T]
proc n[T](data: T; left, right: Node[T] = nil): Node[T] =
Node[T](data: data, left: left, right: right)
proc indent[T](n: Node[T]): seq[string] =
if n == nil: return @["-- (null)"]
result = @["--" & $n.data]
for a in indent n.left: result.add " |" & a
let r = indent n.right result.add " `" & r[0] for a in r[1..r.high]: result.add " " & a
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")</lang>
Perl
<lang perl>#!/usr/bin/perl use warnings; use strict; use utf8; use open OUT => ':utf8', ':std';
sub parse {
my ($tree) = shift; if (my ($root, $children) = $tree =~ /^(.+?)\((.*)\)$/) {
my $depth = 0; for my $pos (0 .. length($children) - 1) { my $char = \substr $children, $pos, 1; if (0 == $depth and ',' eq $$char) { $$char = "\x0"; } elsif ('(' eq $$char) { $depth++; } elsif (')' eq $$char) { $depth--; } } return($root, [map parse($_), split /\x0/, $children]);
} else { # Leaf. return $tree; }
}
sub output {
my ($parsed, $prefix) = @_; my $is_root = not defined $prefix; $prefix //= ' '; while (my $member = shift @$parsed) { my $last = !@$parsed || (1 == @$parsed and ref $parsed->[0]); unless ($is_root) { substr $prefix, -3, 1, ' '; substr($prefix, -4, 1) =~ s/├/│/; substr $prefix, -2, 1, ref $member ? ' ' : '└' if $last; }
if (ref $member) { output($member, $prefix . '├─'); } else { print $prefix, $member, "\n"; } }
}
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);</lang>
- Output:
a ├─b0 │ ├─c1 │ ├─c2 │ │ └─d │ │ ├─ef │ │ └─gh │ └─c3 │ ├─i1 │ ├─i2 │ ├─i3 │ │ └─jj │ └─i4 │ ├─kk │ └─m └─b1 ├─C1 ├─C2 │ ├─D1 │ │ └─E │ ├─D2 │ └─D3 └─C3
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>
- Output:
root ├─a │ └─a1 │ └─a11 └─b ├─b1 │ └─b11 ├─b2 └─b3
Phix
<lang Phix>function rand_tree(integer low, integer high)
for i=1 to 2 do integer v = rand(high-low+1)-1+low if v!=low and v!=high then return {v,rand_tree(low,v),rand_tree(v,high)} end if end for return 0
end function
object tree = rand_tree(0,20) -- (can be 0, ~1% chance)
constant Horizontal = #C4,
Horizontals = "\#C4", TopLeft = #DA, Vertical = #B3, BtmLeft = #C0
procedure visualise_tree(object tree, string root=Horizontals)
if atom(tree) then puts(1,"<empty>\n") else object {v,l,r} = tree integer g = root[$] if sequence(l) then root[$] = iff(g=TopLeft or g=Horizontal?' ':Vertical) visualise_tree(l,root&TopLeft) end if root[$] = g puts(1,root) ?v if sequence(r) then root[$] = iff(g=TopLeft?Vertical:' ') visualise_tree(r,root&BtmLeft) end if end if
end procedure
visualise_tree(tree)</lang>
- Output:
┌3 │└4 │ └5 ┌7 ┌9 │└10 │ └11 ─12 │ ┌13 │┌14 └15 │ ┌16 │┌17 ││└18 └19
A much simpler but less aesthetically pleasing way is just <lang Phix>pp(tree,{pp_Nest,10})</lang>
- Output:
{1, 0, {5, 0, {9, {8, {6, 0, 0}, 0}, 0}}}
PicoLisp
'view' is a built-in function in PicoLisp.
<lang PicoLisp>(view '(1 (2 (3 (4) (5) (6 (7))) (8 (9)) (10)) (11 (12) (13))))</lang>
Output:
+-- 1 | +---+-- 2 | | | +---+-- 3 | | | | | +---+-- 4 | | | | | +---+-- 5 | | | | | +---+-- 6 | | | | | +---+-- 7 | | | +---+-- 8 | | | | | +---+-- 9 | | | +---+-- 10 | +---+-- 11 | +---+-- 12 | +---+-- 13
Prolog
XPCE
XPCE is the SWI-Prolog native GUI library. <lang prolog>% direction may be horizontal/vertical/list display_tree(Direction) :- sformat(A, 'Display tree ~w', [Direction]), new(D, window(A)), send(D, size, size(350,200)), new(T, tree(text('Root'))), send(T, neighbour_gap, 10), new(S1, node(text('Child1'))), new(S2, node(text('Child2'))), send_list(T, son,[S1,S2]), new(S11, node(text('Grandchild1'))), new(S12, node(text('Grandchild2'))), send_list(S1, son, [S11, S12]), new(S21, node(text('Grandchild3'))), new(S22, node(text('Grandchild4'))), send_list(S2, son, [S21, S22]), send(T, direction, Direction), send(D, display, T), send(D, open). </lang>
Python
Library module
Python has the pprint module for pretty-printing data.
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: <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. >>> help('pprint.pprint') Help on function pprint in pprint:
pprint.pprint = pprint(object, stream=None, indent=1, width=80, depth=None)
Pretty-print a Python object to a stream [default is sys.stdout].
>>> from pprint import pprint >>> for tree in [ (1, 2, 3, 4, 5, 6, 7, 8), (1, (( 2, 3 ), (4, (5, ((6, 7), 8))))), ((((1, 2), 3), 4), 5, 6, 7, 8) ]: print("\nTree %r can be pprint'd as:" % (tree, )) pprint(tree, indent=1, width=1)
Tree (1, 2, 3, 4, 5, 6, 7, 8) can be pprint'd as: (1,
2, 3, 4, 5, 6, 7, 8)
Tree (1, ((2, 3), (4, (5, ((6, 7), 8))))) can be pprint'd as: (1,
((2, 3), (4, (5, ((6, 7), 8)))))
Tree ((((1, 2), 3), 4), 5, 6, 7, 8) can be pprint'd as: ((((1,
2), 3), 4), 5, 6, 7, 8)
>>> </lang>
pprint (and print), prints Pythons standard container types in a format that is valid python so Python could parse its output: <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',
('b0', ('c1', 'c2', ('d', ('ef', 'gh')), 'c3', ('i1', 'i2', 'i3', 'jj', 'i4', ('kk', 'm'))), 'b1', ('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3')))
>>> copypasteoutput = ('a', ... ('b0', ... ('c1', ... 'c2', ... ('d', ... ('ef', ... 'gh')), ... 'c3', ... ('i1', ... 'i2', ... 'i3', ... 'jj', ... 'i4', ... ('kk', ... 'm'))), ... 'b1', ... ('C1', ... 'C2', ... ('D1', ... 'E', ... 'D2', ... 'D3'), ... 'C3'))) >>> tree == copypasteoutput True >>> </lang>
pprints width parameter allows it to fold some structure to better fit the page: <lang python>>>> pprint(tree, width=60) ('a',
('b0', ('c1', 'c2', ('d', ('ef', 'gh')), 'c3', ('i1', 'i2', 'i3', 'jj', 'i4', ('kk', 'm'))), 'b1', ('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3')))
>>> </lang>
pprint works with with a mix of nested container types. Here we create a tree from both lists and tuples: <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'))] >>> pprint(mixedtree, width=1) ['a',
('b0', ('c1', 'c2', ['d', ('ef', 'gh')], 'c3', ('i1', 'i2', 'i3', 'jj', 'i4', ['kk', 'm'])), 'b1', ('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3'))]
>>> pprint(mixedtree, width=60) ['a',
('b0', ('c1', 'c2', ['d', ('ef', 'gh')], 'c3', ('i1', 'i2', 'i3', 'jj', 'i4', ['kk', 'm'])), 'b1', ('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3'))]
>>> </lang>
Functional composition
Using the same tree structure (including tree node constructor and accessors) as in the Tree Traversal task:
<lang python>from itertools import (repeat, starmap) from operator import (add)
- drawTree :: Tree String -> String
def drawTree(tree):
return '\n'.join(draw(tree))
- draw :: Tree String -> [String]
def draw(node):
def shift(first, other, xs): return list(starmap( add, zip( [first] + list( repeat(other, len(xs) - 1) ), xs ) ))
def drawSubTrees(xs): return ( ( ['|'] + shift( '├─ ', '│ ', draw(xs[0]) ) + drawSubTrees(xs[1:]) ) if 1 < len(xs) else ['|'] + shift( '└─ ', ' ', draw(xs[0]) ) ) if xs else []
return (str(root(node))).splitlines() + ( drawSubTrees(nest(node)) )
- TEST ----------------------------------------------------
- main :: IO ()
def main():
# tree :: Tree Int tree = Node(1)([ Node(2)([ Node(4)([ Node(7)([]) ]), Node(5)([]) ]), Node(3)([ Node(6)([ Node(8)([]), Node(9)([]) ]) ]) ])
print(drawTree(tree))
- GENERIC -------------------------------------------------
- Node :: a -> [Tree a] -> Tree a
def Node(v):
return lambda xs: {'type': 'Node', 'root': v, 'nest': xs}
- nest :: Tree a -> [Tree a]
def nest(tree):
Accessor function for children of tree node return tree['nest'] if 'nest' in tree else None
- root :: Dict -> a
def root(tree):
Accessor function for data of tree node return tree['root'] if 'root' in tree else None
- MAIN ---
if __name__ == '__main__':
main()</lang>
- Output:
1 | ├─ 2 │ | │ ├─ 4 │ │ | │ │ └─ 7 │ | │ └─ 5 | └─ 3 | └─ 6 | ├─ 8 | └─ 9
Racket
<lang Racket>
- lang racket/base
(define (visualize t0)
(let loop ([t t0] [last? #t] [indent '()]) (define (I mid last) (cond [(eq? t t0) ""] [last? mid] [else last])) (for-each display (reverse indent)) (unless (eq? t t0) (printf "|\n")) (for-each display (reverse indent)) (printf "~a~a\n" (I "\\-" "+-") (car t)) (for ([s (cdr t)] [n (in-range (- (length t) 2) -1 -1)]) (loop s (zero? n) (cons (I " " "| ") indent)))))
(visualize '(1 (2 (3 (4) (5) (6 (7))) (8 (9)) (10)) (11 (12) (13)))) </lang>
Output:
1 | +-2 | | | +-3 | | | | | +-4 | | | | | +-5 | | | | | \-6 | | | | | \-7 | | | +-8 | | | | | \-9 | | | \-10 | \-11 | +-12 | \-13
REXX
<lang rexx>/* REXX ***************************************************************
- 10.05.2014 Walter Pachl using the tree and the output format of C
- /
Call mktree Say node.1.0name Call tt 1, Exit
tt: Procedure Expose node. /**********************************************************************
- show a subtree (recursively)
- /
Parse Arg k,st Do i=1 To node.k.0 If i=node.k.0 Then s='`--' Else s='|--' c=node.k.i If st<> Then st=left(st,length(st)-2)' ' st=changestr('` ',st,' ') Say st||s||node.c.0name Call tt c,st||s End Return
Exit
mktree: Procedure Expose node. root /**********************************************************************
- build the tree according to the task
- /
node.=0 r=mknode('R'); a=mknode('A'); Call attchild a,r b=mknode('B'); Call attchild b,a c=mknode('C'); Call attchild c,a d=mknode('D'); Call attchild d,b e=mknode('E'); Call attchild e,b f=mknode('F'); Call attchild f,b g=mknode('G'); Call attchild g,b h=mknode('H'); Call attchild h,d i=mknode('I'); Call attchild i,h j=mknode('J'); Call attchild j,i k=mknode('K'); Call attchild k,j l=mknode('L'); Call attchild l,j m=mknode('M'); Call attchild m,e n=mknode('N'); Call attchild n,e Return
mknode: Procedure Expose node. /**********************************************************************
- create a new node
- /
Parse Arg name z=node.0+1 node.z.0name=name node.0=z Return z /* number of the node just created */
attchild: Procedure Expose node. /**********************************************************************
- make a the next child of father
- /
Parse Arg a,father node.a.0father=father z=node.father.0+1 node.father.z=a node.father.0=z node.a.0level=node.father.0level+1 Return
</lang>
- Output:
R `--A |--B | |--D | | `--H | | `--I | | `--J | | |--K | | `--L | |--E | | |--M | | `--N | |--F | `--G `--C
Ruby
Modifying Tree_traversal#Ruby by adding somewhere after the line <lang Ruby> root = BinaryTreeNode.from_array [1, [2, [4, 7], [5]], [3, [6, [8], [9]]]] </lang> the lines <lang Ruby> require 'pp' pp root </lang> will produce:
- Output:
#<BinaryTreeNode:0x804f854 @left= #<BinaryTreeNode:0x804fad8 @left=#<BinaryTreeNode:0x804fc28 @left=nil, @right=nil, @value=7>, @right=nil, @value=4>, @right=#<BinaryTreeNode:0x804f9c0 @left=nil, @right=nil, @value=5>, @value=2>, @right= #<BinaryTreeNode:0x804f074 @left= #<BinaryTreeNode:0x804f218 @left=#<BinaryTreeNode:0x804f544 @left=nil, @right=nil, @value=8>, @right=#<BinaryTreeNode:0x804f384 @left=nil, @right=nil, @value=9>, @value=6>, @right=nil, @value=3>, @value=1>
<lang Ruby> def ptree(tree,indent=" ")
case tree when Array head,*tail=tree ptree(head,indent) s=tail.size-1 tail.each_with_index { |tree1,i| ptree(tree1,"#{indent}#{((i==s) ? ' ':'|')} ") } else puts(indent.gsub(/\s\s$/,"--").gsub(/ --$/,"\\--")+tree.to_s) end
end ptree [1,2,3,[4,5,6,[7,8,9]],3,[22,33]] </lang> will produce:
- Output:
--1 |--2 |--3 |--4 | |--5 | |--6 | \--7 | |--8 | \--9 |--3 \--22 \--33
Rust
Console visualization of binary trees translated from parts of the C AVL tree solution. <lang Rust> extern crate rustc_serialize; extern crate term_painter;
use rustc_serialize::json; use std::fmt::{Debug, Display, Formatter, Result}; use term_painter::ToStyle; use term_painter::Color::*;
type NodePtr = Option<usize>;
- [derive(Debug, PartialEq, Clone, Copy)]
enum Side {
Left, Right, Up,
}
- [derive(Debug, PartialEq, Clone, Copy)]
enum DisplayElement {
TrunkSpace, SpaceLeft, SpaceRight, SpaceSpace, Root,
}
impl DisplayElement {
fn string(&self) -> String { match *self { DisplayElement::TrunkSpace => " │ ".to_string(), DisplayElement::SpaceRight => " ┌───".to_string(), DisplayElement::SpaceLeft => " └───".to_string(), DisplayElement::SpaceSpace => " ".to_string(), DisplayElement::Root => "├──".to_string(), } }
}
- [derive(Debug, Clone, Copy, RustcDecodable, RustcEncodable)]
struct Node<K, V> {
key: K, value: V, left: NodePtr, right: NodePtr, up: NodePtr,
}
impl<K: Ord + Copy, V: Copy> Node<K, V> {
pub fn get_ptr(&self, side: Side) -> NodePtr { match side { Side::Up => self.up, Side::Left => self.left, _ => self.right, } }
}
- [derive(Debug, RustcDecodable, RustcEncodable)]
struct Tree<K, V> {
root: NodePtr, store: Vec<Node<K, V>>,
}
impl<K: Ord + Copy + Debug + Display, V: Debug + Copy + Display> Tree<K, V> {
pub fn get_node(&self, np: NodePtr) -> Node<K, V> { assert!(np.is_some()); self.store[np.unwrap()] }
pub fn get_pointer(&self, np: NodePtr, side: Side) -> NodePtr { assert!(np.is_some()); self.store[np.unwrap()].get_ptr(side) }
// Prints the tree with root p. The idea is to do an in-order traversal // (reverse in-order in this case, where right is on top), and print nodes as they // are visited, one per line. Each invocation of display() gets its own copy // of the display element vector e, which is grown with either whitespace or // a trunk element, then modified in its last and possibly second-to-last // characters in context. fn display(&self, p: NodePtr, side: Side, e: &Vec<DisplayElement>, f: &mut Formatter) { if p.is_none() { return; }
let mut elems = e.clone(); let node = self.get_node(p); let mut tail = DisplayElement::SpaceSpace; if node.up != self.root { // If the direction is switching, I need the trunk element to appear in the lines // printed before that node is visited. if side == Side::Left && node.right.is_some() { elems.push(DisplayElement::TrunkSpace); } else { elems.push(DisplayElement::SpaceSpace); } } let hindex = elems.len() - 1; self.display(node.right, Side::Right, &elems, f);
if p == self.root { elems[hindex] = DisplayElement::Root; tail = DisplayElement::TrunkSpace; } else if side == Side::Right { // Right subtree finished elems[hindex] = DisplayElement::SpaceRight; // Prepare trunk element in case there is a left subtree tail = DisplayElement::TrunkSpace; } else if side == Side::Left { elems[hindex] = DisplayElement::SpaceLeft; let parent = self.get_node(node.up); if parent.up.is_some() && self.get_pointer(parent.up, Side::Right) == node.up { // Direction switched, need trunk element starting with this node/line elems[hindex - 1] = DisplayElement::TrunkSpace; } }
// Visit node => print accumulated elements. Each node gets a line and each line gets a // node. for e in elems.clone() { let _ = write!(f, "{}", e.string()); } let _ = write!(f, "{key:>width$} ", key = Green.bold().paint(node.key), width = 2); let _ = write!(f, "{value:>width$}\n", value = Blue.bold().paint(format!("{:.*}", 2, node.value)), width = 4);
// Overwrite last element before continuing traversal elems[hindex] = tail;
self.display(node.left, Side::Left, &elems, f); }
}
impl<K: Ord + Copy + Debug + Display, V: Debug + Copy + Display> Display for Tree<K, V> {
fn fmt(&self, f: &mut Formatter) -> Result { if self.root.is_none() { write!(f, "[empty]") } else { let mut v: Vec<DisplayElement> = Vec::new(); self.display(self.root, Side::Up, &mut v, f); Ok(()) } }
}
/// Decodes and prints a previously generated tree. fn main() {
let encoded = r#"{"root":0,"store":[{"key":0,"value":0.45,"left":1,"right":3, "up":null},{"key":-8,"value":-0.94,"left":7,"right":2,"up":0}, {"key":-1, "value":0.15,"left":8,"right":null,"up":1},{"key":7, "value":-0.29,"left":4, "right":9,"up":0},{"key":5,"value":0.80,"left":5,"right":null,"up":3}, {"key":4,"value":-0.85,"left":6,"right":null,"up":4},{"key":3,"value":-0.46, "left":null,"right":null,"up":5},{"key":-10,"value":-0.85,"left":null, "right":13,"up":1},{"key":-6,"value":-0.42,"left":null,"right":10,"up":2}, {"key":9,"value":0.63,"left":12,"right":null,"up":3},{"key":-3,"value":-0.83, "left":null,"right":11,"up":8},{"key":-2,"value":0.75,"left":null,"right":null, "up":10},{"key":8,"value":-0.48,"left":null,"right":null,"up":9},{"key":-9, "value":0.53,"left":null,"right":null,"up":7}]}"#; let tree: Tree<i32, f32> = json::decode(&encoded).unwrap(); println!("{}", tree);
} </lang>
- Output:
Sidef
<lang ruby>func visualize_tree(tree, label, children,
indent = , mids = ['├─', '│ '], ends = ['└─', ' '],
) {
func visit(node, pre) { gather { take(pre[0] + label(node)) var chldn = children(node) var end = chldn.end chldn.each_kv { |i, child| if (i == end) { take(visit(child, [pre[1]] ~X+ ends)) } else { take(visit(child, [pre[1]] ~X+ mids)) } } } } visit(tree, [indent] * 2)
}
var tree = 'root':['a':['a1':['a11':[]]],'b':['b1':['b11':[]],'b2':[],'b3':[]]] say visualize_tree(tree, { .first }, { .second }).flatten.join("\n")</lang>
- Output:
root ├─a │ └─a1 │ └─a11 └─b ├─b1 │ └─b11 ├─b2 └─b3
Tcl
<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>
- Output:
└─root ├─a │ ├─d │ └─e │ └─f ├─b └─c
Yabasic
<lang Yabasic>clear screen
dim colore$(1)
maxCol = token("white yellow cyan green red", colore$())
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]]]]")
sub showTree(n, A$)
local i, c$ static co c$ = left$(A$, 1) if c$ = "" return switch c$ case "[": co = co + 1 : showTree(n + 1, right$(A$, len(A$) - 1)) break case "]": co = co - 1 : showTree(n - 1, right$(A$, len(A$) - 1)) break default: for i = 2 to n print " "; next i co = max(min(co, maxCol), 1) print color(colore$(co)) "\xc0-", c$ showTree(n, right$(A$, len(A$) - 1)) break end switch
end sub </lang>
zkl
In zkl, the Vault is a global object store object (aka thread safe dictionary). Basically a tiny file system for objects. It has a "dir" method to display the contents
- Output:
:Vault.dir() ... Compiler Asm Compiler Dictionary Exception Test UnitTester foo bar ...
It does this with data that looks like: L("Network.TCPServerSocket","File","ZKLShell.Granny","Int","startup","Utils.Inspector","Thread.Straw","Ref","Utils.Argh" ...) <lang zkl>fcn vaultDir(out=Console){
const INDENT=" "; space:=""; lastPath:=L(); foreach fullname in (TheVault.BaseClass.contents.sort()){ path:=fullname.split("."); name:=path.pop(); if(lastPath==path) out.writeln(space,name); else{
n:=0; p:=path.copy(); try{ while(path[0]==lastPath[0]) { n+=1; path.pop(0); lastPath.pop(0); } }catch{} space=INDENT*n; foreach dir in (path){ out.writeln(space,dir); space+=INDENT; } out.writeln(space,name); lastPath=p;
} } "" // so startup has something to display
} </lang>