Lecture 19
Tries
Linked Stac List
Queu k e
Hea p
Priority Queue
Hash Table
BST AVL Multiway Red-Black Algorithm Analysis
Tools
Is finding a pattern same as finding a
word in the
dictionary?
How fast can
you search for a word in a set of
words?
How to make
ordered tree for
strings?
Standard Tries
• Except root, each node is labeled with a character
• The children of a node are alphabetically ordered
• The path from root to external nodes yields the string
a
e
b
r
l l
s u
l l
y
e t
l l
o c
k
p i
d
S = { bear, bell, bid, bull, buy, sell, stock, stop }
Insert
• Assumption: no word is prefix of other word!
• What if this is not the
case?
Time complexity
• n total size of the strings in S
• m size of given string
• d size of the alphabet
• s number of words
Search?
O(m)
Insert & delete?
O(md)
Word Matching with a Trie
s e e b e a r ? s e l l s t o c k ! s e e b u l l ? b u y s t o c k ! b i d s t o c k !
a a
h e t h e b e l l ? s t o p ! b i d s t o c k !
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2425 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
a r
87 88
a
e
b
l
s u
l
e t
e 0, 24
o c
i
l r
6
l 78
d 47, 58
l 30
y 36
l
12 k
17, 40, 51, 62
p 84 h
e
r 69
a
How many
nodes do we need?
Can we reduce the
number of nodes?
a
e
b
r
l l
s u
l l
y
e t
l l
o c
k
p i
d
e
b
ar ll
s u
ll y
ell to
ck p
id
Compressed
Tries
Compressed Tries
• A compressed trie has internal nodes of degree at least two
• It is obtained from standard trie by compressing chains of
“redundant” nodes
• A redundant node is the one that has only one child
How many nodes do I have in a
compressed Trie?
Compressed Trie Properties
• Every internal node of T has at least two children and at most d children
• T has s leaf nodes
• The number of internal nodes of T is O(s)
s e e b e a r s e l l s t o c k
b u l l b u y b i d
h e b e l l s t o p
0 1 2 3 4
a r
S[0] = S[1] = S[2] = S[3] =
S[4] = S[5] = S[6] =
S[7] = S[8] = S[9] =
0 1 2 3 0 1 2 3
1,0,0
1,1,1 4,1,1 0,1,1 3,1,2
0,0,0 7,0,3
6,1,2
1,2,3 8,2,3 4,2,3 5,2,2 0,2,2 2,2,3 3,3,4 9,3,3
e
b
ar ll
s u
ll y
ell to
ck p
id
hear
e ll
e
Compact
Representation of Tries
• 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
Insertion/Deletion in Compressed Trie
f g
reat one
u or
n r
insert green
f g
reat one
u or
n r
insert green
search ends here
f g
re one
u or
n r at en
Suffix Trie
e nimize
nimize ze
ze
i mi
mize nimize ze
m i n i m i z e 0 1 2 3 4 5 6 7
7
3 1 5 0 4
2 6
Suffix Trie
• Not all strings are guaranteed to have corresponding suffix trie.
• For example: xabxa
a bxa bxa
xa
bxa
a xa
bxa
bxa $ $ bxa
Pattern Matching Using Suffix Trie
Compressed Trie
Auxiliary Trie
How much space do we need to make a suffix
trie of n character
string?
To Trie or Not to Trie?
What would you use to store
phonebook?
Constructing a Suffix Trie
• S[1..n] is the string
• Start with a single edge for S
• Enter the edges for the suffix S[i..n]
where i goes from 2 to n
– Starting at the root node find the
longest part from the root whose label matches a prefix of S[i..n]. At some
point, no further matches are possible
• If the point is at a node, then denote this node by w
• If it is in the middle of an edge, then insert a new node called w, at this point
• Create a new edge running from w to a new leaf labeled S[i..n]
Example
minimize
minimize minimize
minimize inimize
minimize
minimize
minimize
inimize nimize
minimize nimize
mize nimize
i
Example
minimize
minimize nimize
mize nimize
i
minimize
nimize
mize nimize
i
inimize ize
m
Example
e nimize
nimize ze
ze
i mi
mize nimize ze
m i n i m i z e 0 1 2 3 4 5 6 7
7
3 1 5 0 4
2 6