# Module btrie

- Description
- Data Types
- Function Index
- Function Details

### A trie data structure implementation.

The trie (i.e., from "retrieval") data structure was invented by Edward Fredkin (it is a form of radix sort). The implementation stores string suffixes as a list because it is a PATRICIA trie (PATRICIA - Practical Algorithm to Retrieve Information Coded in Alphanumeric, D.R.Morrison (1968)).

This Erlang trie implementation uses binary keys.

Copyright © 2010-2020 Michael Truog

Version: 2.0.1 Dec 4 2021 12:38:18 ------------------------------------------------------------------------

Authors: Michael Truog (`mjtruog at protonmail dot com`).

## Description

### A trie data structure implementation.

The trie (i.e., from "retrieval") data structure was invented by Edward Fredkin (it is a form of radix sort). The implementation stores string suffixes as a list because it is a PATRICIA trie (PATRICIA - Practical Algorithm to Retrieve Information Coded in Alphanumeric, D.R.Morrison (1968)).

This Erlang trie implementation uses binary keys. Using binary keys means that other data structures are quicker alternatives, so this module is probably not a good choice, unless it is used for functions not available elsewhere.

## Data Types

### empty_trie()

`empty_trie() = <<>>`

### nonempty_trie()

`nonempty_trie() = {integer(), integer(), tuple()}`

### trie()

`trie() = nonempty_trie() | empty_trie()`

## Function Index

append/3 | #### Append a value as a list element in a trie instance. .
append_list/3 | #### Append a list of values as a list element in a trie instance. .
erase/2 | #### Erase a value in a trie. .
erase_similar/2 | #### Erase all entries within a trie that share a common prefix. .
fetch/2 | #### Fetch a value from a trie. .
fetch_keys/1 | #### Fetch all the keys in a trie. .
fetch_keys_similar/2 | #### Fetch the keys within a trie that share a common prefix. .
filter/2 | #### Filter a trie with a predicate function. .
find/2 | #### Find a value in a trie. .
find_prefix/2 | #### Find a value in a trie by prefix. The atom 'prefix' is returned if the string supplied is a prefix for a key that has previously been stored within the trie, but no value was found, since there was no exact match for the string supplied.
find_prefix_longest/2 | #### Find the longest key in a trie that is a prefix to the passed string. .
find_prefixes/2 | #### Find all the keys in a trie that are prefixes to the passed string. The entries are returned in alphabetical order.
fold/3 | #### Fold a function over the trie. Traverses in alphabetical order.
fold_similar/4 | #### Fold a function over the keys within a trie that share a common prefix. Traverses in alphabetical order.
foldl/3 | #### Fold a function over the trie. Traverses in alphabetical order.
foldl_similar/4 | #### Fold a function over the keys within a trie that share a common prefix. Traverses in alphabetical order.
foldr/3 | #### Fold a function over the trie in reverse. Traverses in reverse alphabetical order.
foldr_similar/4 | #### Fold a function over the keys within a trie that share a common prefix in reverse. Traverses in reverse alphabetical order.
foreach/2 | #### Call a function for each element. Traverses in alphabetical order.
from_list/1 | #### Create a trie from a list. .
is_key/2 | #### Determine if a key exists in a trie. .
map/2 | #### Map a function over a trie. Traverses in reverse alphabetical order.
merge/3 | #### Merge two trie instance. Update the second trie parameter with all of the elements found within the first trie parameter.
new/0 | #### Create a new trie instance. .
new/1 | #### Create a new trie instance from a list. The list may contain either: strings, 2 element tuples with a string as the first tuple element, or tuples with more than 2 elements (including records) with a string as the first element (second element if it is a record).
prefix/3 | #### Insert a value as the first list element in a trie instance. The reverse of append/3.
size/1 | #### Size of a trie instance. .
store/2 | #### Store only a key in a trie instance. .
store/3 | #### Store a key/value pair in a trie instance. .
take/2 | #### Take a value from the trie. .
to_list/1 | #### Convert all entries in a trie to a list. The list is in alphabetical order.
to_list_similar/2 | #### Return a list of all entries within a trie that share a common prefix. .
update/3 | #### Update a value in a trie. .
update/4 | #### Update or add a value in a trie. .
update_counter/3 | #### Update a counter in a trie. .

## Function Details

### append/3

`append(Key::<<_:8, _:_*8>>, Value::any(), Node::trie()) -> nonempty_trie()`

#### Append a value as a list element in a trie instance.

### append_list/3

`append_list(Key::<<_:8, _:_*8>>, ValueList::list(), Node::trie()) -> nonempty_trie()`

#### Append a list of values as a list element in a trie instance.

### erase/2

`erase(Key::<<_:8, _:_*8>>, Node::trie()) -> trie()`

#### Erase a value in a trie.

### erase_similar/2

`erase_similar(Similar::<<_:8, _:_*8>>, Node::trie()) -> [<<_:8, _:_*8>>]`

#### Erase all entries within a trie that share a common prefix.

### fetch/2

`fetch(X1::<<_:8, _:_*8>>, Node::nonempty_trie()) -> any()`

#### Fetch a value from a trie.

### fetch_keys/1

`fetch_keys(Node::trie()) -> [<<_:8, _:_*8>>]`

#### Fetch all the keys in a trie.

### fetch_keys_similar/2

`fetch_keys_similar(Similar::<<_:8, _:_*8>>, Node::trie()) -> [<<_:8, _:_*8>>]`

#### Fetch the keys within a trie that share a common prefix.

