我正在尝试为trie实现insert方法,因此预期的结果将是我添加的元素存储在与关键字的最后一个字符对应的节点中,以及包含其他节点的节点中没有元素的字符,但我的方法实际上是将它存储在每个节点中,我无法弄清楚原因。这是我的实际代码:
public boolean agregar(String palabra, T elemento)throws Exception
{
boolean resp=false;
if(palabra.length()==1)
{
if(hijo!=null)
{
NodoArbolTrie<T> nodo= new NodoArbolTrie<T>(palabra.charAt(0),elemento);
resp= hijo.agregarHermano(nodo);
}
else
{
hijo= new NodoArbolTrie<T>(palabra.charAt(0),elemento);
hijo.cambiarPapa(this);
resp= true;
peso++;
}
}
else
{
NodoArbolTrie<T> nodo= new NodoArbolTrie<T>(palabra.charAt(0),null);
if(hijo!=null)
{
boolean resp2= hijo.agregarHermano(nodo);
if(resp2)
resp=nodo.agregar(palabra.substring(1), elemento);
}
else
{
hijo= nodo;
hijo.cambiarPapa(this);
peso++;
resp=nodo.agregar(palabra.substring(1), elemento);
}
}
return resp;
}
这里“hijo”是当前节点的下面节点,“agregarHermano()”是一个方法,它添加一个与调用方法相同级别的元素。
英文翻译:
public boolean add(String word, T element)throws Exception
{
boolean resp=false;
if(word.length()==1)
{
if(child!=null)
{
TreeNodeTrie<T> nodo= new TreeNodeTrie<T>(word.charAt(0),element);
resp= child.addBrother(nodo);
}
else
{
child= new TreeNodeTrie<T>(word.charAt(0),element);
child.changeParent(this);
resp= true;
weight++;
}
}
else
{
TreeNodeTrie<T> nodo= new TreeNodeTrie<T>(word.charAt(0),null);
if(child!=null)
{
boolean resp2= child.addBrother(nodo);
if(resp2)
resp=nodo.add(word.substring(1), element);
}
else
{
child= nodo;
child.changeParent(this);
peso++;
resp=nodo.add(word.substring(1), element);
}
}
return resp;
}
这是构造函数NodoArbolTrie的代码:
public NodoArbolTrie(char caracter,T elemento){
this.caracter=caracter;
this.elemento=elemento;
brother=null;
child=null;
parent=null;
if(elemento!=null)
weight++;
}
这是agregarHermano()的一个:
public boolean addBrother(NodoArbolTrie<T> nodo) throwsException
{
boolean resp=false;
if(nodo.getCharacter()>caracter)
{
if(brother!=null)
{
if(brother.getCharacter()>nodo.getCharacter())
{
nodo.setBrother(hermano);
nodo.setParent(parent);
setBrother(nodo);
resp= true;
weight++;
}
else{
resp= brother.setBrother(nodo);
}
}
else{
nodo.setParent(parent);
setBrother(nodo);
resp= true;
weight++;
}
}
else if(nodo.getCharacter()<caracter)
{
parent.setChild(nodo);
nodo.setBrother(this);
nodo.setParent(parent);
resp= true;
weight++;
}
else
{
if(nodo.getElement()!=null && elemento!=null)
{
throw new Exception("The word is already in the trie");
}
else if(nodo.getElement()!=null && elemento==null)
{
elemento=nodo.getElement();
weight++;
resp=true;
}
else
{
if(nodo.getChild()!=null)
{
if(child!=null)
resp=child.addBrother(nodo.getChild());
else
hijo=nodo.getChild();
}
else
resp=true;
}
}
return resp;
}
答案 0 :(得分:0)
在特里数据结构中,您有一个孩子的列表,而不是一个孩子。在添加新节点之前,需要检查节点是否已有第一个字符。
我不确定你的hijo.addhermano是做什么的。发布该功能可能会有所帮助
此外,下面应该可能会改变
new NodoArbolTrie<T>(palabra.charAt(0),elemento);
到
new NodoArbolTrie<T>(palabra.charAt(0),elemento.substring(1,elemento.length());
答案 1 :(得分:0)
这是你的问题。要标记节点包含匹配元素,您需要存储一个布尔标志来说明这是否是元素的结尾
public NodoArbolTrie(char caracter,T elemento){
this.caracter=caracter;
**this.elemento=elemento;**
这应该是
public NodoArbolTrie(char caracter,bollean isEndOfWord){
this.caracter=caracter;
**this.isEndOfWord=isEndOfWord;**
要检索元素列表或检查单词是否在字典中,您将遍历子项(以及示例中的兄弟)并检查(1)下一个字符是否存在以及(2)标志是否为设为true。这将构建一个wrod