Visualize a tree: Difference between revisions

From Rosetta Code
Content added Content deleted
(draft utility task)
 
(Add Python's pprint.pprint)
Line 3: Line 3:


'''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.
'''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.

=={{header|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>

Revision as of 09:06, 1 September 2012

Visualize a tree is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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>