السلام عليكم
أنا عندما قمت بتطبيق برنامج deletion of binary tree by merging تظهر مشكلة عند حذف root فقط ولكن عندما أحذف أي عنصر من أي مكان آخر غير root ينحذف بدون أي مشااكل
هذا هوه الكود أتمنى أن تعرفوا وش المشكله في حذف ال root
javax.swing.*;
class IntBSTNode6{
public IntBSTNode6 left,right,root;
public int key;
public IntBSTNode6(){
left=right=null;
}
public IntBSTNode6(int e1){
this(e1,null,null);
}
public IntBSTNode6(int e1,IntBSTNode6 lt,IntBSTNode6 rt){
key=e1;
left=lt;
right=rt;
}
public void visit(){
System.out.print(key+" ");
}
public void printAll(IntBSTNode6 p){
if(p!=null){
p.visit();
printAll(p.left);
printAll(p.right);
}
}
}
class IntBST{
public IntBSTNode6 root;
public IntBST(){
root=null;
}
public void visit(){
IntBSTNode6 p=root;
System.out.print(p.key+" ");
}
public void deletebymerging(IntBSTNode6 root,int e1){
IntBSTNode6 p=root,prev=null,node,temp;
while(p!=null&&p.key!=e1){
prev=p;
if(p.key<e1)
p=p.right;
else
p=p.left;
}
node=p;
if(p!=null&&p.key==e1){
if(node.right==null)
node=node.left;
else if(node.left==null)
node=node.right;
else{
temp=node.left;
while(temp.right!=null)
temp=temp.right;
temp.right=node.right;
node=node.left;
}
if(root==p)
root=node;
else if(prev.left==p)
prev.left=node;
else
prev.right=node;
}
else if(root!=null)
System.out.println("\nThe element"+e1+"isn't in tree");
else
System.out.println("The tree is empty");
}
public void printAll2(IntBSTNode6 p){
if(p!=null){
p.visit();
printAll2(p.left);
printAll2(p.right);
}
}
}
class MainClass{
public static void main(String[]args){
int option;
IntBST node=new IntBST();
IntBSTNode6 left2=new IntBSTNode6(20);
IntBSTNode6 left4=new IntBSTNode6(42);
IntBSTNode6 right4=new IntBSTNode6(85);
IntBSTNode6 right3=new IntBSTNode6(120);
IntBSTNode6 right2=new IntBSTNode6(45,left4,null);
IntBSTNode6 left3=new IntBSTNode6(80,null,right4);
IntBSTNode6 left1=new IntBSTNode6(40,left2,right2);
IntBSTNode6 right1=new IntBSTNode6(100,left3,right3);
IntBSTNode6 rot=new IntBSTNode6(60,left1,right1);
System.out.println("The tree before deletion:");
rot.printAll(rot);
String num=JOptionPane.showInputDialog("Enter the number:");
int n=Integer.parseInt(num);
node.deletebymerging(rot,n);
System.out.println("\n the tree after deletion :-->"+n+"\n");
node.printAll2(rot);
System.out.println();
}
}أتمنى أن تعرفوا ماهو السبب مشكورررين مقدماُ :)