Author: Hayden Smith 2021
Why?
What?
Tries are a data structure for representing strings that support O(L) lookup and insertion (where L is the length of a string)
Depth of trie = length of longest word
Note: Not every word here is included in the trie
Each node in a trie:
#define ALPHABET_SIZE 26
typedef struct Node *Trie;
typedef struct Node {
char onechar; // current char in key
Trie child[ALPHABET_SIZE];
bool finish; // last char in key?
Item data; // no Item if !finish
} Node;
typedef char *Key; // just lower-case letters
find(trie,key):
| Input trie, key
| Output pointer to element in trie if key found
| NULL otherwise
|
| node=trie
| for each char c in key do
| | if node.child[c] exists then
| | node=node.child[c] // move down one level
| | else
| | return NULL
| | end if
| end for
| if node.finish then // "finishing" node reached?
| return node
| else
| return NULL
| end if
Trie insert(trie, item, key):
if trie is empty then:
t = new trie node
if m = 0 then // end of key
t.finish = true
t.data = item
else:
first = key[0]
rest = key[1..m-1]
t.child[first] = insert(t.child[first], item, rest)
return t
Above representation is space inefficient
And if we allowed all ascii chars in alphabet, 128 children
We could reduce branching factor by reducing "alphabet"