[英]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.