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
Specs
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}
Specs
Returns how many nodes (including parent nodes) a tree contains.
Examples
iex> FlatTree.count(1)
3
Specs
Returns the depth of an element.
Examples
iex> FlatTree.depth(0)
0
iex> FlatTree.depth(3)
2
Specs
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
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
Specs
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
Specs
Returns the left spanning in index in the tree index
spans.
Examples
iex> FlatTree.left_span(0)
0
iex> FlatTree.left_span(23)
16
Specs
Returns the relative offset of an element.
Examples
iex> FlatTree.offset(1)
0
iex> FlatTree.offset(4)
2
Specs
Returns the index of the parent element in tree.
Examples
iex> FlatTree.parent(0)
1
iex> FlatTree.parent(1)
3
Specs
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
Specs
Returns the right spanning in index in the tree index
spans.
Examples
iex> FlatTree.right_span(0)
0
iex> FlatTree.right_span(23)
30
Specs
Return the index of this elements sibling.
Examples
iex> FlatTree.sibling(0)
2
iex> FlatTree.sibling(2)
0
Specs
Returns the range (inclusive) the tree root at index
spans.
Examples
iex> FlatTree.spans(0)
{0, 0}
iex> FlatTree.spans(3)
{0, 6}