22C:21 Problem 4.3 Solution Outline
Trie Store characters in each node, not keys Use characters of the key to guide the search process retrievalFrom retrieval, but pronounced “try”
Example u d en l h st 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!
Applications Spell checkers Data compression Computational biology Routing tables for IP addresses Storing/Querying XML documents … and many other
Building block - Class Node
Class Trie Specify root node Insert method public void insert (String s) Search method public boolean search (String s)
T.insert(“tick”) u d en l h st o g l i e m go shed shell sun time k c tick place a marker
T.insert(“tick”) u d en l h st o g l i e m go shed shell sun time k c tick
T.search(“group”) u d en l h st o g l i e m go shed shell sun time k c tick null Failure return false
T.search(“timer”) u d en l h st o g l i e m go shed shell sun time k c tick null Failure return false
T.search(“she”) u d en l h st o g l i e m go shed shell sun time k c tick String found. But no marker! Failure return false
T.search(“shed”) u d en l h st o g l i e m go shed shell sun time k c tick String found. Marker found! Success return true
Quiz 4: Problem 4.2 Draw a trie corresponding to the collection of words: {abacus, abode, dreaded, dust, dusty, planar, east} Write a main() method that defines a trie and performs successive insertions of the above strings.