A trie is a tree-like data structure used to store strings that allows for efficient retrieval of strings based on prefixes. It works by splitting strings into individual characters and storing them as nodes in a tree. Each node represents a unique character and branches to other nodes until the full string is represented by a path from the root node. Tries allow for fast insertion, searching, and retrieval of strings in O(m) time where m is the length of the string. They are useful for applications involving large dictionaries of strings like auto-complete features, IP routing tables, and storing browser histories.
2. CONTENTS
Introduction
Why Tries?
Types of Tries
Standard Tries
Standard Trie – Insertion
Standard Trie – Searching
Analysis
Compressed Tries
Advantages and Disadvantages
Applications
Conclusion 2
3. INTRODUCTION
In Computer Science,
trie are also called
Digital Trees,
sometimes Radix
trees, and Prefix Trees.
It is an ordered Data
Structure that is used
to store dynamic sets
or associative arrays
where key are usually
STRINGS.
3
4. WHY TRIES?
Preprocessing the pattern speeds up pattern matching
queries.
After preprocessing the pattern, KMP’s algorithm
performs pattern matching in time proportional to the
text size.
If the text is large, immutable and searched for often (e.g.,
works by Shakespeare), we may want to preprocess the text
instead of the pattern.
A trie is a compact data structure for representing a set of
strings, such as all the words in a text
A tries supports pattern matching queries in time
proportional to the pattern size
4
6. STANDARD TRIE
6
Let S be set strings.
S={ bear, bell, bid, bull, buy, sell, stock, stop }
Observations:
o The children of a node are alphabetically ordered.
o The paths from the external nodes to the root yield
the strings of S.
7. STANDARD TRIE - INSERTION
algorithm insert(root: node, s: string, value: any):
node = root
i = 0
n = length(s)
while(i<n):
if node.child (s [ i ] ) ! = nil
node = node.child (s [ i ] )
i = i+1
else:
break
(*append a new node, if necessary*)
while(i<n):
node.child (s [ i ] ) = new node
node = node.child ( s [ i ] )
i = i+1
node.value = value 7
root
Let S be a set strings to be inserted,
S={ bear, bell, bid, bull, buy, sell, stock,
stop}
i
s
p
(39)
d
(5,14)
l
l
(9)
u
l
l
y
(17,26)
(22)
e
l
l
(30)
t
o
c
k
(34,43)
b
e
a
r
(1)
8. STANDARD TRIE - SEARCHING
algorithm find(node, key):
for char in key:
if char is not in node.child:
return null
else:
node = node.child[char]
return node.value
Operations:
find(node, ”bear”)
return (1)
find(node, “stock”)
return (34,43)
find(node, “best”)
return null 88
root
i
p
(39)
d
(5,14)
l
l
(9)
u
l
l
y
(17,26)
(22)
e
l
l
(30)
s
t
o
c
k
(34,43)
b
e
a
r
(1)
Let S be a set strings presents in a trie,
S={ bear, bell, bid, bull, buy, sell, stock, stop}
9. ANALYSIS
9
Space Complexity:
O(n) for insertion, searching and deletion.
Time Complexity:
O(dm) for insertion, searching and deletion
Where:
n : total size of the strings in S.
m : size of the string parameter in operations.
d : size of the alphabet. i,e
26 in case each character is of small english alphabet,
2 in case of binary numbers, and
so on…….
10. COMPRESSED TRIES
It is obtained from standard trie by compressing the chain of
“redundant” nodes in such a way that each nodes has at least two
childrens.
10
Standard Trie Compressed Trie
Continue……
11. Problem with Standard trie
We cannot search for a substring of a particular string in a text because
standard trie is based on prefix matching.
So to handle this situation, we can use compressed tries.
Therefore, instead of storing the substrings, we store the range of indices in
a nodes of compressed trie.
Compressed Trie uses O(n) space, where n is no. of strings in an array
which is much efficient as standard trie.
11
Suppose strings in a text is stored in an array of strings:
12. ADVANTAGES AND DISADVANTAGES
Advantages:
Looking up data in a trie is faster in the worst case, O(m) time (where m is the length
of a search string), compared to an imperfect hash table. An imperfect hash table can
have key collisions
There are no collisions of different keys in a trie.
There is no need to provide a hash function or to change hash functions as more keys
are added to a trie.
A trie can provide an alphabetical ordering of the entries by key.
Disadvantages:
Tries can be slower in some cases than hash tables for looking up data, especially if
the data is directly accessed on a hard disk drive or some other secondary storage
device where the random-access time is high compared to main memory
Some keys, such as floating point numbers, can lead to long chains and prefixes that
are not particularly meaningful
Some tries can require more space than a hash table, as memory may be allocated for
each character in the search string 12
13. APPLICATIONS
In Web search and contact search
Auto complete feature is widely used in internet and mobile apps. A lot
of websites and apps try to complete your input as soon as you start
typing.
13
Continue…
14. In URL search in a web browser
Tries is also be used to store the browser history.
In Application search in windows OS
14
Continue…
15. In Routing table
For example, consider this IPv4 routing table(CIDR notation is
used):
192.168.20.16/28
192.168.0.0/16
When the address 192.168.20.19 needs to look up in the trie
of network addresses, both the entries in the routing table
“match”.
In this case, the longest prefix of the candidate routes is
192.168.20.16/28, since its subnet mask is higher the other
entry’s mask.
Therefore, tries help router to fast lookup the destination
network of an IP packet.
15
16. CONCLUSION
Tries are more space efficient when they contain a large
number of short keys, because nodes are shared between
keys with common initial subsequences.
Tries facilitate longest-prefix matching, helping to find
the key sharing the longest possible prefix of characters
all unique.
So because of its efficiency and amazing real life
applications, it is recommended for soft. Engg to learn
TRIE data structure.
16
17. REFERENCES
www.cse.iitd.ac.in/~saroj/DS/DS2014/Trie.pdf
http://en.wikipedia.org/wiki/Trie
infoscience.epfl.ch/record/64394/files/triesearches.pdf
stackoverflow.com/.../where-do-i-find-a-standard-trie-
based-map-implementation.html
Knuth, Donald (1997). "6.3: Digital Searching". The Art
of Computer Programming Volume 3: Sorting and
Searching (2nd ed.). Addison-Wesley. p. 492. ISBN 0-
201-89685
17