the tree is generted by my code is correct . then why it showing a wrong answer.
I think tree genreted by my code is correct
you do not have to sort the array and construct the tree…you have to add each element of the array one by one in the bst…
also print new line after each test case…
import java.util.*;
public class BSTtest {
static Scanner se=new Scanner(System.in);
private class node{
private int data;
private node left;
private node right;
node(int data){
this.data=data;
}
}
private node root;
public BSTtest(int[] arr) {
for(int i=0;i<arr.length;i++)
this.root=constructTree(arr[i],root);
}
public node constructTree(int data,node node){
if(node==null){
node nn=new node(data);
return nn;
}
if(data<node.data){
node.left=constructTree(data,node.left);
}
if(data>node.data){
node.right=constructTree(data,node.right);
}
return node;
}
// private node construct(int[] arr,int lo,int hi) {
// if(lo>hi) {
// return null;
// }
// int mid=(lo+hi)/2;
// node node=new node();
// node.data=arr[mid];
// node.left=construct(arr,lo,mid-1);
// node.right=construct(arr, mid+1, hi);
// return node;
// }
public void display() {
System.out.println("--------------");
this.display(this.root);
System.out.println("--------------");
}
private void display(node node) {
if(node==null) {
return ;
}
String str="";
if(node.left==null) {
str+=".";
}else {
str+=node.left.data;
}
str+="=>"+node.data+"<=";
if(node.right==null) {
str+=".";
}else {
str+=node.right.data;
}
System.out.println(str);
display(node.left);
display(node.right);
}
public boolean find(int item) {
return this.find(this.root,item);
}
private boolean find (node node,int item) {
if(node==null) {
return false;
}
if(item<node.data) {
return find(node.left,item);
}else if(item>node.data) {
return find(node.right,item);
}else {
return true;
}
}
public int max() {
return this.max(this.root);
}
private int max(node node) {
if(node.right==null) {
return node.data;
}
return max(node.right);
}
// public void add(int item) {
// this.add(this.root, item);
// }
// private void add(node root,int item) {
// if(root.data<item) {
// if(root.right==null) {
// node node =new node();
// node.data=item;
// root.right=node;
// }else {
// add(root.right,item);
// }
// }else {
// if(root.data>item){
// if(root.left==null) {
// node node =new node();
// node.data=item;
// root.right=node;
// }else {
// add(root.left,item);
// }
// }
// }
// }
public void remove(int item) {
this.remove(this.root,null,false,item);
}
private void remove(node node,node parent,boolean ilc, int item) {
if(item>node.data) {
remove(node.right,node,false,item);
}else if(item<node.data) {
remove(node.left,node,true,item);
}else {
if(node.left==null&&node.right==null) {
if(ilc) {
parent.left=null;
}else {
parent.right=null;
}
}else if(node.left==null&&node.right!=null) {
if(ilc) {
parent.left=node.right;
}else {
parent.right=node.right;
}
}
else if(node.left!=null&&node.right==null) {
if(ilc) {
parent.left=node.left;
}else {
parent.right=node.left;
}
}else {
int max=max(node.left);
node.data=max;
remove(node.left,node,true,max);
}
}
}
public void preorder() {
this.preorder(this.root);
// System.out.println("END");
}
private void preorder(node node) {
if(node==null) {
return;
}
System.out.print(node.data+" ");
preorder(node.left);
preorder(node.right);
}
public void print(int a,int b){
this.Print(this.root,a,b);
}
private void Print(node node, int k1, int k2) {
if (node == null) {
return;
}
if (k1 < node.data) {
Print(node.left, k1, k2);
}
if (k1 <= node.data && k2 >= node.data) {
System.out.print(node.data + " ");
}
if (k2 > node.data) {
Print(node.right, k1, k2);
}
}
// public static void insertion(int[] arr) {
// for(int counter=1;counter<=arr.length-1;counter++) {
// int val=arr[counter];
// int j=counter-1;
// while(j>=0&&arr[j]>val) {
// arr[j+1]=arr[j];
// j--;}
// arr[j+1]=val;
// }
// }
public static void main(String[] args) {
int test=se.nextInt();
for(int t=0;t<test;t++){//test case
int tree=se.nextInt();
int[] arr=new int[tree];//input to make tree
for(int i=0;i<tree;i++){
arr[i]=se.nextInt();
}
// insertion(arr);
// Arrays.sort(arr);
BSTtest trees=new BSTtest(arr);
int a=se.nextInt();
int b=se.nextInt();
System.out.print("# Preorder : ");
trees.preorder();
System.out.println();
System.out.print("# Nodes within range are : ");
trees.print(a,b);
System.out.println();
}
}
}
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.