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

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

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

簡(jiǎn)介

Trie樹,又稱為前綴樹或字典樹,是一種有序樹,用于保存關(guān)聯(lián)數(shù)組,其中的鍵通常是字符串。與二叉查找樹不同,鍵不是直接保存在節(jié)點(diǎn)中,而是由節(jié)點(diǎn)在樹中的位置決定。一個(gè)節(jié)點(diǎn)的所有子孫都有相同的前綴,也就是這個(gè)節(jié)點(diǎn)對(duì)應(yīng)的字符串,而根節(jié)點(diǎn)對(duì)應(yīng)空字符串。

它的主要特點(diǎn)如下:

根節(jié)點(diǎn)不包含字符,除根節(jié)點(diǎn)外的每一個(gè)節(jié)點(diǎn)都只包含一個(gè)字符。

從根節(jié)點(diǎn)到某一節(jié)點(diǎn),路徑上經(jīng)過的字符連接起來,為該節(jié)點(diǎn)對(duì)應(yīng)的字符串。

每個(gè)節(jié)點(diǎn)的所有子節(jié)點(diǎn)包含的字符都不相同。

如下是一棵典型的Trie樹:

Trie的來源是Retrieval,它常用于前綴匹配和詞頻統(tǒng)計(jì)。可能有人要說了,詞頻統(tǒng)計(jì)簡(jiǎn)單啊,一個(gè)hash或者一個(gè)堆就可以搞定,但問題來了,如果內(nèi)存有限呢?還能這么 玩嗎?所以這里我們就可以用trie樹來壓縮下空間,因?yàn)楣睬熬Y都是用一個(gè)節(jié)點(diǎn)保存的。

1、定義

這里為了簡(jiǎn)化,只考慮了26個(gè)小寫字母。

首先是節(jié)點(diǎn)的定義:

public class TrieNode {

 public TrieNode[] children;

 public char data;

 public int freq;

 public TrieNode() {
 //因?yàn)橛?6個(gè)字母
 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)該放在哪個(gè)孩子中。

 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)

移除操作中,需要對(duì)詞頻進(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、測(cè)試

測(cè)試代碼如下:

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


 }

測(cè)試結(jié)果如下:

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論