Tries

Tries
Deliverables
Tries basics
Standard Trie
Compressed Trie
Suffix Trie
Operations on Tries
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
2
Standard Trie
Standard trie for a set of strings is an ordered tree such that
Each node but the root is labelled with a character
The children of a node are alphabetically ordered
The paths from the Root to leaves yield the strings of S
Example: standard trie for set
S = { dog, deal, dear, date, kite }
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
3
Standard Trie
K
D
O
E
L
6/8/2012 3:00 AM
T
T
A
G
I
A
R
Copyright @ gdeepak.com®
E
E
4
Word matching with a Trie
0
1
2
D
O
G
15
16
17
E
A
R
3
18
4
5
6
7
D
A
T
E
19
20
21
22
D
A
T
E
8
23
9
10
11
12
D
E
A
L
24
25
26
27
K
I
T
E
E
0
L
9
6/8/2012 3:00 AM
28
29
T
T
R
14
D
I
A
A
G
14
K
D
O
13
Copyright @ gdeepak.com®
E
E
4
24
5
Compressed Trie
KITE
D
OG
EA
L
ATE
R
It has internal nodes of degree at least two. It is obtained from
standard trie by compressing chains of redundant nodes
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
6
Compact representation
•
•
•
•
Compact representation of a compressed trie for an array of strings:
Stores at the nodes ranges of indices instead of substrings
Uses O(s) space, where s is the number of strings in the array
Serves as an auxiliary index structure
0
1
2
3
S[0]
D
E
A
L
S[1]
D
E
A
R
D
A
T
E
S[2]
6/8/2012 3:00 AM
S[3]
S[4]
0
1
2
3
K
I
T
E
D
O
G
Copyright @ gdeepak.com®
7
Compressed Trie-Compact Representation
3,0,3
0,0,0
4,1,2
1,1,2
0,3,3
6/8/2012 3:00 AM
2,1,3
1,3,3
Copyright @ gdeepak.com®
8
Suffix Trie
• The suffix trie of a string X is the compressed trie of all
the suffixes of X
m i n i m i z e
0 1 2 3 4 5 6 7
e
mize
i
nimize
6/8/2012 3:00 AM
mi
ze
nimize
Copyright @ gdeepak.com®
nimize
ze
ze
9
Suffix Trie
Compact representation of the suffix trie for a string X of
size n from an alphabet of size d Uses O(n) space
Supports arbitrary pattern matching queries in X in O(dm)
time, where m is the size of the pattern
m i n i m i z e
0 1 2 3 4 5 6 7
7, 7
4, 7
6/8/2012 3:00 AM
1, 1
2, 7
0, 1
6, 7
2, 7
Copyright @ gdeepak.com®
2, 7
6, 7
6, 7
10
Encoding Trie
A code is a mapping of each character of an alphabet to a binary
code-word
A prefix code is a binary code such that no code-word is the prefix of
another code-word
An encoding trie represents a prefix code
Each leaf stores a character
The code word of a character is given by the path from the root to
the leaf storing the character (0 for a left child and 1 for a right child
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
11
Encoding Trie
00
010
011
10
11
a
b
c
d
E
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
12
Insertion and deletion
To insert a new key, the branching position can be found by
traversing the trie with the key one by one character until it gets
stuck. The state where there is no branch to go is the very place
to insert a new edge, labelled by the failing character.
To delete a key from the trie, all we need to do is delete the tail
block occupied by the key, and all double-array nodes belonging
exclusively to the key, without touching any node belonging to
other keys.
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
13
Applications
A common application of a trie is storing a dictionary, such as one
found on a mobile telephone. Such applications take advantage of a
trie's ability to quickly search for, insert, and delete entries
In particular cases Tries has advantages over hash tables and binary
search trees.
Looking up a key of length m takes worst case O(m) time
Tries facilitate longest prefix matching.
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
14
Questions, Comments and Suggestions
?
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
15
Question 1
Suffix Trie is used in
A) Boyer Moore String matching
B) Rabin-Karp String matching
C) Knuth-Morris-Pratt String Matching
D) Brute-Force String Matching
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
16
Question 2
Give a common application of Trie that you use daily.
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
17
Question 3
All the descendants of a node have a
common ______ of the string associated with that node
A) Value
B) Height
C) Prefix
D) Suffix
6/8/2012 3:00 AM
Copyright @ gdeepak.com®
18