public class TrieNode {
public TrieNode[] children;
public char data;
public int freq;
public TrieNode() {
//因为有26个字母
children = new TrieNode[26];
freq = 0;
}
}
public class TrieTree {
private TrieNode root;
public TrieTree(){
root=new TrieNode();
}
...
}
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);
}
}
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);
}
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);
}
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();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有