Trie
關于Trie的幾種實現方式
children node的存儲
1可以用hashmap來存儲children,HashMap<Character, TrieNode> children, 優勢是利用contains函數便于查找
2用數組?TrieNode[] children; 通過index來查找,最多也就26個char(認為忽略大小寫)
Search 函數:iterative 即可,可以將search和startwith合并
My solution :HashMap + iterative
class TrieNode {// Initialize your data structure here.private char value;private boolean isend;// mark whether is end node//children of the nodepublic HashMap<Character, TrieNode> children;public TrieNode() {isend = false;children = new HashMap<Character, TrieNode>();}public TrieNode(char c){value = c;isend = false;children = new HashMap<Character, TrieNode>();}public boolean isEnd(){return isend;}public void setEnd(){isend = true;} }public class Trie {private TrieNode root;public Trie() {root = new TrieNode();}// Inserts a word into the trie.public void insert(String word) {if(word == null || word.length() == 0) return;//bfs level insertTrieNode cur = root;for(int i = 0; i < word.length(); i++){char c = word.charAt(i);HashMap<Character, TrieNode> child = cur.children;if(child.containsKey(c)){//no need insertcur = child.get(c);}else{//insert a new trie nodeTrieNode newchild = new TrieNode(c);child.put(c, newchild);cur = newchild;}}//mark end cur.setEnd();}// Returns if the word is in the trie.public boolean search(String word) {if(word == null || word.length() == 0) return false;TrieNode cur = root;for(int i = 0; i < word.length(); i++){char c = word.charAt(i);HashMap<Character, TrieNode> child = cur.children;if(!child.containsKey(c)){//no char return false;}else{//continue searchcur = child.get(c);}}return cur.isEnd();}// Returns if there is any word in the trie// that starts with the given prefix.public boolean startsWith(String prefix) {if(prefix == null || prefix.length() == 0) return false;TrieNode cur = root;for(int i = 0; i < prefix.length(); i++){char c = prefix.charAt(i);HashMap<Character, TrieNode> child = cur.children;if(!child.containsKey(c)){//no char return false;}else{//continue searchcur = child.get(c);}}return true;}}// Your Trie object will be instantiated and called as such: // Trie trie = new Trie(); // trie.insert("somestring"); // trie.search("key"); View Code?
其他解法
class TrieNode {// Initialize your data structure here.private char value;private boolean isend;// mark whether is end node//children of the nodepublic TrieNode[] children;public TrieNode() {isend = false;children = new TrieNode[26];}public TrieNode(char c){value = c;isend = false;children = new TrieNode[26];}public boolean isEnd(){return isend;}public void setEnd(){isend = true;} } public class Trie {private TrieNode root;public Trie() {root = new TrieNode();}// Inserts a word into the trie.public void insert(String word) {if(word == null || word.length() == 0) return;//bfs level insertTrieNode cur = root;for(int i = 0; i < word.length(); i++){char c = word.charAt(i);int index = c - 'a';if(cur.children[index] == null){//insert a new nodecur.children[index] = new TrieNode(c);}cur = cur.children[index]; // go down }//mark end cur.setEnd();}// Returns if the word is in the trie.public boolean search(String word) {if(word == null || word.length() == 0) return false;TrieNode cur = root;for(int i = 0; i < word.length(); i++){char c = word.charAt(i);int index = c - 'a';if(cur.children[index] == null){//no such charreturn false;}cur = cur.children[index];}return cur.isEnd();}// Returns if there is any word in the trie// that starts with the given prefix.public boolean startsWith(String prefix) {if(prefix == null || prefix.length() == 0) return false;TrieNode cur = root;for(int i = 0; i < prefix.length(); i++){char c = prefix.charAt(i);int index = c - 'a';if(cur.children[index] == null){//no such charreturn false;}cur = cur.children[index];}return true;}} View Code?
轉載于:https://www.cnblogs.com/xiaomaoliu/p/4548195.html
總結
- 上一篇: ajax详解
- 下一篇: thinkphp3.2定义多模块并设置默