58 lines
1.5 KiB
Plaintext
58 lines
1.5 KiB
Plaintext
; nodes are [data left right], use "first" to get data
|
|
|
|
to node.left :node
|
|
if empty? butfirst :node [output []]
|
|
output first butfirst :node
|
|
end
|
|
to node.right :node
|
|
if empty? butfirst :node [output []]
|
|
if empty? butfirst butfirst :node [output []]
|
|
output first butfirst butfirst :node
|
|
end
|
|
to max :a :b
|
|
output ifelse :a > :b [:a] [:b]
|
|
end
|
|
to tree.depth :tree
|
|
if empty? :tree [output 0]
|
|
output 1 + max tree.depth node.left :tree tree.depth node.right :tree
|
|
end
|
|
|
|
to pre.order :tree :action
|
|
if empty? :tree [stop]
|
|
invoke :action first :tree
|
|
pre.order node.left :tree :action
|
|
pre.order node.right :tree :action
|
|
end
|
|
to in.order :tree :action
|
|
if empty? :tree [stop]
|
|
in.order node.left :tree :action
|
|
invoke :action first :tree
|
|
in.order node.right :tree :action
|
|
end
|
|
to post.order :tree :action
|
|
if empty? :tree [stop]
|
|
post.order node.left :tree :action
|
|
post.order node.right :tree :action
|
|
invoke :action first :tree
|
|
end
|
|
to at.depth :n :tree :action
|
|
if empty? :tree [stop]
|
|
ifelse :n = 1 [invoke :action first :tree] [
|
|
at.depth :n-1 node.left :tree :action
|
|
at.depth :n-1 node.right :tree :action
|
|
]
|
|
end
|
|
to level.order :tree :action
|
|
for [i 1 [tree.depth :tree]] [at.depth :i :tree :action]
|
|
end
|
|
|
|
make "tree [1 [2 [4 [7]]
|
|
[5]]
|
|
[3 [6 [8]
|
|
[9]]]]
|
|
|
|
pre.order :tree [(type ? "| |)] (print)
|
|
in.order :tree [(type ? "| |)] (print)
|
|
post.order :tree [(type ? "| |)] (print)
|
|
level.order :tree [(type ? "| |)] (print)
|