Trie樹(字典樹)的介紹及Java實現(xiàn)
簡介
Trie樹,又稱為前綴樹或字典樹,是一種有序樹,用于保存關聯(lián)數(shù)組,其中的鍵通常是字符串。與二叉查找樹不同,鍵不是直接保存在節(jié)點中,而是由節(jié)點在樹中的位置決定。一個節(jié)點的所有子孫都有相同的前綴,也就是這個節(jié)點對應的字符串,而根節(jié)點對應空字符串。
它的主要特點如下:
根節(jié)點不包含字符,除根節(jié)點外的每一個節(jié)點都只包含一個字符。
從根節(jié)點到某一節(jié)點,路徑上經(jīng)過的字符連接起來,為該節(jié)點對應的字符串。
每個節(jié)點的所有子節(jié)點包含的字符都不相同。
如下是一棵典型的Trie樹:
Trie的來源是Retrieval,它常用于前綴匹配和詞頻統(tǒng)計??赡苡腥艘f了,詞頻統(tǒng)計簡單啊,一個hash或者一個堆就可以搞定,但問題來了,如果內(nèi)存有限呢?還能這么 玩嗎?所以這里我們就可以用trie樹來壓縮下空間,因為公共前綴都是用一個節(jié)點保存的。
1、定義
這里為了簡化,只考慮了26個小寫字母。
首先是節(jié)點的定義:
public class TrieNode { public TrieNode[] children; public char data; public int freq; public TrieNode() { //因為有26個字母 children = new TrieNode[26]; freq = 0; } }
然后是Trie樹的定義:
public class TrieTree { private TrieNode root; public TrieTree(){ root=new TrieNode(); } ... }
2、插入
由于是26叉樹,故可通過charArray[index]-‘a(chǎn)';來得知字符應該放在哪個孩子中。
public void insert(String word){ if(TextUtils.isEmpty(word)){ return; } insertNode(root,word.toCharArray(),0); } private static void insertNode(TrieNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k<0||k>25){ throw new RuntimeException("charArray[index] is not a alphabet!"); } if(rootNode.children[k]==null){ rootNode.children[k]=new TrieNode(); rootNode.children[k].data=charArray[index]; } if(index==charArray.length-1){ rootNode.children[k].freq++; return; }else{ insertNode(rootNode.children[k],charArray,index+1); } }
3、移除節(jié)點
移除操作中,需要對詞頻進行減一操作。
public void remove(String word){ if(TextUtils.isEmpty(word)){ return; } remove(root,word.toCharArray(),0); } private static void remove(TrieNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k<0||k>25){ throw new RuntimeException("charArray[index] is not a alphabet!"); } if(rootNode.children[k]==null){ //it means we cannot find the word in this tree return; } if(index==charArray.length-1&&rootNode.children[k].freq >0){ rootNode.children[k].freq--; } remove(rootNode.children[k],charArray,index+1); }
4、查找頻率
public int getFreq(String word){ if(TextUtils.isEmpty(word)){ return 0; } return getFreq(root,word.toCharArray(),0); } private static int getFreq(TrieNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k<0||k>25){ throw new RuntimeException("charArray[index] is not a alphabet!"); } //it means the word is not in the tree if(rootNode.children[k]==null){ return 0; } if(index==charArray.length-1){ return rootNode.children[k].freq; } return getFreq(rootNode.children[k],charArray,index+1); }
5、測試
測試代碼如下:
public static void test(){ TrieTree trieTree=new TrieTree(); String sourceStr="Democratic presumptive nominee Hillary Clintons campaign posed pounced on Trumps assertion that British term monetary turmoil might benefit his business venture in Scotland"; //String sourceStr="the that"; sourceStr=sourceStr.toLowerCase(); String[]strArray=sourceStr.split(" "); for(String str:strArray){ trieTree.insert(str); } String sourceStr2="Every president is tested by world events But Donald Trump thinks about how is his golf resort can profit from that"; sourceStr2=sourceStr2.toLowerCase(); String[]strArray2=sourceStr2.split(" "); for(String str:strArray2){ trieTree.insert(str); } BinaryTree.print("frequence of 'that':"+trieTree.getFreq("that")); BinaryTree.print("\nfrequence of 'donald':"+trieTree.getFreq("donald")); trieTree.remove("that"); BinaryTree.print("\nafter remove 'that' once,freq of 'that':"+trieTree.getFreq("that")); trieTree.remove("that"); BinaryTree.print("\nafter remove 'that' twice,freq of 'that':"+trieTree.getFreq("that")); trieTree.remove("donald"); BinaryTree.print("\nafter remove 'donald' once,freq of 'donald':"+trieTree.getFreq("donald")); BinaryTree.reallyStartPrint(); }
測試結(jié)果如下:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關文章
IDEA?Error:java:無效的源發(fā)行版:13的解決過程
之前用idea運行時,也會出現(xiàn)這種情況,后面通過網(wǎng)上的資料解決了這個問題,下面這篇文章主要給大家介紹了關于IDEA?Error:java:無效的源發(fā)行版:13的解決過程,需要的朋友可以參考下2023-01-01IDEA設置JVM可分配內(nèi)存大小和其他參數(shù)的教程
這篇文章主要介紹了IDEA設置JVM可分配內(nèi)存大小和其他參數(shù)的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01SpringBoot使用前綴樹實現(xiàn)敏感詞過濾示例
最近項目用到了敏感詞過濾,本文主要就來介紹一下SpringBoot使用前綴樹實現(xiàn)敏感詞過濾示例,具有一定的參考價值,感興趣的可以了解一下2023-10-10實戰(zhàn)分布式醫(yī)療掛號通用模塊統(tǒng)一返回結(jié)果異常日志處理
這篇文章主要為大家介紹了實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之統(tǒng)一返回結(jié)果統(tǒng)一異常處理,統(tǒng)一日志處理到通用模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04基于JPA實體類監(jiān)聽器@EntityListeners注解的使用實例
這篇文章主要介紹了JPA實體類監(jiān)聽器@EntityListeners注解的使用實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08