# Tree structure with parametric type

**URL:** https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300
**Category:** General Usage
**Created:** [February 24, 2018, 5:33pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300 "2018-02-24T17:33:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)
#### Post date: [February 24, 2018, 5:33pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300/1 "2018-02-24T17:33:34Z")

</div>

Hello,

I want to build a tree from

```julia
abstract type ABC end

struct A{x <: ABC} <: ABC
    a::Vector{x}
end

struct B <: ABC
    a::Vector{Float64}
end

```

Then I can build the two first layers like this

```julia
low1 = [B([0.1, 0.2]), B([0.3,0.4])]
low2 = [B([0.5, 0.6]), B([0.7,0.8])]
mid1 = A{B}(low1)
mid2 = A{B}(low2)

```

What is the best way to create the next layer ?

```julia
top = A{A{B}}([mid1, mid2])

```

or

```julia
top = A{A}([mid1, mid2])

```

Is there an efficiency gain with the first way or both are equivalent and I should use the second one for simplicity?

Thanks,

Max

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [February 24, 2018, 8:36pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300/2 "2018-02-24T20:36:06Z")

</div>

Both are possible (and different), but neither is ideal, in my opinion. The issue with `A{A}(...)` is that `A` is a non-concrete type, so the outer layer will contain a `Vector{A}` whose elements are non-concrete. That _may_ harm performance.

`A{A{B}}` does fix that particular issue, but it’s starting to feel like an abuse of the type system. You definitely _can_ do this, but it means, for example, compiling new native code for every layer in the tree (because every layer is a different type).

In [RegionTrees.jl](https://github.com/rdeits/RegionTrees.jl/blob/master/src/cell.jl#L1) I essentially make every node _both_ and A and a B. That is, every node can contain data and/or a vector of children. I use `Nullable`s to keep everything concretely typed, and it seems to work pretty well.

Another option would be to use a small Union:

```julia
struct A
  children::Vector{Union{A, B}}
end

struct B
  data::Vector{Float64}
end

```

In particular, this may end up performing better on Julia v0.7 (nightly) than on the current v0.6 release.

But, really, the most important thing is: there is no single answer to efficiency questions like this. The only way to know for sure what the right approach is is to benchmark it.

---

<div class="post-metadata">

### Author: ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)
#### Post date: [February 25, 2018, 5:07pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300/3 "2018-02-25T17:07:31Z")

</div>

So, if I understand well, you suggest something like

```julia
struct A
   data::Nullable{Vector{Float64}}
   children::Nullable{Vector{A}}
end

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [February 25, 2018, 5:50pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300/4 "2018-02-25T17:50:01Z")

</div>

Yeah, that seems like a good option. But I’d definitely try out a few different structures and see which performs well in practice.

---

<div class="post-metadata">

### Author: ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)
#### Post date: [February 25, 2018, 6:04pm UTC](https://discourse.julialang.org/t/tree-structure-with-parametric-type/9300/5 "2018-02-25T18:04:34Z")

</div>

Good, thank for the advice.
