• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

java 搞代码 4年前 (2022-01-05) 22次浏览 已收录 0个评论

Trie树,又称字典树或前缀树,关于它的结构就不详细介绍了。Trie树在单词统计、前缀匹配等很多方面有很大用处。下面这篇文章主要介绍了Trie树,以及Java实现如何Trie树,有需要的朋友可以参考借鉴,下面来一起看看吧。

简介

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(k25){ 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(k25){ 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){ i<i style="color:transparent">来源gaodai$ma#com搞$$代**码)网</i>f(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(k25){ 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(); }

测试结果如下:

总结

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

以上就是Trie树(字典树)的介绍及Java实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Trie树(字典树)的介绍及Java实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址