Vous êtes sur la page 1sur 10

Tries ADT

m.burce deptofcompe
• A trie is a compact data structure for representing a set of
strings, such as all the words in a text
– A tries supports pattern matching queries in time proportional to the
pattern size

• Basic definition: a recursive tree structure that


uses the digital decomposition of strings to
represent a set of strings for searching

m.burce deptofcompe
• Store this collection of words in a memory
efficient way :

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

m.burce deptofcompe
Arrays
a b a c u s = 6 slots

a b o d e = 5 slots

d r e a d e d = 7 slots

d u s t = 4 slots
d u s t y = 5 slots
m.burce deptofcompe
Strategy
• Take advantage of the common letters in the
words

• Reduce redundancy
• Use same letter for more than one word

m.burce deptofcompe
Tries
• 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”

m.burce deptofcompe
Example
{abacus, abode, dreaded, dust, dusty, planar, east}

m.burce deptofcompe
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

m.burce deptofcompe
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

m.burce deptofcompe
Applications
• 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

m.burce deptofcompe

Vous aimerez peut-être aussi