Java JTextArea中的AVL树遍历 公共类AVLTree { 公共静态字符串inorderTraversal=“”; 专用静态无效索引(AVLNode btree) { if(btree!=null) { 顺序(b树左); inorderTraversal+=btree.value+“”; 顺序(b树右); } } /** 此inoder方法是的公共接口 private inoorder方法。它调用private 方法,并将其传递给树的根。 */ 公共静态无效序() { 顺序(根); } } 类AVLTreeDemo扩展了JFrame 实现ActionListener { 已执行的公共无效操作(操作事件evt) { 字符串cmdStr=cmdTextField.getText(); int size=Integer.parseInt(cmdStr); int[]数组=新的int[size]; //输入验证 //随机数法 randNum(数组,大小); //for循环将数字添加到AVL树 对于(int i=0;i
我试图在Java JTextArea中的AVL树遍历 公共类AVLTree { 公共静态字符串inorderTraversal=“”; 专用静态无效索引(AVLNode btree) { if(btree!=null) { 顺序(b树左); inorderTraversal+=btree.value+“”; 顺序(b树右); } } /** 此inoder方法是的公共接口 private inoorder方法。它调用private 方法,并将其传递给树的根。 */ 公共静态无效序() { 顺序(根); } } 类AVLTreeDemo扩展了JFrame 实现ActionListener { 已执行的公共无效操作(操作事件evt) { 字符串cmdStr=cmdTextField.getText(); int size=Integer.parseInt(cmdStr); int[]数组=新的int[size]; //输入验证 //随机数法 randNum(数组,大小); //for循环将数字添加到AVL树 对于(int i=0;i,java,tree-traversal,Java,Tree Traversal,我试图在JTextArea中的多行上显示AVL树的按序、前序和后序遍历的输出,最好每行显示10个数字。我尝试了我提供的“for”循环,但是我得到了一个编译错误。问题在于inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal)) 错误: java:527:错误:在OrderTextArea.setText(sb.toString(AVLTree.inorderTraversal))中找不到适用于toString(String)的方法
JTextArea
中的多行上显示AVL树的按序、前序和后序遍历的输出,最好每行显示10个数字。我尝试了我提供的“for”循环,但是我得到了一个编译错误。问题在于inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal))代码>
错误:
java:527:错误:在OrderTextArea.setText(sb.toString(AVLTree.inorderTraversal))中找不到适用于toString(String)的方法;^方法StringBuilder.toString()不适用(实际参数列表和形式参数列表长度不同)方法AbstractStringBuilder.toString()不适用(实际参数列表和形式参数列表长度不同)方法对象。toString()不适用(实际参数列表和形式参数列表长度不同)1错误
如何重新编写这行代码以使其正常工作?谢谢您的帮助。请同时发布错误。错误信息对我来说似乎很清楚。关于它,你还不明白什么?@meriton是的,错误信息很清楚,但我如何重新编写代码行以使其工作?什么是AVLTree.inorderTraversal?阵列?对象收集?@StanislavL我没有把所有的代码都放在这里,因为这里有600多行代码,这里的另一个程序员告诉我不要提交那么多代码。我将尝试只提交与问题相关的部分。非常感谢。
public class AVLTree
{
public static String inorderTraversal = " ";
private static void inorder(AVLNode btree)
{
if (btree != null)
{
inorder(btree.left);
inorderTraversal += btree.value + " ";
inorder(btree.right);
}
}
/**
This inorder method is the public interface to
the private inorder method. It calls the private
inorder method and passes it the root of the tree.
*/
public static void inorder()
{
inorder(root);
}
}
class AVLTreeDemo extends JFrame
implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String cmdStr = cmdTextField.getText();
int size = Integer.parseInt(cmdStr);
int[] array = new int[size];
// input validation
// Random number method
randNum(array, size);
// for loop adds numbers to AVL Tree
for (int i = 0; i < size; i++)
{
int value = array[i];
avlTree.add(value);
}
if (view != null)
remove(view);
view = avlTree.getView();
add(view);
pack();
validate();
cmdResultTextField.setText(" ");
// inorder method
AVLTree.inorder();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++)
{
sb.append(String.format(" %2d", size)); // Formats right justified
if ((i + 1) % 10 == 0)
{
sb.append(System.lineSeparator()); // Adds a new line after 10 values
}
}
//inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal));
// display the array in inorder to the inorder text field
inorderTextArea.setText(AVLTree.inorderTraversal);
}
/**
The randNum method randomly selects numbers
in a given range.
@param array The array.
@param num The integer numbers.
*/
public static void randNum(int[] array, int num)
{
Random rand = new Random();
// Selection sort method
selectionSort(array);
// display duplicates
/*int last = array[0];
int count = -1;
for (int i : array)
{
if (i == last)
{
count++;
continue;
}
System.out.println("Number " + last + " found " + count + " times.");
count = 1;
last = i;
}*/
for(int i = 0; i < num; i++)
{
// display duplicates
/*if(num == num - 1)
{
System.out.print("Duplicate: " + num);
}*/
array[i] = rand.nextInt(500);
}
}
public static void main(String [ ] args)
{
AVLTreeDemo atd = new AVLTreeDemo();
}
}