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?