【问题标题】:Insert Method in a BinarySearchTree在 BinarySearchTree 中插入方法
【发布时间】:2015-04-09 16:23:12
【问题描述】:

嘿,我写了某种二叉搜索树,它有一个插入方法。 所以它得到一个要插入的对象、一个字符数组和一个整数,它给它一个索引来查看。

所以这是插入方法:

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);
        }
    }
}

我有一个传递所需参数的方法(在这种情况下):一个 Object Buchstabe,然后是 Char Array['.','.'] 和整数 0 到 insert 方法。

我得到一个越界错误:

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)

有谁知道我做错了什么?

【问题讨论】:

    标签: java binary-search-tree


    【解决方案1】:

    看来你有问题

     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] == '.') -- 你得到 indexOutOfBound bc/ 你需要说 if(pChar[pStelle-1] == '.') .. 由于 Java 数组索引从 0 开始,如果长度为 5,则最后一个索引为 pChar[4]...

    此代码可能存在更多问题,因为我们没有完整的代码/上下文,我无法推测更多。但这是您可能会出现 indexoutofbound 的原因之一

    【讨论】:

    • 我不这么认为,因为我一开始就将 0 作为参数传递给方法。我将 'if(pStelle
    • 在你的代码中你能告诉我第 22 行是什么吗? java.lang.ArrayIndexOutOfBoundsException: 2 at Mybaum.insert(Mybaum.java:22)
    • 另外我会写一个小单元测试来找出究竟是哪种情况产生了错误。我还建议你将你的代码生成更容易理解
    • 第 22 行是 if 语句:if(pChar[pStelle] == '.')
    • 泛化是什么意思? :D
    【解决方案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);
    }
    

    这将创建对象 Buchstabe 并将其排序到 List 中,以便您在开始时拥有最短的字符串。 然后将它们插入到二叉树中并显示出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 2021-10-22
      • 1970-01-01
      • 2018-10-02
      相关资源
      最近更新 更多