Professional Documents
Culture Documents
What isatrie?
Thetermtriecomesfrom(information)retrieval
Thetriedata
anspecialized
structure
(very
(abstract
data
type)is
efficient)implementation
of
node) islabeledwith
acharacterc
Theroothasnolabel
Eachinternal nodeofTcanhave || # of keys
Thekeysare
stored
aninternal node
inalphabetical
orderinside
exactlyone string in S
Example:
S={ bear, bell, bid, bull, buy, sell, stock, stop } (s=8)
Howtoimplementatatrie
Useanarrayofreferences
Useabinary tree
Useanarrayofreferences
Eacharray elementrepresentoneletterofthealphabet
Eacharray
referencewill
point
to
asub-triethat
correspondstostringsthatstartswiththecorresponding
letter
Thebinary treeimplementation:
Thebinary
treeimplementation
of
atrieis
known
as
abitwise trie
Theimplementationusesthealphabet: = {0, 1}
Inotherwords,theimplementationstoresasequences of bits
Thekeysarereadasasequence of bits
Example:
Thisfollowsfromthewaythatthetrieisconstructed
Structuralpropertiesofthestandardtrie
ThetrieTonthesetSwithsstrings(keys)hasexactlys
externalnodes
Thisisbecause
apathfromtheroot
nodetoone extern
al nodecorrespond
sto1 key
Structuralpropertiesofthestandardtrie
The height of the trie T on the set S = the length of the
longeststringS
longest path=longest key S
Thisisbecauseapathfromtherootnodetooneexternalnode
correspondsto1key
Structuralpropertiesofthestandardtrie
The number of nodes in the trie T on the set S = O(n),
wheren=#charactersinthestringsS
In
theworst
case,every
thekeysaredifferent
Example :
S={ try, die, flaw, pack }
characterin
trie:"sto":
"ck":
AdvantagesofTriesoveranordinarymap
Lookingupdatainatrieisintheworst case = O(m), where
table!!!
Unlike a hash table, a trie can provide an alphabetical
orderingoftheentriesbykey
(i.e., A trie implements an ordered map while a hash table
cannot!)
Handlingkeysthatareprefixesofanotherkey
Thestandard triehasthepropertythat:
Onlytheexternalnodescanstoreinformation
(Thepathformedbytheinternalnodesrepresentsthekey)
Whenakey(string)isaprefixofanotherkey,thepathofthefirst
keywouldendinaninternalnode
Example:atandate
Handlingkeysthatareprefixesofanotherkey
Solution
Addaspecialterminationsymbol tothealphabet
Theterminationsymbolhasthelower value inthe
alphabet
I.e.,:terminationsymbolprecedeseverycharacterinthe
alphabet
Weappendtheterminationsymboltoeachkeywordstored
inthetrie
WetypicallyusetheNULcharacter'\0'asterminationsymbol
APPLICATIONSOFTRIEDATASTRUCTURES
TRIESINAUTOCOMPLETE
Since a trie is a tree-like data structure in which
AUTOCOMPLETE
Auto-complete functionality is used widely over
AUTOCOMPLETEINGOOGLESEARCH
WHYTRIESINAUTOCOMPLETE
Implementing auto complete using a trie is easy.
We simply trace pointers to get to a node that
represents
the
string
the
user
entered.
By
CRIMINOLOGY
Suppose that you are at the scene of a crime and
AUTOMATICCOMMANDCOMPLETION
When using an operating system such as Unix or
Commandsthathavetheprefixps
ps2ascii
ps2pdf
psbook
ps2epsi
ps2pk
pscal
ps2frag
ps2ps
psidtopgm
ps2gif
psbb
pslatex
psmandup
psmerge
psnup
psresize
psselect
pstopnm
pstops
pstruct
LONGESTPREFIXMATCHING
Longest prefix match (also called Maximum prefix length match)
destination address may match more than one routing table entry.
The most specific table entry the one with the highest subnet
mask is called the longest prefix match. It is called this because
it is also the entry where the largest number of leading address
bits in the table entry match those of the destination address.
SPELLCHECKERS
SPELLCHECKERS
There are many appropriate data structures to
SpellCheck..
SpellCheck..
a
bc
0 p
0
a
1
a
page
pug
pig
peg
pest
PHONEBOOKSEARCH..
Trie data structure are mostly used to search for a
Example
a
bc
0
t
a
alberto
ram
1
a
sanka
r
alberto
ram
sankar
star
stella
2
star
stella
PHONEBOOKSEARCH..
Suffix Matching
Can be used to index all
suffixes in a text in order
to carry out fast full text
searches.
TRIESINT9
T9 is a technology used on many mobile phones to make
in its dictionary, and will add that word. Others keep track of
the frequency of certain words and favor those words over
other words that have the same sequence of keypresses.
TRIESINT9
How does a T9 dictionary work?
It can be implemented in several ways, one of
TRIESINT9
It can be implemented using nested hash tables
Application
Spellcheckers.
Datacompression.
PrincetonU-CALL.
Computationalbiology.
RoutingtablesforIPaddresses.
StoringandqueryingXMLdocuments.
Associativearrays,associativeindexing.
Modernapplication:invertedindexofWeb.
Inserteachwordofeverywebpageintotrie,storingURLlist
inleaves.
Findquerykeywordsintrie,andtakeintersectionofURLlists.
UsePagerankalgorithmtorankresultingwebpages.
Reference
http://www.mathcs.emory.edu/~
cheung/Courses/323/Syllabus/Text/trie01.html