Visualize a tree: Difference between revisions

→‎Functional Python: Sub-labels for two plain text version, for easier finding and editing. + Minor edits.
(→‎Functional Python: Sub-labels for two plain text version, for easier finding and editing. + Minor edits.)
Line 3,025:
 
===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:
 
Line 3,060:
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 3,096:
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 3,229 ⟶ 3,227:
])
 
print((
print('Fully compacted (parents not centered):')
print( '\n\n'.join([
print( 'Fully compacted (parents not all centered):'),
drawTree2(True)(False)(
tree1drawTree2(True)(False)(
) tree1
)),
print( '\nExpandedExpanded with vertically centered parents:\n'),
 
drawTree2(TrueFalse)(False)(
print('\nExpanded with vertically centered parents:\n')
tree2
print((
drawTree2(False )(False)(,
print( '\nCenteredCentered parents with nodeless lines pruned out:\n'),
tree2
drawTree2(False)(True)(
tree2
))
)
 
) ])
print('\nCentered parents with nodeless lines pruned out:\n')
print(()
drawTree2(False)(True)(
tree2
)
))
 
 
Line 3,371 ⟶ 3,365:
main()</lang>
{{Out}}
<pre>Fully compacted (parents not all centered):
 
┌ 4 ─ 7
┌ 2 ┴ 5
Line 3,410 ⟶ 3,405:
└─ Lambda </pre>
 
====Simple decorated-outline tree====
Or using the same data structures, but generating a simpler (decorated outline) visualisation:
{{Works with|Python|3}}
<lang python>'''Visualize a tree'''
9,659

edits