Я изменил ваш код и добавил условие, что только если новое отображение не равно текущему дочернему элементу, тогда удаляется только текущий дочерний элемент и добавляется новое отображение.
Я также не был уверен, что вы были пытаясь напечатать в конце. Поэтому я добавил свои заявления для печати.
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
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());
// only if the new mapping is not equal.
if (!newMapping.equals(child.getKey())) {
this.children.remove(child.getKey());
this.children.put(newMapping, child.getValue());
}
//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();
System.out.println(" root " + tN.getValue());
System.out.println(" first level child 1 " + tN.getChildren().get(0).getValue());
System.out.println(" first leve child 2 " + tN.getChildren().get(1).getValue());
System.out.println(" second level child 1 " + tN.getChildren().get(1).getChildren().get(0).getValue());
System.out.println(" first level child 3 " + tN.getChildren().get(2).getValue());
/*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());
*/
}
}