(Data structure) Implement Trie and Add and Search Word

Source: Internet
Author: User

Implement Trie (Prefix Tree)

Implement a trie with insert , search , and startsWith methods.

Note:
Assume that all inputs is consist of lowercase lettersa-z

Solution:

classTrienode {//Initialize your data structure here.    BooleanIsend;    Trienode[] sons; Charvalue;  PublicTrienode () {isend=false; Sons=NewTrienode[26]; }} Public classTrie {PrivateTrienode Root;  PublicTrie () {root=NewTrienode (); }    //inserts a word into the trie.     Public voidInsert (String word) {if(Word = =NULL|| Word.length () = = 0)            return; Trienode P=Root;  for(inti = 0; I < word.length (); i++) {            intpos = Word.charat (i)-' a '; if(P.sons[pos] = =NULL) {P.sons[pos]=NewTrienode (); P.sons[pos].value=Word.charat (i); } P=P.sons[pos]; } p.isend=true; }    //Returns If the word is in the trie.     Public BooleanSearch (String word) {if(Word = =NULL|| Word.length () = = 0)            return false; Trienode P=Root;  for(inti = 0; I < word.length (); i++) {            intpos = Word.charat (i)-' a '; if(P.sons[pos] = =NULL)                return false; P=P.sons[pos]; }        returnP.isend; }    //Returns If there is any word in the trie//That's starts with the given prefix.     Public BooleanstartsWith (String prefix) {if(prefix = =NULL|| Prefix.length () = = 0)            return false; Trienode P=Root;  for(inti = 0; I < prefix.length (); i++) {            intpos = Prefix.charat (i)-' a '; if(P.sons[pos] = =NULL)                return false; P=P.sons[pos]; }        return true; }}//Your Trie object would be instantiated and called as such://Trie Trie = new Trie ();//Trie.insert ("somestring");//trie.search ("key");

Add and Search Word

Design a data structure that supports the following and the operations:

void Addword (Word) bool Search (word)

Search (Word) can search a literal word or a regular expression string containing only letters a-z or . . A . means it can represent any one letter.

For example:

Addword ("Bad") Addword ("Dad") Addword ("Mad") Search ("pad"), Falsesearch ("bad"), Truesearch (". Ad") Truesearch ("B..")-True

Note:
You may assume this all words is consist of lowercase letters a-z .

Solution:

 Public classWorddictionary {PrivateTode Root;  PublicWorddictionary () {root=NewTode (); }        //Adds A word into the data structure.     Public voidAddword (String word) {if(Word = =NULL|| Word.length () = = 0)            return ; Tode P=Root;  for(inti=0; I<word.length (); i++){            intpos = Word.charat (i)-' a '; if(P.sons[pos] = =NULL) {P.sons[pos]=NewTode (); P.sons[pos].value=Word.charat (i); } P=P.sons[pos]; } p.isend=true; }    //Returns If the word is in the data structure. A Word could//contain the dot character '. ' to represent.     Public BooleanSearch (String word) {if(Word = =NULL|| Word.length () = = 0)            return false; Char[] arr =Word.tochararray (); intindex = 0; Tode P=Root; returnGosearch (arr, index, p); }    Private BooleanGosearch (Char[] arr,intindex, Tode p) {        if(Index = =arr.length)returnP.isend; if(Arr[index] = = '. '){             for(Tode n:p.sons) {if(n! =NULL&& Gosearch (arr, index+1, N)) return true; }            return false; }Else{            intpos = Arr[index]-' a '; if(P.sons[pos]! =NULL)                returnGosearch (arr, index+1, P.sons[pos]); Else                return false; }    }    classtode{BooleanIsend;        Tode[] sons; Charvalue;  PublicTode () {isend=false; Sons=NewTode[26]; }    }}//Your Worddictionary object would be instantiated and called as such://worddictionary worddictionary = new Worddictionary ();//Worddictionary.addword ("word");//Worddictionary.search ("pattern");

(Data structure) Implement Trie and Add and Search Word

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.