Help for bst operation (BSTOPS)

What is wrong with this code? I hava tried a lot of case but not found

/* package codechef; // don’t place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int queries=sc.nextInt();
		Tree tree=new Tree();
		for(int i=0;i<queries;i++){
		    String operation=sc.next();
		    int data=sc.nextInt();
		    //System.out.println(operation+"   "+data);
		    if(operation.equals("i")){
		        tree.insert(data);
		    }else{
		        tree.delete(data);
		    }
		}
	}
}

class Tree{
    TreeNode Root=null;
    public Tree(){
        
    }
    public void insert(int val){
        if(Root==null){
            Root=new TreeNode(val);
            System.out.println(1);
            return;
        }
        insert(Root,val,1);
    }
    
    private void insert(TreeNode root,int val,int pos)
    {
        if(val<root.val){
            if(root.left==null){
                TreeNode newnode=new TreeNode(val);
                root.left=newnode;
                System.out.println(2*pos);
            }else{
                insert(root.left,val,2*pos);
            }
        }else{
            if(root.right==null){
                TreeNode newnode=new TreeNode(val);
                root.right=newnode;
                System.out.println(2*pos+1);
            }else{
                insert(root.right,val,2*pos+1);
            }
        }
    }
    
    public void delete(int data){
        delete(null,Root,data,1,1); //1:left  2:right
        //System.out.println("after "+Root.val);
    }
    private void delete(TreeNode parent,TreeNode root,int data,int pos,int side){
        if(root==null){
            return;
        }
        
        if(root.val==data)
        {
                //3 case:
                System.out.println(pos);
                if(root.left==null&&root.right==null){
                    if(parent==null){
                        Root=null;
                        return;
                    }
                    if(side==1){
                        parent.left=null;
                    }else{
                        parent.right=null;
                    }
                    return;
                }
                
                if(root.left==null&&root.right!=null){
                    if(parent==null){
                        Root=root.right;
                        return;
                    }
                    if(side==1){
                        parent.left=root.right;
                    }else{
                        parent.right=root.right;
                    }
                    return;
                }
                if(root.right==null&&root.left!=null){
                    if(parent==null){
                        Root=Root.left;
                        return;
                    }
                    if(side==1){
                        parent.left=root.left;
                    }else{
                        parent.right=root.left;
                    }
                    return;
                }
                //2children
                if(root.right!=null&&root.left!=null)
                {
                    TreeNode node=find(root,root.right,2);    
                    root.val=node.val;
                    return;
                    
                }
        }
        
        if(data<root.val){
            delete(root,root.left,data,2*pos,1);
            return;
        }
         if(data>root.val){
            delete(root,root.right,data,2*pos+1,2);
        }
        
    }
    
    private TreeNode find(TreeNode parent,TreeNode root,int side){
        if(root.right==null&&root.left==null){
            if(side==1){
                parent.left=null;
            }else{
                parent.right=null;
            }
            return root;
        }
        if(root.right!=null&&root.left==null){
            if(side==1){
                parent.left=root.right;
            }else{
                parent.right=root.right;
            }
            return root;
        }
        
        return find(root,root.left,1);
        
    }
    
}

class TreeNode{
    TreeNode left;
    TreeNode right;
    int val;
    public TreeNode(int val){
        this.val=val;
    }
}

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - what Problem are you trying to solve? BSTOPS Problem - CodeChef?

really appreciate since i am new to here. Yes, the link you show is the problem i tried to solve

1 Like