国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Trie樹(字典樹)的介紹及Java實現

2019-11-26 13:09:17
字體:
來源:轉載
供稿:網友

簡介

Trie樹,又稱為前綴樹或字典樹,是一種有序樹,用于保存關聯數組,其中的鍵通常是字符串。與二叉查找樹不同,鍵不是直接保存在節點中,而是由節點在樹中的位置決定。一個節點的所有子孫都有相同的前綴,也就是這個節點對應的字符串,而根節點對應空字符串。

它的主要特點如下:

根節點不包含字符,除根節點外的每一個節點都只包含一個字符。

從根節點到某一節點,路徑上經過的字符連接起來,為該節點對應的字符串。

每個節點的所有子節點包含的字符都不相同。

如下是一棵典型的Trie樹:

Trie的來源是Retrieval,它常用于前綴匹配和詞頻統計。可能有人要說了,詞頻統計簡單啊,一個hash或者一個堆就可以搞定,但問題來了,如果內存有限呢?還能這么 玩嗎?所以這里我們就可以用trie樹來壓縮下空間,因為公共前綴都是用一個節點保存的。

1、定義

這里為了簡化,只考慮了26個小寫字母。

首先是節點的定義:

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';來得知字符應該放在哪個孩子中。

 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、移除節點

移除操作中,需要對詞頻進行減一操作。

 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(); }

測試結果如下:

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贺州市| 海南省| 宁远县| 辉南县| 金门县| 阿城市| 任丘市| 察哈| 宁城县| 澜沧| 洞头县| 扎赉特旗| 安岳县| 大同县| 夏邑县| 波密县| 嵩明县| 沙田区| 巫溪县| 琼海市| 如东县| 明溪县| 嵊泗县| 巍山| 莆田市| 尼勒克县| 玉林市| 方山县| 韶关市| 镇远县| 徐州市| 嵊泗县| 双鸭山市| 蒲江县| 工布江达县| 安义县| 宁安市| 武穴市| 金秀| 五家渠市| 深州市|