# Hello everyone, I'm trying to write a function that returns an array with all path

**URL:** <https://discourse.julialang.org/t/hello-everyone-im-trying-to-write-a-function-that-returns-an-array-with-all-path/57296>\
**Category:** General Usage\
**Created:** [March 16, 2021, 12:30pm UTC](https://discourse.julialang.org/t/hello-everyone-im-trying-to-write-a-function-that-returns-an-array-with-all-path/57296 "2021-03-16T12:30:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 16, 2021, 12:30pm UTC](https://discourse.julialang.org/t/hello-everyone-im-trying-to-write-a-function-that-returns-an-array-with-all-path/57296/1 "2021-03-16T12:30:25Z")

</div>

Hello everyone,  
I’m trying to write a function that returns an array with all paths from root to leaves in a binary tree, where each path is an array of symbols.  
The tree struct is defined like this:

```julia
abstract type Node end

struct Branch_Node &lt;: Node
    left::Node
    right::Node
end

struct Leaf_Node &lt;: Node
end

```

So for this tree

```julia
tree = Branch_Node(Branch_Node(Branch_Node(Leaf_Node(), Leaf_Node()), Leaf_Node()),Branch_Node(Leaf_Node(), Leaf_Node()))

```

I want the function to return

```julia
[[:left, :left, :left], [:left, :left, :right], [:left, :right], [:right, :left], [:right, :right]]

```

I can write a function that returns a nested array like this:

```julia
function get_path_to_leaf(node::Leaf_Node)
    return
end
function get_path_to_leaf(node::Branch_Node)
    return [[:left, get_path_to_leaf(node.left)], [:right, get_path_to_leaf(node.right)]]
end

```

But I can’t figure out how to unfold the paths into flat arrays. I though something like this:

```julia
function get_path_to_leaf(node::Leaf_Node)
    return []
end

function get_path_to_leaf(node::Branch_Node)
    return [[append!([:left], i) for i in get_path_to_leaf(node.left)], [append!([:right], i) for i in get_path_to_leaf(node.right)]]
end

```

would do it, but I get the following error “ERROR: MethodError: Cannot `convert` an object of type Array{Symbol,1} to an object of type Symbol”  
and I’m having no luck debugging it. Sorry for the wall of text, can anyone help?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1615897793015400?thread_ts=1615897793.015400&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [March 16, 2021, 1:32pm UTC](https://discourse.julialang.org/t/hello-everyone-im-trying-to-write-a-function-that-returns-an-array-with-all-path/57296/2 "2021-03-16T13:32:46Z")

</div>

I’m not entirely sure, but I don’t think this will work. Anytime you’d reach the leaf, you’d return an empty collection, which would “propagate” and give you an empty collection in the end. I think for something like this to work, you need to return a _non-empty_ thing with the leaf, even if it’s an 1-element vector containing an empty array of symbols:

```julia
julia> struct Branch_Node <: Node
           left::Node
           right::Node
       end

julia> struct Leaf_Node <: Node
       end

julia> function get_path_to_leaf(node::Leaf_Node)
           return [Symbol[]]
       end
get_path_to_leaf (generic function with 1 method)

julia> function get_path_to_leaf(node::Branch_Node)
           return [[[:left; x] for x in get_path_to_leaf(node.left)]; [[:right; x] for x in get_path_to_leaf(node.right)]]
       end
get_path_to_leaf (generic function with 2 methods)

julia> tree = Branch_Node(Branch_Node(Branch_Node(Leaf_Node(), Leaf_Node()), Leaf_Node()),Branch_Node(Leaf_Node(), Leaf_Node()))
Branch_Node(Branch_Node(Branch_Node(Leaf_Node(), Leaf_Node()), Leaf_Node()), Branch_Node(Leaf_Node(), Leaf_Node()))

julia> get_path_to_leaf(tree)
5-element Vector{Vector{Symbol}}:
 [:left, :left, :left]
 [:left, :left, :right]
 [:left, :right]
 [:right, :left]
 [:right, :right]

```
