简体   繁体   English

在二叉搜索树中插入方法

[英]Insert Method in a 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;
    }
}

So this is my insert Method. 这就是我的插入方法。 The Problem is that it only adds the last Buchstabe which i pass to it to the BinaryTree. 问题在于它仅将我传递给它的最后一个Buchstabe添加到BinaryTree。 So it will get a Buchstabe, a char Array with some '.' 这样它将得到一个Buchstabe,一个带有一些“。”的字符数组。 or '-' code in it and a integer which starts at 0 when the insert merhod is called at the beginning. 或其中的“-”代码,以及在开始时插入插入式方法时从0开始的整数。 There is no real error but i get this output : http://puu.sh/h9I4E/beee4f30a9.png . 没有真正的错误,但我得到以下输出: http : //puu.sh/h9I4E/beee4f30a9.png It should create a Binary Tree with 26 Items but only one is showing up on the wrong side. 它应该创建一个包含26个项目的二叉树,但是只有一个出现在错误的一侧。

I found the Problem it is the second Part of the insert it takes the left instead of the right tree. 我发现问题是插入的第二部分,它需要左侧而不是右侧的树。

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

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

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