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

Hierarchical data structure for tree-based visualizations.

Provides functions to create and manipulate tree data structures
that can be used with various layout algorithms (tree, treemap, pack, etc.).

## Creating a Hierarchy

From nested data:

    data = %{
      name: "root",
      children: [
        %{name: "child1", value: 10},
        %{name: "child2", children: [
          %{name: "grandchild", value: 5}
        ]}
      ]
    }

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

From tabular data with stratify:

    data = [
      %{id: "root", parent: nil},
      %{id: "child1", parent: "root"},
      %{id: "child2", parent: "root"},
      %{id: "grandchild", parent: "child2"}
    ]

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

## Working with Nodes

    # Traverse all nodes
    Visualize.Layout.Hierarchy.each(root, fn node -> IO.inspect(node.data) end)

    # Sum values up the tree
    root = Visualize.Layout.Hierarchy.sum(root, fn d -> d[:value] || 0 end)

    # Count leaves
    root = Visualize.Layout.Hierarchy.count(root)

    # Get all descendants
    nodes = Visualize.Layout.Hierarchy.descendants(root)

    # Get all leaves
    leaves = Visualize.Layout.Hierarchy.leaves(root)

# `t`

```elixir
@type t() :: %Visualize.Layout.Hierarchy{
  children: [t()] | nil,
  data: any(),
  depth: non_neg_integer(),
  height: non_neg_integer(),
  id: any(),
  parent: t() | nil,
  r: number() | nil,
  value: number() | nil,
  x: number() | nil,
  x0: number() | nil,
  x1: number() | nil,
  y: number() | nil,
  y0: number() | nil,
  y1: number() | nil
}
```

# `ancestors`

```elixir
@spec ancestors(t()) :: [t()]
```

Returns all ancestor nodes (from parent to root).

# `copy`

```elixir
@spec copy(t(), (any() -&gt; any()) | nil) :: t()
```

Copies the hierarchy, optionally transforming data.

# `count`

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

Sets each node's value to the count of its leaves.

# `descendants`

```elixir
@spec descendants(t()) :: [t()]
```

Returns all descendant nodes as a flat list (including the node itself).

# `each`

```elixir
@spec each(t(), (t() -&gt; any())) :: :ok
```

Traverses the hierarchy, calling the function on each node.

Visits nodes in pre-order (parent before children).

# `each_after`

```elixir
@spec each_after(t(), (t() -&gt; any())) :: :ok
```

Traverses post-order (children before parent) and calls function.

# `leaves`

```elixir
@spec leaves(t()) :: [t()]
```

Returns all leaf nodes (nodes without children).

# `links`

```elixir
@spec links(t()) :: [{t(), t()}]
```

Returns all links (parent-child pairs) in the hierarchy.

Returns list of `{source, target}` tuples.

# `new`

```elixir
@spec new(map(), keyword()) :: t()
```

Creates a hierarchy from nested data.

## Options

- `:children` - Function to get children from a node (default: `& &1[:children]` or `& &1.children`)

# `path`

```elixir
@spec path(t(), t()) :: [t()]
```

Returns the shortest path from `source` to `target`: `source`, its ancestors up to
the lowest common ancestor, then the ancestors of `target` down to `target`.

Nodes are matched by their `data` lineage from the root, so a node compares equal
to the snapshot of itself held in a descendant's `parent` field.

# `sort`

```elixir
@spec sort(t(), (t(), t() -&gt; boolean())) :: t()
```

Sorts children at each level using the comparator.

The comparator receives two nodes and returns true if the first
should come before the second.

# `stratify`

```elixir
@spec stratify([map()], keyword()) :: t()
```

Creates a hierarchy from flat/tabular data.

## Options

- `:id` - Function to get node ID (default: `& &1[:id]` or `& &1.id`)
- `:parent_id` - Function to get parent ID (default: `& &1[:parent]` or `& &1.parent`)

# `sum`

```elixir
@spec sum(t(), (any() -&gt; number())) :: t()
```

Computes the sum of values for each node.

The value of each node becomes the sum of its own value plus
all descendant values. Evaluates the value function for leaves.

---

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