Bintree (bintree v1.1.1) View Source

Basic module

Link to this section Summary

Types

Binary tree with left and right branch

Branch can be nil or other bintree

Currently only integers are supported as a value

Functions

Filters the bintree, i.e. returns only those branch for which filter_fun returns a truthy value.

Inserts a value at a given path

Creates base binary tree

Automatically generates binary tree values

Formats the type of a binary tree to a string

Link to this section Types

Specs

bintree() :: %Bintree{left: branch(), right: branch(), value: integer()}

Binary tree with left and right branch

Specs

branch() :: bintree() | nil

Branch can be nil or other bintree

Specs

filter_fun() :: (value() -> boolean())

Specs

process_fun() :: (value() -> value())

Specs

value() :: integer()

Currently only integers are supported as a value

Link to this section Functions

Link to this function

filter(tree, filter_fun)

View Source (since 1.0.0)

Specs

filter(branch(), filter_fun()) :: branch()

Filters the bintree, i.e. returns only those branch for which filter_fun returns a truthy value.

Link to this function

insert(tree, path, value)

View Source (since 1.1.1)

Specs

insert(bintree(), [:left | :right, ...], value()) :: bintree()

Inserts a value at a given path

Example

iex> Bintree.new(1, 3, 5)
iex> |> Bintree.insert([:left, :left], 5)
iex> |> Bintree.insert([:left, :right], 28)
# Result:
#     1
#     |
#    /---\
#    |   |
#    3   5
#    |
#  /--\
#  |  |
#  5 28
Link to this function

new(value, left \\ nil, right \\ nil)

View Source (since 1.0.0)

Specs

new(any(), branch(), branch()) :: bintree()

Creates base binary tree

Link to this function

new(value, left_fun, right_fun, filter_fun)

View Source (since 1.0.0)

Specs

Automatically generates binary tree values

Generates a branch while the filter_fun returns a truthy value.

Examples

iex> Bintree.new(1, &(&1*3), &(&1+3), &(&1 > 10))
# Returns a binary tree, where turning left is multiplying by three,
# turning right is adding three. If the number is greater than 10,
# then the generation of this branch is stopped.

iex> Bintree.new(1, &(&1*3), &(&1+3), 4)
# The rules are the same as the previous one, but the generation of the tree will end
# when the depth of 4 values is reached.
Link to this function

to_string(tree)

View Source (since 1.0.0)

Specs

to_string(bintree()) :: String.t()

Formats the type of a binary tree to a string