/AbstractTrees.jl

Abstract julia interfaces for working with trees

Primary LanguageJuliaOtherNOASSERTION

AbstractTrees

Build Status codecov

A package for dealing with generalized tree-like data structures.

Examples

julia> t = [[1,2], [3,4]];  # AbstractArray and AbstractDict are trees

julia> children(t)
2-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]

julia> getdescendant(t, (2,1))
3

julia> collect(PreOrderDFS(t))  # iterate from root to leaves
7-element Vector{Any}:
  [[1, 2], [3, 4]]
  [1, 2]
 1
 2
  [3, 4]
 3
 4

julia> collect(PostOrderDFS(t))  # iterate from leaves to root
7-element Vector{Any}:
 1
 2
  [1, 2]
 3
 4
  [3, 4]
  [[1, 2], [3, 4]]

julia> collect(Leaves(t))  # iterate over leaves
4-element Vector{Int64}:
 1
 2
 3
 4

julia> struct FloatTree  # make your own trees
        x::Float64
        children::Vector{FloatTree}
    end;

julia> AbstractTrees.children(t::FloatTree) = t.children;

julia> AbstractTrees.nodevalue(t::FloatTree) = t.x;

julia> print_tree(FloatTree(NaN, [FloatTree(Inf, []), FloatTree(-Inf, [])]))
NaN
├─ Inf
└─ -Inf

Related Packages

  • D3Trees.jl provides interactive rendering of large trees using the D3.js javascript package.