欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年02月04日 10:04:54   作者:小樓一夜聽春雨  
Trie樹,又稱字典樹或前綴樹,關于它的結(jié)構就不詳細介紹了。Trie樹在單詞統(tǒng)計、前綴匹配等很多方面有很大用處。下面這篇文章主要介紹了Trie樹,以及Java實現(xiàn)如何Trie樹,有需要的朋友可以參考借鑒,下面來一起看看吧。

簡介

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)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關文章

最新評論