Está en la página 1de 11

Trie Data Structure

Suthahar.P

Assistant Professor
Department of Computer Science and Engineering
M.Kumarasamy College of Engineering (Autonomous)

Problem

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 e r a d d e
d s u t
d s u t y
= 6 slots
= 5 slots
= 7 slots
= 4 slots
= 5 slots
Idea

Take advantage of the common letters in


the words

Reduce redundancy

Use same letter for more than one word


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



The Trie Data Structure
Basic Definition:

A recursive tree structure that uses the digital


decomposition of strings to represent a set o
f strings for searching.

One of the advantages of the trie data struct


ure is that its tree depth depends on the amo
unt of data stored in it. Each element of data
is stored at the highest level of the tree that s
till allows a unique retrieval.

Example
u
d
e n
l
h
s t
o
g
l
i
e
m
go
shed
shell
sun
time
she
Make sure the word
she is not found

unless it has been
explicitly inserted!
example

A trie for keys "t", "to", "te", "tea", "ten",


"i", "in", and "inn".
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

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


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

También podría gustarte