# `Macaw.Draw.Layout.Tree`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/layout/tree.ex#L1)

The tidy tree, for node-link diagrams (spec/08 §3.2).

    iex> alias Macaw.Draw.Layout.{Hierarchy, Tree}
    iex> {:ok, root, []} = Hierarchy.stratify(~w(r a b), [nil, "r", "r"], [])
    iex> {:ok, tree} = Tree.new(size: {100, 50})
    iex> tree |> Tree.generate(root) |> Hierarchy.descendants() |> Enum.map(&{&1.x, &1.y})
    [{50.0, 0.0}, {0.0, 50.0}, {100.0, 50.0}]

`generate/2` sets `x` and `y` on every node: `y` the depth, `x` by the Reingold–Tilford
algorithm in the linear-time form of Buchheim, Jünger and Leipert, ported from
d3-hierarchy's `tree`. Children of one parent are placed left to right with the gap
`separation` gives, each parent centered over its first and last child, and no two nodes
at one depth closer than `separation` allows.

Options (§3.1), checked by `new/1`:

| Option | Type | Default |
|---|---|---|
| `size` | `{w, h}` or `[w, h]` of non-negative numbers | `nil` |
| `node_size` | `{dx, dy}` or `[dx, dy]` of positive numbers | `nil` |
| `separation` | function of two nodes answering a non-negative number | siblings 1, otherwise 2 |

`size` and `node_size` are exclusive. Without either, `x` is in separation units with the
root at 0 and `y` in depth levels; `size` normalizes both into the box, `node_size`
multiplies them. This is visualize's `Visualize.Layout.Tree`, its setters replaced by
`new/1` (spec/08 §17).

# `t`

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

# `columns`

```elixir
@spec columns(Macaw.Draw.Layout.Hierarchy.t(), atom() | String.t(), keyword()) ::
  %{required(String.t()) =&gt; [term()]} | Macaw.Draw.Layout.error()
```

The columns of a laid-out tree (§3.5): `:nodes`, one row per node in pre-order (`"row"`,
`"id"`, `"depth"`, `"height"`, `"value"`, `"x"`, `"y"`), or `:links`, one row per
parent-child pair (`"row"` the child's, `"source"`, `"target"`, `"x0"`, `"y0"`, `"x1"`,
`"y1"`, `"path"`) with options `shape:` and `orientation:` (`Macaw.Draw.Layout.Links`).

# `generate`

```elixir
@spec generate(t(), Macaw.Draw.Layout.Hierarchy.t()) ::
  Macaw.Draw.Layout.Hierarchy.t() | Macaw.Draw.Layout.error()
```

Every node of `root` positioned by the tidy tree, then scaled (§3.1, §3.2).

# `new`

```elixir
@spec new() :: {:ok, t()}
```

`new/1` with every option at its default.

# `new`

```elixir
@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()
```

The layout of `opts` (§3.1): `{:ok, tree}`, or `{:error, faults}` with every bad option
at its path, `{[:node_size], {:exclusive, :size}}` when both sizes are given.

---

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