Visualize a tree: Difference between revisions
→Functional Python: Sub-labels for two plain text version, for easier finding and editing. + Minor edits.
m (→{{header|AppleScript}}: (tidying)) |
(→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
# lmrFromStrings :: [String] -> ([String], String, [String])
def lmrFromStrings(xs):
'''
i = len(xs) // 2
return
# stringsFromLMR :: ([String], String, [String]) -> [String]
Line 3,096:
def leftPad(n):
return lambda s: (' ' * n) + s
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('Fully compacted (parents not centered):')▼
drawTree2(True)(False)(▼
)
▲ print('\nExpanded with vertically centered parents:\n')
tree2▼
▲ print((
▲ tree2
drawTree2(False)(True)(
tree2▼
))▼
)▼
▲ print('\nCentered parents with nodeless lines pruned out:\n')
▲ 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====
{{Works with|Python|3}}
<lang python>'''Visualize a tree'''
|