日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Trie

發布時間:2025/5/22 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Trie的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。