簡體   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;
    }
}

這就是我的插入方法。 問題在於它僅將我傳遞給它的最后一個Buchstabe添加到BinaryTree。 這樣它將得到一個Buchstabe,一個帶有一些“。”的字符數組。 或其中的“-”代碼,以及在開始時插入插入式方法時從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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM