I'm trying to implement the insert method for a trie, so the expected result would be for the element I add to be stored in the node which corresponds to the last character of the keyword, and for the nodes containing the other characters to have no element, but my method is actually storing it in every node and I can't figure out why. Here's my actual code:
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;
}
here "hijo" is the below node of the current node and "agregarHermano()" is a method that adds an element at the same level as the one that invokes the method.
English translation:
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;
}
here's the code for the constructor NodoArbolTrie:
public NodoArbolTrie(char caracter,T elemento){
this.caracter=caracter;
this.elemento=elemento;
brother=null;
child=null;
parent=null;
if(elemento!=null)
weight++;
}
and here's the one for 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;
}