Trie樹(字典樹)的介紹及Java實(shí)現(xiàn)
簡介
Trie樹,又稱為前綴樹或字典樹,是一種有序樹,用于保存關(guān)聯(lián)數(shù)組,其中的鍵通常是字符串。與二叉查找樹不同,鍵不是直接保存在節(jié)點(diǎn)中,而是由節(jié)點(diǎn)在樹中的位置決定。一個節(jié)點(diǎn)的所有子孫都有相同的前綴,也就是這個節(jié)點(diǎn)對應(yīng)的字符串,而根節(jié)點(diǎn)對應(yīng)空字符串。
它的主要特點(diǎn)如下:
根節(jié)點(diǎn)不包含字符,除根節(jié)點(diǎn)外的每一個節(jié)點(diǎn)都只包含一個字符。
從根節(jié)點(diǎn)到某一節(jié)點(diǎn),路徑上經(jīng)過的字符連接起來,為該節(jié)點(diǎn)對應(yīng)的字符串。
每個節(jié)點(diǎn)的所有子節(jié)點(diǎn)包含的字符都不相同。
如下是一棵典型的Trie樹:

Trie的來源是Retrieval,它常用于前綴匹配和詞頻統(tǒng)計??赡苡腥艘f了,詞頻統(tǒng)計簡單啊,一個hash或者一個堆就可以搞定,但問題來了,如果內(nèi)存有限呢?還能這么 玩嗎?所以這里我們就可以用trie樹來壓縮下空間,因?yàn)楣睬熬Y都是用一個節(jié)點(diǎn)保存的。
1、定義
這里為了簡化,只考慮了26個小寫字母。
首先是節(jié)點(diǎn)的定義:
public class TrieNode {
public TrieNode[] children;
public char data;
public int freq;
public TrieNode() {
//因?yàn)橛?6個字母
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)';來得知字符應(yīng)該放在哪個孩子中。
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é)點(diǎn)
移除操作中,需要對詞頻進(jìn)行減一操作。
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)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
IDEA?Error:java:無效的源發(fā)行版:13的解決過程
之前用idea運(yùn)行時,也會出現(xiàn)這種情況,后面通過網(wǎng)上的資料解決了這個問題,下面這篇文章主要給大家介紹了關(guān)于IDEA?Error:java:無效的源發(fā)行版:13的解決過程,需要的朋友可以參考下2023-01-01
Spring MVC完全注解方式配置web項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Spring MVC完全注解方式配置web項(xiàng)目的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
深入理解java異常處理機(jī)制的原理和開發(fā)應(yīng)用
Java異常處理機(jī)制在日常開發(fā)中應(yīng)用頻繁,本篇文章主要在基礎(chǔ)的使用方法上,更進(jìn)一步的,如何更加合理的使用異常機(jī)制,希望可以對各位朋友能有所幫助。2017-04-04
IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程
這篇文章主要介紹了IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
SpringBoot使用前綴樹實(shí)現(xiàn)敏感詞過濾示例
最近項(xiàng)目用到了敏感詞過濾,本文主要就來介紹一下SpringBoot使用前綴樹實(shí)現(xiàn)敏感詞過濾示例,具有一定的參考價值,感興趣的可以了解一下2023-10-10
Java實(shí)現(xiàn)Shazam聲音識別算法的實(shí)例代碼
Shazam算法采用傅里葉變換將時域信號轉(zhuǎn)換為頻域信號,并獲得音頻指紋,最后匹配指紋契合度來識別音頻。這篇文章給大家介紹Java實(shí)現(xiàn)Shazam聲音識別算法的實(shí)例代碼,需要的朋友參考下吧2018-09-09
實(shí)戰(zhàn)分布式醫(yī)療掛號通用模塊統(tǒng)一返回結(jié)果異常日志處理
這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之統(tǒng)一返回結(jié)果統(tǒng)一異常處理,統(tǒng)一日志處理到通用模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04
基于JPA實(shí)體類監(jiān)聽器@EntityListeners注解的使用實(shí)例
這篇文章主要介紹了JPA實(shí)體類監(jiān)聽器@EntityListeners注解的使用實(shí)例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

