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;
}
In a trie data structure, you have a list of children, not a single child. Before you add a new node, you need to check if the node already has the first character.
I am not sure what your hijo.addhermano does . Posting that function might help
Also, the below should probably change
new NodoArbolTrie<T>(palabra.charAt(0),elemento);
to
new NodoArbolTrie<T>(palabra.charAt(0),elemento.substring(1,elemento.length());
This is your problem. To mark that a node contains a matching element, You need to store a boolean flag to say if this is end of an element
public NodoArbolTrie(char caracter,T elemento){
this.caracter=caracter;
**this.elemento=elemento;**
This should be
public NodoArbolTrie(char caracter,bollean isEndOfWord){
this.caracter=caracter;
**this.isEndOfWord=isEndOfWord;**
To retreive the list of elements or check if a word is in the dictionary, you iterate through the children(and brothers in your example) and check if (1) the next char is present and (2) if the flag is set to true. This will construct a wrod
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.