stella v0.4.0 BST

Documentation for Binary search tree data structure.

Link to this section Summary

Functions

Insert a new node to the given tree.

Returns a maximum node of a given subtree

Returns a minimum node of a given subtree

Create a new Binary Search Tree with root's value as the given 'key'

Search a specific node by given searched_key in given subtree (or a whole tree)

Link to this section Functions

Link to this function

insert(tree, new_key)

Insert a new node to the given tree.

  • Average time complexity: O(log n)
  • Worst time complexity: O(n)

Examples

iex> BST.insert(nil, 2)
%{key: 2, left: nil, right: nil, parent: nil}
Link to this function

maximum(subtree)

Returns a maximum node of a given subtree

Examples

iex> BST.insert(nil, 2) |> BST.insert(5) |> BST.insert(1) |> BST.maximum
%{key: 5, left: nil, right: nil, parent: 2}
Link to this function

minimum(subtree)

Returns a minimum node of a given subtree

Examples

iex> BST.insert(nil, 2) |> BST.insert(5) |> BST.insert(1) |> BST.minimum
%{key: 1, left: nil, right: nil, parent: 2}
Link to this function

new(key, partner_key \\ nil)

Create a new Binary Search Tree with root's value as the given 'key'

Examples

iex> BST.new(5)
%{key: 5, left: nil, right: nil, parent: nil}
Link to this function

search(subtree, searched_key)

Search a specific node by given searched_key in given subtree (or a whole tree)

  • Average time complexity: O(log n)
  • Worst time complexity: O(n)

Examples

iex> BST.insert(nil, 2)
%{key: 2, left: nil, right: nil, parent: nil}