Print bst given range

why it give wrong answer??
import java.util.*;
class Tree {

public static class Node
{
	Node left;
	Node right;
	int data;
	public Node(int d)
	   {
		data=d;
	   }
}
private Node root;
private int size;

public Tree(int []arr)
{
this.root=bst(arr,0,arr.length-1);
}

public static Node bst(int []arr,int l,int h)
   {
       if(l>h)
	return null;

	int mid=(l+h)/2;
    
	Node n=new Node(arr[mid]);

	n.left=bst(arr,l,mid-1);
	n.right=bst(arr,mid+1,h);

      return n;
   }

public  void print(int k1,int k2)
    {
		 this.printrange(this.root,k1,k2);
	}

public void display()
   {
	   this.preorder(this.root);
   }	

public static void preorder(Node node )
{
      if(node==null)
         return;

		 System.out.print(node.data+" ");
		 preorder(node.left);
		 preorder(node.right);

}
public static void printrange(Node node ,int k1,int k2)
    {
	    if(node==null)
		  return;

           printrange(node.left,k1,k2);
		 if(node.data>=k1 && node.data<=k2)
		  System.out.print(node.data+" ");

		  printrange(node.right,k1,k2); 	
	}

}

public class main {
public static void main(String args[]) {
Scanner kb=new Scanner(System.in);

	int t=kb.nextInt();

	while(t-->0)
       {  
		 int n=kb.nextInt();
         int []arr=new int[n];

		 for(int i=0;i<n;i++)
		  arr[i]=kb.nextInt();
          
		  Arrays.sort(arr);

          Tree tr=new Tree(arr);
		  
		  int k1=kb.nextInt();
		  int k2=kb.nextInt();
          
		  tr.display();
		  System.out.println("");
		  tr.print(k1,k2);

		   System.out.println("");
	   }


}

}

get the Node class out of the Tree class, instead of making it an inner class.
Thanks.

but till now we make node class as inner class in tree so why can’t we make as inner classi n this question??

yes, you can make Node as static inner class. that not the concern here, sorry for that.
the problem in your approach is:
you are sorting the array to form the bst. order of the input elements decides the structure of the tree. for eg. 4 2 3 1 and 1 2 3 4 will not end up to structurally same bst. so array sorting must not be done before tree creation.

thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

@abhishekg,

You need not sort the array before construction. Also, you need not pass the entire array at one go, do it element by element. I have commented the mistakes in your code and corrected them.

Also, follow the output format as given in the question.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.