2. What is a trie?
A trie is a tree-like data structure whose nodes store the letters of an alphabet. By
structuring the nodes in a particular way, words and strings can be retrieved from the
structure by traversing down a branch path of the tree.
There are a handful of different ways to represent something as seemingly simple as a set
of words. For example, a hash or dictionary is one that we’re probably familiar with, as is
as hash table.
But there’s another structure that was created to solve the very problem of representing a
set of words: a trie. The term “trie” comes from the word retrieval, and is usually
pronounced “try”, to distinguish it from other “tree” structures.
6. Pseudo code to check whether a single word
exists in a dictionary of words or not
boolean check(String s) {
for(every char in String s) {
if(child node is null) {
return false;
}
}
return true;
}
7. How do we insert the letters into their correct places?
8. Pseudo code for insertion into a tire
void insert(String s) {
for(every char in string s) {
if(child node belonging to current char is null) {
child node=new Node();
}
current_node=child_node;
}
}
9.
10.
11. HASH TABLE
Array + Linked List
Hash function is needed
Memory is not wasted as much
TRIE
Array + Pointers
No hash function is required
Takes up a lot of memory with
empty or null pointers
Hash Tables Vs Tries
12. How a trie changes as it grows?
As trie grows in size, less work must be done to add a value,
since the “intermediate nodes”, or the branches of the trie
have already been build up.
Each time we add a word’s letter, we need to look at 26
references, since there are only 26 possibilities in the
alphabet. This number never changes in the context of our
trie, so it is a constant value.
13. Big O notation of a Trie
The worst case runtime of creating a trie structure is
dependent on how many words the trie contains, and how
long they might potentially be. This is known as O(m.n)
time, where m is the longest word and n is the total number
of words.
The time of searching, inserting & deleting from a trie
depends on length of the word a and total number of words
: O(a.n)
14. Uses of Tries
Phone Book
Google search
T9 dictionary
Criminology
Linux commands completion
And many more…
15. Resources
Digit-based sorting and data structures, Professor Avrim Blum
Lecture Notes on Tries, Professor Frank Pfenning
Algorithms: Tries, Robert Sedgewick and Kevin Wayne
Tries, Brilliant Learning
Tries, Daniel Ellard
Tries, Harvard CS50