Ders10 Data Structures-Tries

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

BBS516– DATA STRUCTURES

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

Tries. [from retrieval, but pronounced "try"]


• Store characters in nodes (not keys). for now, we do not
draw null links
• Each node has R children, one for each possible character.
• Store values in nodes corresponding to last characters in keys.
link to trie for all keys
root that start with s
link to trie for all keys
that start with she

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

Follow links corresponding to each character in the key.


• Search hit: node where search ends has a non-null value.
• Search miss: reach a null link or node where search ends has null value.

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

return value associated


s 3 with last key character
4
Search in a trie

Follow links corresponding to each character in the key.


• Search hit: node where search ends has a non-null value.
• Search miss: reach a null link or node where search ends has null value.

get("she")

b s t

y 4 e h h

a 6 l e 0 o e 5

r search may terminated


l l
at an intermediate node
(return 0)
s 1 l e 7

s 3
5
Search in a trie

Follow links corresponding to each character in the key.


• Search hit: node where search ends has a non-null value.
• Search miss: reach a null link or node where search ends has null value.

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

Follow links corresponding to each character in the key.


• Search hit: node where search ends has a non-null value.
• Search miss: reach a null link or node where search ends has null value.

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

Follow links corresponding to each character in the key.


• Encounter a null link: create new node.
• Encounter the last character of the key: set value in that node.

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

key is sequence value is in node


of characters from corresponding to
root to value last character

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

Node. A value, plus references to R nodes.

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

#define R 256 extended ASCII

Node root;

put(root, key, val, 0);

void put(Node x, char key, int val, int d)


{
if (x == null)
x = getNode();
if (d ==strlen(key)) {x.value = val; return;}
char c = key[d];
put((x.next[c]), key, val, d+1);
}

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)

int get(Node x, char key, int d)


{
if (x == null) return -1; //-1 refers no match
if (d == strlen(key))
return x.value;
char c = key[d];
return get(x.next[c], key, d+1);
}

32
Trie performance

Search hit. Need to examine all L characters for equality.

Search miss.
• Could have mismatch on first character.
• Typical case: examine only a few characters (sublinear).

Space. R null links at each leaf.


(but sublinear space possible if many short strings share common
prefixes)

Bottom line. Fast search hit and even faster search miss, but wastes
space.

33
String symbol table implementations cost summary

character accesses (typical case)

space
implementation search hit Search miss insert
(references)

hashing
N N 1 N
(separate chaining)

R-way trie L log R N L RNw

N = number of entries, L= key length,


R= alphabet size, w= average key length

R-way trie.
• Method of choice for small R.
• Too much memory for large R.

Challenge. Use less memory, e.g., 65,536-way trie for Unicode!

34

You might also like