Algorithms - String Processing - 01 - Trie
Algorithms - String Processing - 01 - Trie
Mostafa Saad
Trie
A simple but powerful data structure.
It adds/searches for string in O(L)
It is a tree with branches as letters
Add abcd
a
Add xyz
a
Add abf
Add xn
Add ab
Add bcd
b
So
Simple tree
Common prefixes are created once
Easy to know the nodes (red ones)
Could answer Questions such as:
Does word exist? Frequency?
Does prefix exist? Frequency?
Any question is answered by a trace from root to
maximum a leaf
So O(L) where L is word length
Could also print all strings on O(S), where S # of
words
Implementation
Node is similar to binary search tree, but we