# `Visualize.Layout.Tree`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/layout/tree.ex#L1)

Tree layout algorithm for hierarchical node-link diagrams.

Implements the Reingold–Tilford "tidy" algorithm as refined by Buchheim, Jünger and
Leipert (linear time, threads and accumulated shifts), ported from d3-hierarchy's
`tree`: children of one parent are placed left to right with the gap given by
`separation`, every parent is centered over its children, and subtrees are shifted so
that nodes at the same depth never overlap while adjacent subtrees keep their
separation.

## Examples

    # Create hierarchy
    data = %{
      name: "root",
      children: [
        %{name: "A", children: [%{name: "A1"}, %{name: "A2"}]},
        %{name: "B"}
      ]
    }

    root = Visualize.Layout.Hierarchy.new(data)

    # Apply tree layout
    tree = Visualize.Layout.Tree.new()
      |> Visualize.Layout.Tree.size([400, 200])

    positioned = Visualize.Layout.Tree.generate(tree, root)

    # Each node now has x, y coordinates
    Visualize.Layout.Hierarchy.each(positioned, fn node ->
      IO.puts("#{node.data.name}: (#{node.x}, #{node.y})")
    end)

# `t`

```elixir
@type t() :: %Visualize.Layout.Tree{
  node_size: {number(), number()} | nil,
  separation:
    (Visualize.Layout.Hierarchy.t(), Visualize.Layout.Hierarchy.t() -&gt; number())
    | nil,
  size: {number(), number()} | nil
}
```

# `generate`

```elixir
@spec generate(t(), Visualize.Layout.Hierarchy.t()) :: Visualize.Layout.Hierarchy.t()
```

Generates the tree layout, positioning all nodes.

Without `size` or `node_size` the coordinates are raw: `x` in separation units with
the root at 0, `y` the depth.

# `new`

```elixir
@spec new() :: t()
```

Creates a new tree layout

# `node_size`

```elixir
@spec node_size(t(), [number()] | {number(), number()}) :: t()
```

Sets the fixed node size.

Unlike size/2, this sets a fixed spacing between nodes rather
than scaling to fit a container.

# `separation`

```elixir
@spec separation(t(), (Visualize.Layout.Hierarchy.t(),
                 Visualize.Layout.Hierarchy.t() -&gt;
                   number())) ::
  t()
```

Sets the separation function between nodes.

The function receives two nodes and returns the desired separation.
Default is 1 for siblings, 2 for non-siblings.

# `size`

```elixir
@spec size(t(), [number()] | {number(), number()}) :: t()
```

Sets the size of the layout.

The tree will be scaled to fit within [width, height].

---

*Consult [api-reference.md](api-reference.md) for complete listing*
