0

I want to create binary search tree and to traverse the tree in-order. I have the following code:

public class BST {
    static Node root;
    public class Node{
        int data;
        Node left;
        Node right;

        Node(int data){
         this.data = data;
         left = null;
         right = null;
        }
        Node(int data, Node left, Node right){
         this.data = data;
         this.left = left;
         this.right = right;
        }
    }
    public void inOrderTraversal(Node root){
        if(root == null)
           return;
        inOrderTraversal(root.left);
        System.out.println(root.data);
        inOrderTraversal(root.right);
    }
    public static void main(String[] args) {
       Node n1 = new Node(1);
    }
 }

Howhever I cannot create node n1 with this code: Node n1 = new Node(1); I hace msg saying "No enclosing instance of type InOrder is accessible. Must qualify the allocation with an enclosing instance of type InOrder (e.g. x.new A() where x is an instance of BST)." Can someone explain me where is my mistake and how I have to create my nodes, respectively my BST?

sammy333
  • 1,384
  • 6
  • 21
  • 39

1 Answers1

0

Just make Node a static class:

public static class Node { ...
morgano
  • 17,210
  • 10
  • 45
  • 56