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
Specs
Link to this section Functions
Specs
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
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
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 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
new(any()) :: Algae.Tree.BinarySearch.Node.t()
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
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
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]