[英]can't get insert method to work in a trie data structure
我正在尝试为特里实现插入方法,因此预期结果将是我添加的元素存储在与关键字的最后一个字符相对应的节点中,而包含其他字符的节点将具有没有元素,但是我的方法实际上是将其存储在每个节点中,我不知道为什么。 这是我的实际代码:
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;
}
在trie数据结构中,您有一个孩子列表,而不是一个孩子列表。 在添加新节点之前,需要检查节点是否已经具有第一个字符。
我不确定您的hijo.addhermano会做什么。 发布该功能可能会有所帮助
此外,以下内容可能会更改
new NodoArbolTrie<T>(palabra.charAt(0),elemento);
至
new NodoArbolTrie<T>(palabra.charAt(0),elemento.substring(1,elemento.length());
这是你的问题。 要标记节点包含匹配的元素,您需要存储一个布尔值标记以说明这是否是元素的结尾
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 。 这会造成混乱
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.