Java for Leetcode 208 Implement Trie (Prefix Tree)

Source: Internet
Author: User

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

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

Problem Solving Ideas:

Reference Baidu Encyclopedia: Trie Tree

The detailed code has been given:

The Java implementation is as follows:

Class Trienode {//Initialize your data structure Here.int num;//How many words are passed through this node, that is, the number of occurrences of the node character trienode[] son;//all sons node Boolean I send;//is not the value of the last node Char val;//node Trienode () {this.num = 1;this.son = new Trienode[26];this.isend = false;}} public class Trie {private Trienode root;public Trie () {root = new Trienode ();} public void Insert (String word) {if (Word = = NULL | | word.length () = = 0) return; Trienode node = this.root;char[] Letters = Word.tochararray (); for (int i = 0; i < word.length (); i++) {Int. POS = Letter S[i]-' a ', if (node.son[pos] = = null) {Node.son[pos] = new Trienode (); node.son[pos].val = Letters[i];} else {Node.son[pos] . num++;} node = Node.son[pos];} Node.isend = true;} Returns If the word is in the Trie.public boolean search (String word) {if (Word = = NULL | | word.length () = = 0) {return false;} Trienode node = root;char[] Letters = Word.tochararray (); for (int i = 0; i < word.length (); i++) {int pos = letters[i] -' a '; if (node.son[pos]! = null) {node = Node.son[pos];} else {return false;}} return node.isend;} Returns If there is any word in the trie//this starts with the given Prefix.public boolean startsWith (String prefix) { if (prefix = = NULL | | prefix.length () = = 0) {return false;} Trienode node = root;char[] Letters = Prefix.tochararray (); for (int i = 0; i < prefix.length (); i++) {int pos = letters [i]-' a '; if (node.son[pos]! = null) {node = Node.son[pos];} else {return false;}} return true;}} Your Trie object would be instantiated and called as such://Trie Trie = new Trie ();//Trie.insert ("somestring");//Trie . Search ("key");

Java for Leetcode 208 Implement Trie (Prefix Tree)

Related Article

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.