leetcode-trie树实现 发表于 2016-08-21 | 分类于 算法 , leetcode | 题目大意 https://leetcode.com/problems/implement-trie-prefix-tree/ trie树的实现 题目分析 trie树,实现插入,搜索,复杂度都是o(len),len为单词的长度 代码 Java版本 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576class TrieNode { public TrieNode[] childNodes; public int freq; public char nodeChar; // Initialize your data structure here. public TrieNode() { childNodes = new TrieNode[26]; freq = 0; }}public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { if(word.length() == 0) { return; } TrieNode cur = root; for(int i = 0; i < word.length(); i++) { int k = word.charAt(i) - 'a'; if(cur.childNodes[k] == null) { cur.childNodes[k] = new TrieNode(); cur.childNodes[k].nodeChar = word.charAt(i); } cur = cur.childNodes[k]; if(i == word.length() - 1) { cur.freq ++; } } } // Returns if the word is in the trie. //root->'a'->'b'->'c' public boolean search(String word) { if(word.length() == 0) { return false; } TrieNode cur = root; for(int i = 0; i < word.length(); i++) { int k = word.charAt(i) - 'a'; if(cur.childNodes[k] == null) { return false; } cur = cur.childNodes[k]; } if(cur.freq > 0) { return true; } else { return false; } } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { if(prefix.length() == 0) { return false; } TrieNode cur = root; for(int i = 0; i < prefix.length(); i++) { int k = prefix.charAt(i) - 'a'; if(cur.childNodes[k] == null) { return false; } cur = cur.childNodes[k]; } return true; }} - C++版本 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182class TrieNode {public: // Initialize your data structure here. char nodeChar; int freq; TrieNode* childNodes[26]; TrieNode() { for(int i = 0; i < 26; i++) { childNodes[i] = NULL; } freq = 0; }};class Trie {public: Trie() { root = new TrieNode(); } // Inserts a word into the trie. void insert(string word) { int s = word.length(); if(s == 0) { return; } TrieNode* cur = root; for(int i = 0; i < s; i++) { int k = word[i] - 'a'; if((cur->childNodes)[k] == NULL) { (cur->childNodes)[k] = new TrieNode(); (cur->childNodes)[k]->nodeChar = word[i]; } cur = (cur->childNodes)[k]; if(i == s - 1) { cur->freq ++; } } } // Returns if the word is in the trie. bool search(string word) { int s = word.length(); if(s == 0) { return false; } TrieNode* cur = root; for(int i = 0; i < s; i++) { int k = word[i] - 'a'; if((cur->childNodes)[k] == NULL) { return false; } cur = (cur->childNodes)[k]; } if(cur->freq > 0) { return true; } else { return false; } } // Returns if there is any word in the trie // that starts with the given prefix. bool startsWith(string prefix) { int s = prefix.length(); if(s == 0) { return false; } TrieNode* cur = root; for(int i = 0; i < s; i++) { int k = prefix[i] - 'a'; if((cur->childNodes)[k] == NULL) { return false; } cur = (cur->childNodes)[k]; } return true; } private: TrieNode* root;};