在BinarySearchTree中插入方法

编程入门 行业动态 更新时间:2024-10-27 16:38:15
本文介绍了在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中插入方法

本文发布于:2023-10-25 03:12:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   BinarySearchTree

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!