TST (Elixir Algorithms and Data Structures (eads) v0.1.1) View Source

This module supports the Ternary Search Trie data structure.

Link to this section Summary

Functions

Query the TST to see if a key is a valid key

Get the item stored by the given key.

Insert a key-value pair into a TST. It is acceptable to pass nil as the first argument - a new TST will then be created.

Creates an empty ternary search trie data structure

Link to this section Functions

Specs

exists?(
  nil
  | %TST{
      item: term(),
      left: term(),
      middle: term(),
      right: term(),
      value: term()
    },
  String.t()
) :: boolean()

Query the TST to see if a key is a valid key

Parameters

  • tst: The Ternary Search Trie if you have saved one, or nil (although this doesn't make much sense to look for valid keys in)
  • key: The key which you are querying whether is valid or not

Examples

iex> TST.new() |> TST.insert("key", "value") |> TST.exists?("key")
true

iex> TST.new() |> TST.insert("key", "value") |> TST.exists?("a whole other key")
false

Specs

get(
  %TST{
    item: term(),
    left: term(),
    middle: term(),
    right: term(),
    value: term()
  },
  String.t()
) :: {:ok | :error, any()}

Get the item stored by the given key.

Returns {:ok, item} or {:error, msg}

Parameters

  • tst: The Ternary Search Trie that is being considered
  • key: The key which might have an item associated with it

Examples

# TODO

Specs

insert(
  %TST{
    item: term(),
    left: term(),
    middle: term(),
    right: term(),
    value: term()
  },
  String.t(),
  any()
) :: %TST{
  item: term(),
  left: term(),
  middle: term(),
  right: term(),
  value: term()
}

Insert a key-value pair into a TST. It is acceptable to pass nil as the first argument - a new TST will then be created.

Parameters

  • tst: A Ternary Search Trie map or nil (no tree yet created)
  • key: A String key in which to associate a value with
  • value: The value that you want to have stored. Can be of any type

Examples

iex> TST.new() |> TST.insert("key", "value")
%TST{
item: nil,
left: nil,
middle: %TST{
  item: nil,
  left: nil,
  middle: %TST{item: "value", left: nil, middle: nil, right: nil, value: "y"},
  right: nil,
  value: "e"
},
right: nil,
value: "k"
}

Specs

new() :: %TST{
  item: term(),
  left: term(),
  middle: term(),
  right: term(),
  value: term()
}

Creates an empty ternary search trie data structure

Returns %TST{}

Examples

iex> TST.new() %TST{item: nil, left: nil, middle: nil, right: nil, value: nil}