### filter/2

`filter(F::fun((<<_:8, _:_*8>>, any()) -> boolean()), Node::trie()) -> trie()`

#### Filter a trie with a predicate function.

### find/2

`find(X1::<<_:8, _:_*8>>, Node::trie()) -> {ok, any()} | error`

#### Find a value in a trie.

### find_prefix/2

`find_prefix(X1::<<_:8, _:_*8>>, X2::trie()) -> {ok, any()} | prefix | error`

#### Find a value in a trie by prefix.

The atom 'prefix' is returned if the string supplied is a prefix for a key that has previously been stored within the trie, but no value was found, since there was no exact match for the string supplied.

### find_prefix_longest/2

`find_prefix_longest(Match::<<_:8, _:_*8>>, Node::trie()) -> {ok, <<_:8, _:_*8>>, any()} | error`

#### Find the longest key in a trie that is a prefix to the passed string.

### find_prefixes/2

`find_prefixes(Match::<<_:8, _:_*8>>, Node::trie()) -> [{<<_:8, _:_*8>>, any()}]`

#### Find all the keys in a trie that are prefixes to the passed string.

The entries are returned in alphabetical order.

### fold/3

`fold(F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), A::any(), Node::trie()) -> any()`

#### Fold a function over the trie.

Traverses in alphabetical order.

### fold_similar/4

`fold_similar(Similar::<<_:8, _:_*8>>, F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), A::any(), Node::trie()) -> any()`

#### Fold a function over the keys within a trie that share a common prefix.

Traverses in alphabetical order.

### foldl/3

`foldl(F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), A::any(), Node::trie()) -> any()`

#### Fold a function over the trie.

Traverses in alphabetical order.

### foldl_similar/4

`foldl_similar(Similar::<<_:8, _:_*8>>, F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), A::any(), Node::trie()) -> any()`

#### Fold a function over the keys within a trie that share a common prefix.

Traverses in alphabetical order.

### foldr/3

`foldr(F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), A::any(), Node::trie()) -> any()`

#### Fold a function over the trie in reverse.

Traverses in reverse alphabetical order.

### foldr_similar/4

`foldr_similar(Similar::<<_:8, _:_*8>>, F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), A::any(), Node::trie()) -> any()`

#### Fold a function over the keys within a trie that share a common prefix in reverse.

Traverses in reverse alphabetical order.

### foreach/2

`foreach(F::fun((<<_:8, _:_*8>>, any()) -> any()), Node::trie()) -> any()`

#### Call a function for each element.

Traverses in alphabetical order.

### from_list/1

`from_list(L::[<<_:8, _:_*8>> | tuple()]) -> trie()`

#### Create a trie from a list.

### is_key/2

`is_key(X1::<<_:8, _:_*8>>, Node::trie()) -> boolean()`

#### Determine if a key exists in a trie.

### map/2

`map(F::fun((<<_:8, _:_*8>>, any()) -> any()), Node::trie()) -> trie()`

#### Map a function over a trie.

Traverses in reverse alphabetical order.

### merge/3

`merge(F::fun((<<_:8, _:_*8>>, any(), any()) -> any()), Node1::trie(), Node2::trie()) -> trie()`

#### Merge two trie instance.

Update the second trie parameter with all of the elements found within the first trie parameter.

### new/0

`new() -> empty_trie()`

#### Create a new trie instance.

### new/1

`new(L::[<<_:8, _:_*8>> | tuple()]) -> trie()`

#### Create a new trie instance from a list.

The list may contain either: strings, 2 element tuples with a string as the first tuple element, or tuples with more than 2 elements (including records) with a string as the first element (second element if it is a record). If a list of records (or tuples larger than 2 elements) is provided, the whole record/tuple is stored as the value.

### prefix/3

`prefix(Key::<<_:8, _:_*8>>, Value::any(), Node::trie()) -> nonempty_trie()`

#### Insert a value as the first list element in a trie instance.

The reverse of append/3.

### size/1

`size(Node::trie()) -> non_neg_integer()`

#### Size of a trie instance.

### store/2

`store(Key::<<_:8, _:_*8>>, Node::trie()) -> nonempty_trie()`

#### Store only a key in a trie instance.

### store/3

`store(Key::<<_:8, _:_*8>>, NewValue::any(), Node::trie()) -> nonempty_trie()`

#### Store a key/value pair in a trie instance.

### take/2

`take(Key::<<_:8, _:_*8>>, Node::trie()) -> {any(), trie()} | error`

#### Take a value from the trie.

### to_list/1

`to_list(Node::trie()) -> [{<<_:8, _:_*8>>, any()}]`

#### Convert all entries in a trie to a list.

The list is in alphabetical order.

### to_list_similar/2

`to_list_similar(Similar::<<_:8, _:_*8>>, Node::trie()) -> [{<<_:8, _:_*8>>, any()}]`

#### Return a list of all entries within a trie that share a common prefix.

### update/3

`update(X1::<<_:8, _:_*8>>, F::fun((any()) -> any()), Node::nonempty_trie()) -> nonempty_trie()`

#### Update a value in a trie.

### update/4

`update(Key::<<_:8, _:_*8>>, F::fun((any()) -> any()), Initial::any(), Node::trie()) -> nonempty_trie()`

#### Update or add a value in a trie.

### update_counter/3

`update_counter(Key::<<_:8, _:_*8>>, Increment::number(), Node::trie()) -> nonempty_trie()`

#### Update a counter in a trie.

Generated by EDoc