日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Trie

發布時間:2025/5/22 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

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