Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and...

10
Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Transcript of Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and...

Page 1: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Lecture 12 : Trie Data Structure

Bong-Soo Sohn

Assistant ProfessorSchool of Computer Science and Engineering

Chung-Ang University

Page 2: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Problem

Store this collection of words in a memory efficient way :

{abacus, abode, dreaded, dust, {abacus, abode, dreaded, dust, dusty, planar, east}dusty, planar, east}

Page 3: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Arrays

a ab c u s

a ob d e

d er a d de

d su t

d su t y

= 6 slots

= 5 slots

= 7 slots

= 4 slots

= 5 slots

Page 4: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Idea

Take advantage of the common letters in the words

Reduce redundancy Use same letter for more than one word

Page 5: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Trie

Store characters (digits) in each node, not keys. Path from root to a node is associated with a key Use characters of the key to guide the search

process All the descendants of a node have a common

prefix of the string associated with that node

From reretrietrievalval, but pronounced “try”

Page 6: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

ExampleExample

u

d

e n

l

h

s t

o

g

l

i

e

m

go

shed

shell

sun

time

sheMake sure the word“she” is not found…

…unless it has been explicitly inserted!

Page 7: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

example

A trie for keys "t", "to", "te", "tea", "ten", "i", "in", and "inn".

Page 8: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Advantages

Looking up keys is faster. O(m) time. A BST takes O(log n) time m : length of key , n : # of elements In the worst case, log(n) will approach m Tries can require less space when they contain a

large number of short strings Longest prefix matching is possible

finding the key sharing the longest possible prefix of characters all unique

Fast search/insert/delete

Page 9: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Drawbacks

Tries can be slower in some cases. Especially when the tree is accessed from

disk

It is not easy to represent all keys as strings Floating point numbers (1.0 , +1.0, 1 …) Inefficient when a key is too long

Page 10: Lecture 12 : Trie Data Structure Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Applications

Can replace a hash table Only take O(m) , m : length of key No collision No need to provide hash function Can provide an alphabetical ordering of keys

Dictionary IP routing

Routing : process of selecting paths in a network along which to send network traffic

when given a destination address, find next place(place) to go. Internet router table is a collection of rules of the form (P, NH),

P : prefix, NH : next hop