Leetcode Implement Trie (Prefix Tree)

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/implement-trie-prefix-tree/

Trie is a data structure used to make dictionary lookups, which is a multi-fork number structure for fast retrieval. For example, the dictionary tree of the English alphabet is 26 forks, and the number of the dictionary tree is a 10-fork tree.

The basic properties of the trie tree are three points, which are summarized as:

      1. The root node does not contain characters, and each node outside the root node contains only one character.
      2. From the root node to a node, the characters that pass through the path are concatenated to the corresponding string for that node.
      3. All child nodes of each node contain different strings.

Note:search () and StartsWith () are different, and search also needs to be aware of whether the leaf node of the trie tree has been hit when jumping out of the loop.

AC Java:

1 classTrienode {2     //Mark If this node is the end of the word3     Booleanisleaf;4Hashmap<character,trienode>nexts;5      PublicTrienode () {6Nexts =NewHashmap<character,trienode>();7     }8 }9 Ten  Public classTrie { One     PrivateTrienode Root; A      -      PublicTrie () { -Root =NewTrienode (); the     } -  -     //inserts a word into the trie. -      Public voidInsert (String word) { +Trienode p =Root; -         Char[] s =Word.tochararray (); +         intLen =s.length; A         inti = 0; at         //traverse the existing character -          while(i<Len) { -             if(P.nexts.containskey (S[i])) { -p =P.nexts.get (S[i]); -i++; -}Else{ in                  Break; -             } to         } +         //Append New Nodes -          while(i<Len) { theTrienode NewNode =NewTrienode (); * P.nexts.put (s[i],newnode); $p =NewNode;Panax Notoginsengi++; -         } the         //Set the end of the word +P.isleaf =true; A     } the  +     //Returns If the word is in the trie. -      Public BooleanSearch (String word) { $Trienode p =Root; $         Char[] s =Word.tochararray (); -         intLen =s.length; -         inti = 0; the          while(i<Len) { -Trienode NextNode =P.nexts.get (S[i]);Wuyi             if(NextNode = =NULL){ the                 return false; -             } Wup =NextNode; -i++; About         } $         //return If this is a leaf node -         returnp.isleaf; -     } -  A     //Returns If there is any word in the trie +     //That's starts with the given prefix. the      Public BooleanstartsWith (String prefix) { -Trienode p =Root; $         Char[] s =Prefix.tochararray (); the         intLen =s.length; the         inti = 0; the          while(i<Len) { theTrienode NextNode =P.nexts.get (S[i]); -             if(NextNode = =NULL){ in                 return false; the             } thep =NextNode; Abouti++; the         } the         return true; the     } + } -  the //Your Trie object would be instantiated and called as such:Bayi //Trie Trie = new Trie (); the //Trie.insert ("somestring"); the //trie.search ("key");

Reference this post: Http://blog.csdn.net/wzy_1988/article/details/45744067#trie Tree Basic implementation

Leetcode Implement Trie (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.