Insert Method in a BinarySearchTree

社会主义新天地 提交于 2019-12-25 03:32:14

问题


Hey i have written some kind of Binary Search Tree, which has a insert method. So it gets a Object to insert, a Char Array and a Integer which gives it the Index to look at.

So this is the insert method :

public void insert(Buchstabe pBuchstabe,char[] pChar,int pStelle)
{
    if(pBuchstabe==null)
        return;

    if(baum.isEmpty())
    {
        baum=new BinaryTree(pBuchstabe);
    }
    else 
    if(pStelle <= pChar.length)
    {
        if(pChar[pStelle] == '.')
        {
            Mybaum lTree=this.getLeftTree();
            lTree.insert(pBuchstabe,pChar,pStelle+1);
            this.baum.setLeftTree(lTree.baum);
        }
        else
        if(pChar[pStelle]=='-')
        {
            Mybaum lTree=this.getRightTree();
            lTree.insert(pBuchstabe,pChar,pStelle+1);
            this.baum.setLeftTree(lTree.baum);
        }
    }
}

I have a Method which passes the required Parameters (in this case) : A Object Buchstabe,then the Char Array['.','.'] and the integer 0 to the insert method.

And i get a out of bounds error :

java.lang.ArrayIndexOutOfBoundsException: 2
at Mybaum.insert(Mybaum.java:22)
at Mybaum.insert(Mybaum.java:25)
at Mybaum.insert(Mybaum.java:25)
at Mörserbaum.einlesen(Mörserbaum.java:42)

Does anyone know what ive made wrong ?


回答1:


Looks like you have an issue

 if(baum.isEmpty())
    {
        baum=new BinaryTree(pBuchstabe);
    }
    else 
    **if(pStelle <= pChar.length)**
    {
        **if(pChar[pStelle] == '.')**
        {
            Mybaum lTree=this.getLeftTree();
            lTree.insert(pBuchstabe,pChar,pStelle+1);
            this.baum.setLeftTree(lTree.baum);
        }

if(pChar[pStelle] == '.') -- you get indexOutOfBound bc/ you need to say if(pChar[pStelle-1] == '.') .. since Java array index starts from 0, if the length is 5, last index would be pChar[4]...

there could be more issues with this code, since we don't have full code/context i can't speculate more.. but this is one of the reason you could get indexoutofbound




回答2:


public void einlesen()
{
    Buchstabeenschlange sch = new Buchstabeenschlange();
    for(int i = 0;i<codeTabelle.length;i++)
    {
        Buchstabe a = new Buchstabe(alphabet[i],codeTabelle[i]);
        if(a == null)
        {
            System.out.println("Buchstabe mit Error == "+a);
        }
        System.out.println("Buchstabe == "+a);
         sch.hinzufuegen(a);

        System.out.println("------------");

    }
    List l = sch.gibListe();
    sch.druckeListe();
    l.toFirst();
    while(l.hasAccess())
    {

      Buchstabe buch = (Buchstabe) l.getObject();  
      char[] code = buch.getCode().toCharArray();
      baum.insert(buch,code,0);
      l.next();
    }
    TreeViewGUI view = new TreeViewGUI(baum);
}

This creates the object Buchstabe and sorts it in a List so that you have the shortest Strings at the beginning. Then it inserts them into a Binaray Tree and displays it.



来源:https://stackoverflow.com/questions/29543903/insert-method-in-a-binarysearchtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!