samedi 25 avril 2015

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

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

I'm trying to display the output of an AVL Tree inorder, preorder, and postorder traversals on multiple lines in the JTextArea, preferably 10 numbers to a line. I tried the 'for' loop I've provided, but I'm getting a compile error. The problem is with inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal));.

Error:

AVLTree.java:527: error: no suitable method found for toString(String) inorderTextArea.setText(sb.toString(AVLTree.inorderTraversal)); ^ method StringBuilder.toString() is not applicable (actual and formal argument lists differ in length) method AbstractStringBuilder.toString() is not applicable (actual and formal argument lists differ in length) method Object.toString() is not applicable (actual and formal argument lists differ in length) 1 error

How can I re-write this line of code to make it work? Thank you for all your help.


Aucun commentaire:

Enregistrer un commentaire