Ders10 Data Structures-Tries
Ders10 Data Structures-Tries
TRİES
Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick
and K. Wayne of Princeton University.
TODAY
‣ Tries
‣ R-way tries
Tries
b s t
y 4 e h h
key value
by 4 a 6 l e 0 o e 5
sea 6
value for she in node
sells 1 l l r
corresponding to last
she 0 key character
shells 3 s 1 l e 7
shore 7
the 5 s 3
3
Search in a trie
get("shells")
b s t
y 4 e h h
a 6 l e 0 o e 5
l l r
s 1 l e 7
get("she")
b s t
y 4 e h h
a 6 l e 0 o e 5
s 3
5
Search in a trie
get("shell")
b s t
y 4 e h h
a 6 l e 0 o e 5
l l r
s 1 l e 7
no value associated
s 3 with last key character
6
(return null)
Search in a trie
get("shelter")
b s t
y 4 e h h
a 6 l e 0 o e 5
l l r
s 1 l e 7
s no link to 't'
3
(return null) 7
Insertion into a trie
put("shore", 7)
b s t
y 4 e h h
a 6 l e 0 o e 5
l l r
s 1 l e 7
s 3
8
Trie construction demo
trie
9
Trie construction demo
put("she", 0)
e 0
10
Trie construction demo
she
trie
e 0
11
Trie construction demo
she
trie
e 0
12
Trie construction demo
she
put("sells", 1)
e h
l e 0
s 1
13
Trie construction demo
she
sells
trie
e h
l e 0
s 1
14
Trie construction demo
she
sells
trie
e h
l e 0
s 1
15
Trie construction demo
she
sells
put("sea", 2)
e h
a 2 l e 0
s 1
16
Trie construction demo
she
sells
sea
trie
e h
a 2 l e 0
s 1
17
Trie construction demo
she
sells
sea
put("shells", 3)
e h
a 2 l e 0
l l
s 1 l
s 3
18
Trie construction demo
she
sells
sea
trie
e h
a 2 l e 0
l l
s 1 l
s 3
19
Trie construction demo
she
sells
sea
put("by", 4)
b s
y 4 e h
a 2 l e 0
l l
s 1 l
s 3
20
Trie construction demo
she
sells
sea
by
trie
b s
y 4 e h
a 2 l e 0
l l
s 1 l
s 3
21
Trie construction demo
she
sells
sea
by
put("the", 5)
b s t
y 4 e h h
a 2 l e 0 e 5
l l
s 1 l
s 3
22
Trie construction demo
she
sells
sea
by
the
trie
b s t
y 4 e h h
a 2 l e 0 e 5
l l
s 1 l
s 3
23
Trie construction demo
put("sea", 6)
b s t
y 4 e h h
a 2 6 l e 0 e 5
l l
overwrite
old value with
s 1 l
new value
s 3
24
Trie construction demo
trie
b s t
y 4 e h h
a 6 l e 0 e 5
l l
s 1 l
s 3
25
Trie construction demo
trie
b s t
y 4 e h h
a 6 l e 0 e 5
l l
s 1 l
s 3
26
Trie construction demo
she
sells
sea
by
the
put("shore", 7)
b s t
y 4 e h h
a 6 l e 0 o e 5
l l r
s 1 l e 7
s 3
27
Trie construction demo
she
sells
sea
by
the
shore
trie
b s t
y 4 e h h
a 6 l e 0 o e 5
l l r
s 1 l e 7
s 3
28
Trie representation: implementation
class Node
{ A child node for each character in Alphabet.
int value; No need to search for character, but a
Node next[R]; pointer reserved for each character in
} memory
29
R-way trie: implementation
Node root;
30
R-way trie: implementation (continued)
Node getNode(){
Node pNode = NULL;
pNode = new Node;
if (pNode){
for (int i = 0; i < R; i++)
pNode.next[i] = NULL;
}
return pNode;
}
31
R-way trie: implementation (continued)
32
Trie performance
Search miss.
• Could have mismatch on first character.
• Typical case: examine only a few characters (sublinear).
Bottom line. Fast search hit and even faster search miss, but wastes
space.
33
String symbol table implementations cost summary
space
implementation search hit Search miss insert
(references)
hashing
N N 1 N
(separate chaining)
R-way trie.
• Method of choice for small R.
• Too much memory for large R.
34