Different answer for preoder part

private void RangePrint(Node node,int r1,int r2)
{
if(node==null)
return;
if(node.data>r1)
RangePrint(node.left, r1, r2);

    if(node.data>=r1 && node.data<=r2)
    {
        System.out.print(node.data+" ");
    }
    
    if(node.data<r2)
        RangePrint(node.right,r1, r2);
}
public void pre()
{
	System.out.print("# Preorder : ");
	this.pre(this.root);
	System.out.println("");
}
private void pre(Node node)
{
	if(node==null)
		return;
	System.out.print(node.data+ " ");
	pre(node.left);
	pre(node.right);	
}

// public void Range(int[] arr,int r1,int r2)
// {
// this.range_print(arr,this.root, r1, r2);
// }
// private void range_print(int[] arr,Node node,int r1,int r2)
// {
// int[] a=new int[arr.length];
// int idx=0;
// if(node==null)
// {
// return;
// }
// range_print(arr, node.left,r1,r2);
// a[idx++]=node.data;
// range_print(arr, node.right,r1,r2);
//
// Arrays.sort(a);
// int i=0;
// while(a[i]>=r1 && a[i]<=r2)
// {
// System.out.print(a[i]+" ");
// i++;
// }
// }

i am getting different answer for preoder part. and in the commented function range keys are unsorted. what to change there?

@Kapsime_S,
Your RangePrint method is correct in part of code not commented. You need to construst the bst without sorting the original elements. Also, for further doubts please share your complete code.

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.