Implement Tries (Prefix Tree)

Source: Internet
Author: User

1 classTrienode {2     //Initialize your data structure here.3Trienode[] Children =NewTrienode[26];4     BooleanIsword;5      PublicTrienode () {6     }7 }8 9  Public classTrie {Ten     PrivateTrienode Root; One  A      PublicTrie () { -Root =NewTrienode (); -     } the  -     //inserts a word into the trie. -      Public voidInsert (String word) { -Trienode current =Root; +          for(inti = 0; I < word.length (); i++) { -             if(Current.children[word.charat (i)-' a '] = =NULL) { +Current.children[word.charat (i)-' a '] =NewTrienode (); A             } atCurrent = Current.children[word.charat (i)-' a ']; -         } -Current.isword =true; -     } -  -     //Returns If the word is in the trie. in      Public BooleanSearch (String word) { -Trienode current =Root; to          for(inti = 0; I < word.length (); i++) { +             if(Current.children[word.charat (i)-' a '] = =NULL) { -                 return false; the             } *Current = Current.children[word.charat (i)-' a ']; $         }Panax Notoginseng         returnCurrent.isword; -     } the  +     //Returns If there is any word in the trie A     //That's starts with the given prefix. the      Public BooleanstartsWith (String prefix) { +Trienode current =Root; -          for(inti = 0; I < prefix.length (); i++) { $             if(Current.children[prefix.charat (i)-' a '] = =NULL) { $                 return false; -             } -Current = Current.children[prefix.charat (i)-' a ']; the         } -         return true;Wuyi     } the } -  Wu //Your Trie object would be instantiated and called as such: - //Trie Trie = new Trie (); About //Trie.insert ("somestring"); $ //trie.search ("key");

Implement Tries (Prefix Tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.