This question already has answers here:
What causes “Can't find Symbol” and how to fix it?
                            
                                (5个答案)
                            
                    
                2年前关闭。
        

    

我似乎无法使我的程序实现一个二进制搜索树(使用用户输入)并搜索一个值,以打印出实际找到该值所需的迭代次数。
我创建了一个名为“ getLastIterationCount()”的方法,该方法返回迭代次数,但是当我想在主方法中将其打印出来时,在“ System.out.println(getLastIterationCount())”行出现错误。 '。我认为我的方法不在正确的位置,但是我不确定缺少什么。有什么想法可以使这个程序起作用吗?

     /* Class Node */
class Node


{
 Node left, right;
 int data;



 /* Constructor */
 public Node(int n)
 {
     left = null;
     right = null;
     data = n;
 }

/* Function to get data from node */
public int getData()
{
    return data;
}

 /* Function to get left node */
public Node getLeft()
{
    return left;
}

/* Function to get right node */
public Node getRight()
{
    return right;
  }
 }

 /* Class BST */
 class BST

{

 private Node root;
 private int iterations;
 /* Constructor */
 public BST()
 {
     root = null;
 }
 /* Functions to insert data */
 public void insert(int data)
 {
     root = insert(root, data);
 }
 /* Function to insert data recursively */
 private Node insert(Node node, int data)
 {
     if (node == null)
         node = new Node(data);
     else
     {
         if (data <= node.data)
             node.left = insert(node.left, data);
         else
             node.right = insert(node.right, data);
     }
     return node;
 }


  /* Functions to search for an element */
public boolean search(int val)
{
    iterations=0;
    iterations++;
    return search(root, val);
}



/* Function to search for an element recursively */
private boolean search(Node r, int val)
{
    iterations=0;
    boolean found = false;
    while ((r != null) && !found)
    {
        int rval = r.getData();
        if (val < rval){
            r = r.getLeft();
        }

        else if (val > rval){
            r = r.getRight();
        }

        else
        {
            found = true;
            break;
        }
        found = search(r, val);

    }

    return found;
}

   public int getLastIterationCount(){
   return iterations;
   }


  }
   /* Class LinkedListBST */
   public class LinkedListBST
  {


 public static void main(String[] args)

 {


     Scanner scan = new Scanner(System.in);
     /* Creating object of BST */
     BST bst = new BST();
     System.out.println("Linked List Binary Search Tree Test\n");
     char ch;
     /*  Accept input  */
     do
     {
         System.out.println("Enter integer element to insert");
         bst.insert( scan.nextInt() );



         System.out.println("\nDo you want to continue (Type y or n) \n");
         ch = scan.next().charAt(0);


     } while (ch == 'Y'|| ch == 'y');
     System.out.println("\nEnter an element to be searched: ");
    Scanner sc = new Scanner(System.in);
    System.out.println("Search result : " + bst.search(sc.nextInt()));

    System.out.println(getLastIterationCount()); //ISSUE IS HERE

    sc.close();

   }

  }

最佳答案

您在没有对象的情况下访问方法getLastIterationCount()。请使用bst.getLastIterationCount()进行调用

关于java - 如何输出在BST中查找值所需的迭代次数? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47784348/

10-08 21:59