Visualize a tree
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.
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.
Python
Python has the pprint module for pretty-printing data. <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>