0

I'm learning the BST implementation, this is my code for insertion and inorder function. I have a doubt regarding the inorder function

public class BSTtry {
    
    Node root;
    
    class Node{
        int data;
        Node left,right;
        Node(int data){
            this.data=data;
            left=right=null;
        }
    }
    
    public void insert(int data) {
        root=insertdata(root,data);
    }
    
    public void inorder(){
     printinorder(root);
    }
    public Node insertdata(Node root,int data) {
        if(root==null) {
            root=new Node(data);
            return root;
        }
        if(data<root.data) {
            root.left=insertdata(root.left,data);
        }
        if(data>root.data) {
            root.right=insertdata(root.right,data);
        }
        return root;
    }
    public void printinorder(Node root) {
        if(root!=null) {
            printinorder(root.left);
            System.out.print(root.data+" ");
            printinorder(root.right);
        }
    }
    
    public static void main(String[] args) {
     BSTtry bst=new BSTtry();
     //Inserted some values in the tree
     bst.printinorder(root);
    }

}

So when I try to use the bst.printinorder(root); an error is thrown Cannot make a static reference to the non-static field root.

So can I change the root to static or print the inorder by calling the inorder() function.Which is a better way??

rayne
  • 11
  • 1
  • Does this answer your question? [Cannot make a static reference to the non-static method](https://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method) – zubergu Jul 15 '21 at 17:41
  • The same question was asked and answered so many times before, that there's like 10 of duplicates already. Please, first search, then ready, then make a decision to ask or not. – zubergu Jul 15 '21 at 17:43

3 Answers3

0

Main methods are static, root is a field of BSTtry. Yes you could change the field to static but that's not what you want.

You should initialize the field e.g with a setter or with an constructor, having a Node argument.

Best regards

MaQ
  • 11
  • 4
0

Just use inorder() instead, which calls printinorder() with root as the first argument.

bst.inorder();
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

Remove Node class out of BSTtry Class, it will work

  class Node {
  int data;
  Node left, right;

  Node(int data) {
    this.data = data;
    left = right = null;
  }
}


public class BSTtry {

  Node root;

  public void insert(int data) {
    root = insertdata(root, data);
  }

  public void inorder() {
    printinorder(root);
  }

  public Node insertdata(Node root, int data) {
    if (root == null) {
      root = new Node(data);
      return root;
    }
    if (data < root.data) {
      root.left = insertdata(root.left, data);
    }
    if (data > root.data) {
      root.right = insertdata(root.right, data);
    }
    return root;
  }

  public void printinorder(Node root) {
    if (root != null) {
      printinorder(root.left);
      System.out.print(root.data + " ");
      printinorder(root.right);
    }
  }



  public static void main(String[] args) { 
    
    BSTtry bst=new BSTtry();
    //Inserted some values in the tree
    Node a = new Node(1);
    a.left = new Node(2);
    bst.printinorder(a);

  }
}
AshishC
  • 1
  • 3