在BinarySearchTree中插入方法 [英] Insert Method in a BinarySearchTree

查看:90
本文介绍了在BinarySearchTree中插入方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我写了某种二进制搜索树,它有一个插入方法. 因此,它将获得一个要插入的对象,一个字符数组和一个整数,以为其提供索引以供查看.

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.

这是插入方法:

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

我有一个方法,该方法将必需的参数(在这种情况下)传递:一个对象Buchstabe,然后将Char Array ['.','.']和整数0传递给insert方法.

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 ?

推荐答案

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并将其排序在列表中,以便您开头的字符串最短. 然后将它们插入Binaray树并显示.

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.

这篇关于在BinarySearchTree中插入方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