On this page:
8.18
top
up

8TriesπŸ”— i

(require pfds/trie ) package: pfds

A Trie (also known as a Digital Search Tree) is a data structure which takes advantage of the structure of aggregate types to achieve good running times for its operations. Our implementation provides Tries in which the keys are lists of the element type; this is sufficient for representing many aggregate data structures. In our implementation, each trie is a multiway tree with each node of the multiway tree carrying data of base element type. Tries provide lookup and insert operations with better asymptotic running times than hash tables. This data structure is very useful when used for an aggregate type like strings.

syntax

( Trie KV)

A trie with keys of type K and values of type V.

procedure

( tries valueskeys)(Trie KV)

values:(ListofV)
keys:(Listof(ListofK))
Function tries creates a trie with each value assigned to the list in keys at the corresponding location.

Example:
> (tries (list12345)
(mapstring->list(list"abc""xyz""abcfg""abxyz""xyz12")))

- : #(struct:Trie

((U (Some Positive-Byte) Mt)

(Immutable-HashTable Char (Trie Char Positive-Byte))))

#<Trie>

In the above example, "abc" will have the value 1, "xyz" will get 2 and so on.

procedure

( trie keys)(Trie KInteger)

keys:(Listof(ListofK))
Function trie creates a trie by assigning a integer value to each list in keys.

Example:
> (trie (mapstring->list(list"abc""xyz""abcfg""abxyz""xyz12")))

- : #(struct:Trie

((U (Some Integer) Mt) (Immutable-HashTable Char (Trie Char Integer))))

#<Trie>

In the above example, "abc" will have the value 1, "xyz" will get 2 and so on.

procedure

( insert valueskeystrie)(Trie KV)

values:(ListofV)
keys:(Listof(ListofK))
trie:(Trie KV)
Inserts multiple keys into the trie.

Example:
> (insert (list45)
(mapstring->list(list"abcfg""abxyz"))
(tries (list12)
(mapstring->list(list"abc""xyz"))))

- : #(struct:Trie

((U (Some Positive-Byte) Mt)

(Immutable-HashTable Char (Trie Char Positive-Byte))))

#<Trie>

In the above example, "abcfg" will have the value 4, "abxyz" will get 5

procedure

( bind keyvaluetrie)(Trie KV)

key:(ListofK)
value:V
trie:(Trie KV)
Inserts a key into the trie.

Example:
> (bind (string->list"abcfg")3
(tries (list12)
(mapstring->list(list"abc""xyz"))))

- : #(struct:Trie

((U (Some Positive-Byte) Mt)

(Immutable-HashTable Char (Trie Char Positive-Byte))))

#<Trie>

procedure

( lookup keytrie)V

key:(ListofK)
trie:(Trie KV)
Looks for the given key. If found, the value corresponding to the key is returned. Else throws an error.

Examples:
> (lookup (string->list"abcde")
(tries (list1234)
(mapstring->list(list"123""abc""xyz""abcde"))))

- : Integer [more precisely: Positive-Byte]

4

> (lookup (string->list"abc")
(tries (list1234)
(mapstring->list(list"123""abc""xyz""abcde"))))

- : Integer [more precisely: Positive-Byte]

2

> (lookup (string->list"abcd")
(tries (list1234)
(mapstring->list(list"123""abc""xyz""abcde"))))

lookup: given key not found in the trie

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /