FlatTree (Flat Tree v0.1.0) View Source

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

      3
  1       5
0   2   4   6  ...

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

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")

Link to this section Summary

Functions

Returns a tuple {left_child, right_child} with the indexes of this elements children. If this element does not have any children is returns nil.

Returns how many nodes (including parent nodes) a tree contains.

Returns the depth of an element.

Returns a list of all the full roots (subtrees where all nodes have either 2 or 0 children) < index. For example full_roots(8) returns [3] since the subtree rooted at 3 spans 0 -> 6 and the tree rooted at 7 has a child located at 9 which is >= 8.

Returns an array index for the tree element at the given depth and offset.

Returns the index of the left child of this element. If this element does not have a left child it returns -1.

Returns the left spanning in index in the tree index spans.

Returns the relative offset of an element.

Returns the index of the parent element in tree.

Returns the index of the right child of this element. If this element does not have a right child it returns -1.

Returns the right spanning in index in the tree index spans.

Return the index of this elements sibling.

Returns the range (inclusive) the tree root at index spans.

Link to this section Functions

Link to this function

children(index, depth \\ nil)

View Source

Specs

children(index :: integer(), depth :: integer() | nil) :: {integer(), integer()}

Returns a tuple {left_child, right_child} with the indexes of this elements children. If this element does not have any children is returns nil.

Examples

iex> FlatTree.children(0)
nil

iex> FlatTree.children(3)
{1, 5}
Link to this function

count(index, depth \\ nil)

View Source

Specs

count(index :: integer(), depth :: integer() | nil) :: integer()

Returns how many nodes (including parent nodes) a tree contains.

Examples

iex> FlatTree.count(1)
3
Link to this function

depth(index, depth \\ 0)

View Source

Specs

depth(index :: integer(), depth :: integer()) :: integer()

Returns the depth of an element.

Examples

iex> FlatTree.depth(0)
0

iex> FlatTree.depth(3)
2
Link to this function

full_roots(index, result \\ [])

View Source

Specs

full_roots(index :: integer(), result :: [integer()]) :: [integer()]

Returns a list of all the full roots (subtrees where all nodes have either 2 or 0 children) < index. For example full_roots(8) returns [3] since the subtree rooted at 3 spans 0 -> 6 and the tree rooted at 7 has a child located at 9 which is >= 8.

Examples

iex> FlatTree.full_roots(0)
[]

iex> FlatTree.full_roots(18)
[7, 16]

Specs

index(depth :: integer(), offset :: integer()) :: integer()

Returns an array index for the tree element at the given depth and offset.

Examples

iex> FlatTree.index(0, 0)
0

iex> FlatTree.index(0, 2)
4
Link to this function

left_child(index, depth \\ nil)

View Source

Specs

left_child(index :: integer(), depth :: integer() | nil) :: integer()

Returns the index of the left child of this element. If this element does not have a left child it returns -1.

Examples

iex> FlatTree.left_child(0)
-1

iex> FlatTree.left_child(3)
1
Link to this function

left_span(index, depth \\ nil)

View Source

Specs

left_span(index :: integer(), depth :: integer() | nil) :: integer()

Returns the left spanning in index in the tree index spans.

Examples

iex> FlatTree.left_span(0)
0

iex> FlatTree.left_span(23)
16
Link to this function

offset(index, depth \\ nil)

View Source

Specs

offset(index :: integer(), depth :: integer() | nil) :: integer()

Returns the relative offset of an element.

Examples

iex> FlatTree.offset(1)
0

iex> FlatTree.offset(4)
2
Link to this function

parent(index, depth \\ nil)

View Source

Specs

parent(index :: integer(), depth :: integer() | nil) :: integer()

Returns the index of the parent element in tree.

Examples

iex> FlatTree.parent(0)
1

iex> FlatTree.parent(1)
3
Link to this function

right_child(index, depth \\ nil)

View Source

Specs

right_child(index :: integer(), depth :: integer() | nil) :: integer()

Returns the index of the right child of this element. If this element does not have a right child it returns -1.

Examples

iex> FlatTree.right_child(0)
-1

iex> FlatTree.right_child(3)
5 
Link to this function

right_span(index, depth \\ nil)

View Source

Specs

right_span(index :: integer(), depth :: integer() | nil) :: integer()

Returns the right spanning in index in the tree index spans.

Examples

iex> FlatTree.right_span(0)
0

iex> FlatTree.right_span(23)
30
Link to this function

sibling(index, depth \\ nil)

View Source

Specs

sibling(index :: integer(), depth :: integer() | nil) :: integer()

Return the index of this elements sibling.

Examples

iex> FlatTree.sibling(0)
2

iex> FlatTree.sibling(2)
0
Link to this function

spans(index, depth \\ nil)

View Source

Specs

spans(index :: integer(), depth :: integer() | nil) :: {integer(), integer()}

Returns the range (inclusive) the tree root at index spans.

Examples

iex> FlatTree.spans(0)
{0, 0}

iex> FlatTree.spans(3)
{0, 6}