Algae.Tree.BinarySearch (Algae v1.3.1) View Source

Represent a BinarySearch tree.

Examples

iex> alias Algae.Tree.BinarySearch, as: BSTree
...>
...> BSTree.Node.new(
...>   42,
...>   BSTree.Node.new(77),
...>   BSTree.Node.new(
...>     1234,
...>     BSTree.Node.new(98),
...>     BSTree.Node.new(32)
...>   )
...> )
%Algae.Tree.BinarySearch.Node{
  node: 42,
  left: %Algae.Tree.BinarySearch.Node{
    node:  77,
    left:  %Algae.Tree.BinarySearch.Empty{},
    right: %Algae.Tree.BinarySearch.Empty{}
  },
  right: %Algae.Tree.BinarySearch.Node{
    node:  1234,
    left:  %Algae.Tree.BinarySearch.Node{
      node:  98,
      left:  %Algae.Tree.BinarySearch.Empty{},
      right: %Algae.Tree.BinarySearch.Empty{}
    },
    right: %Algae.Tree.BinarySearch.Node{
      node:  32,
      left:  %Algae.Tree.BinarySearch.Empty{},
      right: %Algae.Tree.BinarySearch.Empty{}
    }
  }
}

Link to this section Summary

Functions

Remove an element from a tree by value.

Build a BinarySearch tree from a list.

Build a BinarySearch tree from a list and attach to an existing tree.

Insert a new element into a tree.

Create an empty tree.

Bring a value into an otherwise empty tree.

Flatten a tree into a list.

Flatten a tree into a list with elements sorted.

Link to this section Types

Link to this section Functions

Specs

delete(t(), any()) :: t()

Remove an element from a tree by value.

Examples

iex> alias Algae.Tree.BinarySearch, as: BSTree
...>
...> BSTree.Node.new(
...>   42,
...>   BSTree.Node.new(77),
...>   BSTree.Node.new(
...>     1234,
...>     BSTree.Node.new(98),
...>     BSTree.Node.new(32)
...>   )
...> ) |> delete(98)
%Algae.Tree.BinarySearch.Node{
  node: 42,
  left: %Algae.Tree.BinarySearch.Node{
    node: 77
  },
  right: %Algae.Tree.BinarySearch.Node{
    node: 1234,
    right: %Algae.Tree.BinarySearch.Node{
      node: 32
    }
  }
}

Specs

from_list(list()) :: t()

Build a BinarySearch tree from a list.

Examples

iex> Algae.Tree.BinarySearch.from_list([42, 77, 1234, 98, 32])
%Algae.Tree.BinarySearch.Node{
  node: 42,
  left: %Algae.Tree.BinarySearch.Node{
    node:  32
  },
  right: %Algae.Tree.BinarySearch.Node{
    node: 77,
    right: %Algae.Tree.BinarySearch.Node{
      node: 1234,
      left: %Algae.Tree.BinarySearch.Node{
        node:  98
      }
    }
  }
}

Specs

from_list(list(), t()) :: t()

Build a BinarySearch tree from a list and attach to an existing tree.

Examples

iex> Algae.Tree.BinarySearch.from_list([42, 77, 1234, 98, 32], new(-9))
%Algae.Tree.BinarySearch.Node{
  node:  -9,
  right: %Algae.Tree.BinarySearch.Node{
    left: %Algae.Tree.BinarySearch.Node{
      node:  32
    },
    node: 42,
    right: %Algae.Tree.BinarySearch.Node{
      node: 77,
      right: %Algae.Tree.BinarySearch.Node{
        node: 1234,
        left: %Algae.Tree.BinarySearch.Node{
          node: 98
        },
        right: %Algae.Tree.BinarySearch.Empty{}
      }
    }
  }
}

Specs

insert(t(), any()) :: t()

Insert a new element into a tree.

Examples

iex> insert(new(42), 43)
%Algae.Tree.BinarySearch.Node{
  node: 42,
  right: %Algae.Tree.BinarySearch.Node{
    node: 43
  }
}

Specs

new() :: t()
new() :: Algae.Tree.BinarySearch.Empty.t()

Create an empty tree.

Examples

iex> new()
%Algae.Tree.BinarySearch.Empty{}

Specs

Bring a value into an otherwise empty tree.

Examples

iex> new(42)
%Algae.Tree.BinarySearch.Node{
  node:  42,
  left:  %Algae.Tree.BinarySearch.Empty{},
  right: %Algae.Tree.BinarySearch.Empty{}
}

Specs

to_list(t()) :: list()

Flatten a tree into a list.

Examples

iex> alias Algae.Tree.BinarySearch, as: BSTree
...>
...> BSTree.Node.new(
...>   42,
...>   BSTree.Node.new(77),
...>   BSTree.Node.new(
...>     1234,
...>     BSTree.Node.new(98),
...>     BSTree.Node.new(32)
...>   )
...> )
...> |> BSTree.to_list()
[42, 77, 1234, 98, 32]

Specs

to_ordered_list(t()) :: list()

Flatten a tree into a list with elements sorted.

Examples

iex> alias Algae.Tree.BinarySearch, as: BSTree
...>
...> BSTree.Node.new(
...>   42,
...>   BSTree.Node.new(77),
...>   BSTree.Node.new(
...>     1234,
...>     BSTree.Node.new(98),
...>     BSTree.Node.new(32)
...>   )
...> )
...> |> BSTree.to_ordered_list()
[32, 42, 77, 98, 1234]