【发布时间】:2020-07-16 18:10:48
【问题描述】:
尝试实现 Trie 数据结构添加元素,然后尝试将其减少为 trie 压缩机制。有一个特定的用例,由于它的实现永远作为一个无限循环运行,我们可以看看它,看看什么可能是最好的基本条件。
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class TrieNode {
private String value;
private ConcurrentHashMap<String, TrieNode> children;
private boolean isWord;
public TrieNode(String value){
this.value = value;
this.children = new ConcurrentHashMap<String, TrieNode>();
}
public static void sortbykey(ConcurrentHashMap<String, TrieNode> map) {
ArrayList<String> sortedKeys = new ArrayList<String>(map.keySet());
Collections.sort(sortedKeys);
}
public static ArrayList<TrieNode> hashMapValues (ConcurrentHashMap<String, TrieNode> map) {
ArrayList<TrieNode> nodeList = new ArrayList<TrieNode>(map.values());
return nodeList;
}
public ArrayList<TrieNode> getChildren() {
sortbykey(this.children);
return hashMapValues(this.children);
}
public ConcurrentHashMap<String, TrieNode> getChildrenMappings()
{
return this.children;
}
public void addChild(int numCharacters,String value)
{
//If we have arrived at the root level
if(value.length() == numCharacters)
{
this.isWord = true;
}
//If the value still has more characters then add a child
else
{
TrieNode nextNode = null;
String nextCharacter = Character.toString(value.charAt(numCharacters));
if(this.children.get(nextCharacter)==null)
{
String nextPrefix = value.substring(0,numCharacters+1);
nextNode = new TrieNode(nextPrefix);
this.children.put(nextCharacter, nextNode);
}
numCharacters++;
this.children.get(nextCharacter).addChild(numCharacters,value);
}
}
public void reduce(){
for(ConcurrentHashMap.Entry<String,TrieNode> child : this.children.entrySet()){
child.getValue().reduce();
//Get rid of original child mapping
this.children.remove(child.getKey());
//Replace with a new mapping which may include multiple characters if my child has been combined with other children
String newMapping = child.getValue().getValue().substring(this.getValue().length());
this.children.put(newMapping, child.getValue());
}
if(this.children.size() == 1){
for(ConcurrentHashMap.Entry<String,TrieNode> child : this.children.entrySet()){
//If I only have a single child, replace it with his children if I'm not also a word on my own
if(!this.isWord()){
//Get rid of the extra child node
this.children.remove(child.getKey());
//Take the child nodes value, word status and children
this.setValue(child.getValue().getValue());
this.setIsWord(child.getValue().isWord());
for(ConcurrentHashMap.Entry<String,TrieNode> grandchild : child.getValue().getChildrenMappings().entrySet()){
this.children.put(grandchild.getKey(),grandchild.getValue());
}
}
}
}
}
public boolean isWord()
{
return this.isWord;
}
public void setIsWord(boolean isWord){
this.isWord = isWord;
}
public String getValue()
{
return this.value;
}
public void setValue(String newValue){
this.value = newValue;
}
public static void main(String[] args){
TrieNode tN = new TrieNode("");
tN.addChild(0, "www.corriere.it/27esimaora/laquila");
tN.addChild(0, "www.corriere.it/27esimaora");
tN.addChild(0, "www.corriere.it/cronache/sesso-e-amore/");
tN.addChild(0, "www.corriere.it/foto-gallery/cronache/sesso-e-amore/");
tN.reduce();
ConcurrentHashMap<String, TrieNode> childrenMappings = tN.getChildren().get(0).getChildren().get(0).getChildren().get(0).getChildrenMappings();
for(Map.Entry<String, TrieNode> children : childrenMappings.entrySet()){
System.out.println(children.getKey() + " = " + children.getValue().getChildrenMappings() + children.getValue().getValue() + " "+ children.getValue().isWord);
}
System.out.println(tN.getChildren());
}
}
【问题讨论】:
-
代码太多,无法查看哥们。您能告诉我们什么操作/调用哪个方法导致无限循环吗?
-
感谢您查看@ArunGowda。后来实际解决了。是的,所需的实现非常复杂。
标签: java hashmap trie concurrenthashmap