简体   繁体   English

无法获得插入方法以在Trie数据结构中工作

[英]can't get insert method to work in a trie data structure

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. 这里的“ hijo”是当前节点的下一个节点,“ agregarHermano()”是一种在与调用该方法的元素相同级别上添加元素的方法。

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: 这是构造函数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(): 这是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. 在trie数据结构中,您有一个孩子列表,而不是一个孩子列表。 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 . 我不确定您的hijo.addhermano会做什么。 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. 要检索元素列表或检查词典中是否有单词,请遍历子级(和示例中的兄弟),并检查(1)是否存在下一个字符,以及(2)标志是否设置为true 。 This will construct a wrod 这会造成混乱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM