الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

استفسار بخصوص deletion by merging of binary tree

بدأه طموح لاينثني في 25 ديسمبر 2011 · 3 رد · 479 مشاهدة · في الرياضيات والخوارزميات
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

أنا عندما قمت بتطبيق برنامج 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();
		}
	}

أتمنى أن تعرفوا ماهو السبب مشكورررين مقدماُ :)

تم تعديل هذه المشاركة بواسطة طموح لاينثني في 25 ديسمبر 2011 في 18:51

#2

ما هى المشكلة عند حذف ال root؟ الكود شكله طبيعى ماعدا

else if(root!=null)

أعتقد  أنك تقصد

else if(p==null)
#3

أنا عندما أطلب حذف الroot وليكن 50 لا ينحذف بل يبقى كما هو وتتكرر الاربعة الاعداد الاخيرة

ياليت تجربة وتشوف الاوت بوت لكي تفهم قصدي جيداً

وشكرا لك

#4

طيب أرجو منك وضع ملفات الجافا حتى أنفذها مباشرة فى المترجم ﻷن الكود الموضوع يعطى لى أخطاء عند التنفيذ بأنه ﻻ يجد ال Main class و أنا ﻻ أتذكر على وجه التحديد طريقة تسمية كلاسات الجافا. أنا أستخدم ال Netbeans.

مواضيع مشابهة