searching and deletion in binary search tree in java

 searching and deletion in binary search tree java hindi

public class binarysearch {
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}
public Node addtress(Node root,int values){
if (root==null){
return new Node(values);
}
if (values< root.data){
root.left=addtress(root.left,values);
}
else{
root.right=addtress(root.right,values);
}
return root;
}
public void inorder(Node root){
if (root==null) return;
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
public Node search(Node root,int value){
if (root==null) return null;
if (root.data== value){
return root;
}
if (root.data > value){
return search(root.left,value);
}
else{
return search(root.right,value);
}
}
public Node delete(Node root,int delval){
if (root==null) return null;
if (delval< root.data){
root.left=delete(root.left,delval);
}
else if (delval > root.data){
root.right=delete(root.right,delval);
}
else{
if (root.left !=null && root.right !=null){
int findmax=max(root.left);
root.data=findmax;
root.left=delete(root.left,findmax);
}
else if (root.left!=null){
return root.left;
}
else if (root.right!=null){
return root.right;
}
else {
return null;
}

}
return root;
}
public static int max(Node node){
if (node.right !=null){
return max(node.right);
}
else {
return node.data;
}
}
public static void main(String[] args) {
binarysearch bss=new binarysearch();
Node root=null;
root=bss.addtress(root,10);
root=bss.addtress(root,200);
root=bss.addtress(root,30);
root=bss.addtress(root,40);
root=bss.addtress(root,50);
root=bss.addtress(root,80);
root=bss.addtress(root,90);
bss.inorder(root);
// if(bss.search(root,155)==null){
// System.out.println("data not found");
// }
// else{
// System.out.println(" data found");
// }
System.out.println("********");
bss.delete(root,40);
bss.inorder(root);
//
}
}

Comments

Popular posts from this blog

Back / Flip Cover Universal List

Super-D, 11D, Super X tempered glass Universal list