当前位置: 移动技术网 > IT编程>开发语言>Java > Trie树(字典树)的介绍及Java实现

Trie树(字典树)的介绍及Java实现

2019年07月22日  | 移动技术网IT编程  | 我要评论

简介

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


 }

测试结果如下:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网