java - 在二叉搜索树中插入方法

标签 java binary-search-tree

public void insert(Buchstabe pBuchstabe,char[] pChar,int pStelle)
{
    if(pBuchstabe==null)
        return;
    int Stelle = pStelle;
    if(baum.isEmpty())
    {
        baum=new BinaryTree(pBuchstabe);
    }
    else {
        if(pStelle < pChar.length)
        {
            if(pChar[Stelle] == '.')
            {
                Mybaum lTree=this.getLeftTree();
                Stelle++;
                lTree.insert(pBuchstabe,pChar,Stelle);
                this.baum.setLeftTree(lTree.baum);
            }
            else
            if(pChar[Stelle]=='-')
            {
                Mybaum rTree=this.getRightTree();
                Stelle++;
                rTree.insert(pBuchstabe,pChar,Stelle);
                this.baum.setLeftTree(rTree.baum);
            }
        }
        else
            return;
    }
}

这就是我的插入方法。问题是它只将我传递给它的最后一个 Buchstabe 添加到二叉树。所以它会得到一个 Buchstabe,一个带有一些“.”的字符数组。或者其中的“-”代码和一个在开始调用 insert merhod 时从 0 开始的整数。没有真正的错误,但我得到这个输出:http://puu.sh/h9I4E/beee4f30a9.png 。 它应该创建一棵包含 26 个项目的二叉树,但只有一个项目显示在错误的一侧。

最佳答案

我发现问题是插入的第二部分它使用左侧而不是右侧的树。

 if(pChar[Stelle]=='-')
        {
            Mybaum rTree=this.getRightTree();
            Stelle++;
            rTree.insert(pBuchstabe,pChar,Stelle);
            this.baum.**setLeftTree**(rTree.baum);
        }

关于java - 在二叉搜索树中插入方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29577599/

相关文章:

java - Android MapView 从 searchView 加载位置

c - 带字符串的 BST

java - BST size() 看起来很漂亮

java - 我需要一个数据结构来保存两种数据类型

java - 由 : java. lang.ClassNotFoundException : org. apache.zookeeper.KeeperException 引起

java - 使用 JAXB 将 XML 解码为 Iterable<Long>

java - list 中存在的项目模型 (json) 中缺少标识符

java - 将 Netbeans 项目逻辑 View 替换为大纲 View

c++ - 我的析构函数似乎没有命中树中的每个节点,因为我有内存泄漏,我在这里错过了什么?

python - Python 中树的中序遍历返回列表