README.md

# FlatTree

[![Build Status](https://travis-ci.org/wittjosiah/flat_tree.svg?branch=main)](https://travis-ci.org/wittjosiah/flat_tree) [![Coverage Status](https://coveralls.io/repos/github/wittjosiah/flat_tree/badge.svg?branch=main)](https://coveralls.io/github/wittjosiah/flat_tree?branch=main)

You can represent a binary tree in a simple flat list using the following structure:

```
      3
  1       5
0   2   4   6  ...
```

Flat Trees are the core data structure that power [Hypercore feeds](https://hypercore-protocol.org/). This Elixir implementation is adapted from [mafintosh/flat-tree](https://github.com/mafintosh/flat-tree). More details can be found [here](https://datprotocol.github.io/book/ch01-01-flat-tree.html).

## Installation

The package can be installed by adding `flat_tree` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:flat_tree, "~> 0.1.0"}
  ]
end
```

## Usage

`FlatTree` exposes a series of functions to help you build and maintain this data structure.

```elixir
import FlatTree

list = []
i = FlatTree.index(0, 0) # get array index for depth: 0, offset: 0
j = FlatTree.index(1, 0) # get array index for depth: 1, offset: 0

# use these indexes to store some data
list = List.insert_at(list, i, "a")
list = List.insert_at(list, j, "b")
list = List.insert_at(list, FlatTree.parent(i), "parent of a and b")
```