java - AVL Tree traversals in JTextArea -
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); } } /** inorder method public interface private inorder method. calls private inorder method , passes root of 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); // loop adds numbers avl tree (int = 0; < 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(); (int = 0; < size; i++) { sb.append(string.format(" %2d", size)); // formats right justified if ((i + 1) % 10 == 0) { sb.append(system.lineseparator()); // adds new line after 10 values } } //inordertextarea.settext(sb.tostring(avltree.inordertraversal)); // display array in inorder inorder text field inordertextarea.settext(avltree.inordertraversal); } /** randnum method randomly selects numbers in given range. @param array array. @param num 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; (int : array) { if (i == last) { count++; continue; } system.out.println("number " + last + " found " + count + " times."); count = 1; last = i; }*/ for(int = 0; < 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(); } } i'm trying display output of avl tree inorder, preorder, , postorder traversals on multiple lines in jtextarea, preferably 10 numbers line. tried 'for' loop i've provided, i'm getting compile error. problem inordertextarea.settext(sb.tostring(avltree.inordertraversal));.
error:
avltree.java:527: error: no suitable method found tostring(string) inordertextarea.settext(sb.tostring(avltree.inordertraversal)); ^ method stringbuilder.tostring() not applicable (actual , formal argument lists differ in length) method abstractstringbuilder.tostring() not applicable (actual , formal argument lists differ in length) method object.tostring() not applicable (actual , formal argument lists differ in length) 1 error
how can re-write line of code make work? thank help.
Comments
Post a Comment