Lecture 12 : Trie Data Structure - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 12 : Trie Data Structure

Description:

Store this collection of words in a memory efficient way : {abacus, abode, dreaded, dust, dusty, planar, east} Arrays. a. a. b. c. u. s. a. o. b. d. e. d ... – PowerPoint PPT presentation

Number of Views:2302
Avg rating:3.0/5.0
Slides: 11
Provided by: cau2
Category:

less

Transcript and Presenter's Notes

Title: Lecture 12 : Trie Data Structure


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

2
Problem
  • Store this collection of words in a memory
    efficient way
  • abacus, abode, dreaded, dust, dusty, planar,
    east

3
Arrays
a
a
b
c
u
s
6 slots
a
o
b
d
e
5 slots
d
e
r
a
d
d
e
7 slots
d
s
u
t
4 slots
d
s
u
t
y
5 slots
4
Idea
  • Take advantage of the common letters in the words
  • Reduce redundancy
  • Use same letter for more than one word

5
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 retrieval, but pronounced try

6
Example
g
s
t
i
o
u
h
go
m
e
n
she
Make sure the word she is not found unless
it has been explicitly inserted!
sun
d
l
e
shed
time
l
shell
7
example
  • A trie for keys "t", "to", "te", "tea", "ten",
    "i", "in", and "inn".

8
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

9
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

10
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
Write a Comment
User Comments (0)
About PowerShow.com