tree utilities
This commit is contained in:
parent
6eb2ec154b
commit
752a9e2808
@ -13,6 +13,9 @@ Tree(x::T, xs::Tree{T}...) where T = Tree{T}(x, xs...)
|
|||||||
AbstractTrees.children(t::Tree) = t.children
|
AbstractTrees.children(t::Tree) = t.children
|
||||||
AbstractTrees.printnode(io::IO, t::Tree) = show(io, t.value)
|
AbstractTrees.printnode(io::IO, t::Tree) = show(io, t.value)
|
||||||
|
|
||||||
|
Base.show(io::IO, t::Type{Tree}) = print(io, "Tree")
|
||||||
|
Base.show(io::IO, t::Type{Tree{T}}) where T = print(io, "Tree{", T, "}")
|
||||||
|
|
||||||
function Base.show(io::IO, t::Tree)
|
function Base.show(io::IO, t::Tree)
|
||||||
println(io, typeof(t))
|
println(io, typeof(t))
|
||||||
print_tree(io, t)
|
print_tree(io, t)
|
||||||
@ -24,3 +27,16 @@ using Juno
|
|||||||
render(t) = Juno.Tree(t.value, render.(t.children))
|
render(t) = Juno.Tree(t.value, render.(t.children))
|
||||||
Juno.Tree(typeof(t), [render(t)])
|
Juno.Tree(typeof(t), [render(t)])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Base.getindex(t::Tree, i::Integer) = t.children[i]
|
||||||
|
Base.getindex(t::Tree, i::Integer, is::Integer...) = t[i][is...]
|
||||||
|
|
||||||
|
# Utilities
|
||||||
|
|
||||||
|
isleaf(t) = isempty(children(t))
|
||||||
|
|
||||||
|
leaves(xs::Tree) = map(x -> x.value, Leaves(xs))
|
||||||
|
|
||||||
|
Base.map(f, t::Tree, ts::Tree...) =
|
||||||
|
Tree{Any}(f(map(t -> t.value, (t, ts...))...),
|
||||||
|
[map(f, chs...) for chs in zip(map(t -> t.children, (t, ts...))...)]...)
|
||||||
|
Loading…
Reference in New Issue
Block a user