www.pudn.com > MyDM1.rar > DecisionTree.cs


using System; 
using System.Data; 
using System.Data.SqlClient; 
namespace MyDM1 
{ 
	///  
	/// Tree 的摘要说明。 
	///  
	public class DecisionTree 
	{ 
		private TreeNode Root; 
		public TreeNode Curr_Node; 
		private string result; 
		public DecisionTree(){Root = null;result=null;Curr_Node=null;} 
		 
		 
		public virtual void Clear(){Root = null;Curr_Node=null;} 
		 
		public void MakeRoot(string root_name) 
		{ 
			MyDM1.TreeNode tn=new TreeNode(); 
			tn.Name=root_name; 
			tn.SetParent(null); 
			tn.Setparentvalue(null); 
			tn.SetLeftChild(null); 
			tn.SetRightSibling(null); 
			tn.SetChildCount(0); 
			this.Root=tn; 
			this.Curr_Node=this.Root; 
		} 
		 
		 
        //将当前节点移至该节点的最左兄弟节点处 
		public void MoveToLeft() 
		{ 
			if(!this.Curr_Node.IsLeftNode()) 
			{ 
				this.MoveToParent(); 
				this.MoveToLeftChild(); 
			} 
		} 
 
         
		 
		//将当前节点移至该节点的最右兄弟节点处 
		public void MoveToRight() 
		{ 
			while(!this.Curr_Node.IsRightNode()) 
			{this.MoveToRightSibling();} 
		} 
 
 
		//将当前节点移至该节点的左子节点处 
		public void MoveToLeftChild() 
		{ 
			if(this.Curr_Node.Child_Count!=0) 
			{this.Curr_Node=this.Curr_Node.Left_Child;} 
		} 
		 
		 
		//将当前节点移至该节点的右兄弟节点处 
		public void MoveToRightSibling() 
		{ 
			if(this.Curr_Node.GetRightSibling()!=null) 
			{this.Curr_Node=this.Curr_Node.Right_Sibling;} 
		 
		} 
		 
		 
		//将当前节点移至该节点的父节点处 
		public void MoveToParent() 
		{ 
			if(this.Curr_Node.GetParent()!=null) 
			{this.Curr_Node=this.Curr_Node.Parent;} 
		} 
 
		 
		 
		//将当前节点移至根节点处 
		public void MoveToRoot() 
		{this.Curr_Node=this.Root;} 
 
		 
		//返回当前节点 
		public TreeNode GetCurr() 
		{return this.Curr_Node;} 
 
 
		//设置当前节点位置 
		public void SetCurr(TreeNode node) 
		{this.Curr_Node=node;} 
 
 
		public TreeNode GetNode(string name,TreeNode tn)//根据节点名称,返回该节点 
		{ 
			if(tn == null)	return null; 			if(tn.Name.Trim() == name.Trim())	return tn ; 			if(tn.Child_Count!=0) 			{ 				for(TreeNode temp = tn.Left_Child; temp != null; temp = temp.Right_Sibling) 				{ 					TreeNode node = GetNode(name,temp);                      if(node!= null){return node;}  				} 			} 			else {return null;} 
			return null;	  
		} 
		 
		 
		 
		//插入节点,依次右插,如果父节点没有子节点,将该节点置为父节点的最左子节点,若父节点 
		//有子节点,将该节点置为父节点的最右子节点的右边. 
		public void Insert(TreeNode parent,TreeNode node,string parent_value) 
		{ 
			if(parent==null ){this.MakeRoot(node.Name);return;} 
		 	if(parent.Child_Count==0)  //该父节点没有子节点 
			{parent.Left_Child=node;} 
			else        //该父节点有子节点 
			{ 
                this.SetCurr(parent);	 
				this.MoveToLeftChild(); 
				this.MoveToRight(); 
				this.Curr_Node.Right_Sibling=node; 
			} 
			node.SetParent(parent); 
			parent.Child_Count++; 
			node.Child_Count=0; 
			node.Left_Child=null; 
			node.Right_Sibling=null; 
			node.Parent_Value=parent_value; 
		} 
		 
		 
		 
		//判断该书是否为空,若是,返回true 
		public bool IsNull() 
		{ 
		    if(this.Root==null)return true; 
			return false; 
		} 
		 
		 
		 
		//显示树 
		public string Print(TreeNode rootnode) 
		{ 
			 
		    if(rootnode==null)return null; 
			result=result + rootnode.PrintNode(); 
            this.Print(rootnode.Left_Child); 
			this.Print(rootnode.Right_Sibling); 
			return result; 
		} 
			 
		 
		 
		public TreeNode GetRoot(){return this.Root;} 
		 
 
		 
		 
	} 
 
 
	public class TreeNode 
	{ 
		#region TreeNode属性 
 
		public string Name;            //该节点的属性名称 
		public string Parent_Value;    //该节点的父节点分支值 
		public TreeNode Parent;        //该节点的父节点 
		public int Child_Count;        //该节点的子节点数目 
        public TreeNode Left_Child;    //该节点的最左子节点 
	    public TreeNode Right_Sibling; //该节点的右兄弟节点 
		#endregion 
		 
		public TreeNode():this(null,null,null,null,null){} 
		public TreeNode(string name):this(name,null,null,null,null){} 
		public TreeNode(string name,string parent_value,TreeNode Parent,TreeNode Left_Child,TreeNode Right_Sibling) 
		{ 
			this.Child_Count=0; 
			this.Name = name; 
			this.Parent_Value=parent_value; 
            this.Left_Child=Left_Child; 
			this.Parent=Parent; 
			this.Right_Sibling=Right_Sibling; 
		} 
	 
		 
		public 	int GetChildCount(){return this.Child_Count;} 
		public 	void SetChildCount(int count){this.Child_Count=count;} 
 
		public 	string GetName(){return this.Name;} 
		public 	void SetName(string name){this.Name=name;} 
	 
		public 	string GetParentValue(){return this.Parent_Value;} 
		public 	void Setparentvalue(string parent_value){this.Parent_Value=parent_value;} 
	 
		public 	TreeNode GetParent(){return this.Parent;} 
		public 	void SetParent(TreeNode parent){this.Parent=parent;} 
	 
		public 	TreeNode GetLeftChild(){return this.Left_Child;} 
		public 	void SetLeftChild(TreeNode left_child){this.Left_Child=left_child;} 
		 
		public 	TreeNode GetRightSibling(){return this.Right_Sibling;} 
		public 	void SetRightSibling(TreeNode right_sibling){this.Right_Sibling=right_sibling;} 
	 
		public bool IsLeaf() 
		{ 
			if(this.Child_Count==0 && this.Parent!=null){return true;} 
			return false; 
		} 
	 
 
		public bool IsLeftNode() 
		{ 
			if(this.Parent.Left_Child==this)return true; 
			return false; 
		} 
		 
		 
		public bool IsRightNode() 
		{ 
			if(this.Right_Sibling==null)return true; 
			return false; 
		} 
 
		 
		 
		public bool IsRoot() 
		{ 
		    if(this.Parent!=null)return false; 
			return true; 
		} 
	 
		 
         //显示节点信息		 
		public string PrintNode() 
		{ 
 
			string temp; 
			 
			if(this.IsLeaf()) 
			{ 
				temp="\n"+"该节点名称: "	+ this.GetName() + "\n"  
					+ "该节点父节点名称: "	+ this.Parent.Name+"\n"	 
					+ "该节点父值: "+this.Parent_Value+"\n" ; 
			} 
			else 
			{ 
				if(!this.IsRoot()) 
				{ 
					if(this.IsRightNode()) 
					{ 
						temp="\n"+"该节点名称: "	+ this.GetName() + "\n"  
							+ "该节点父节点名称: "	+ this.Parent.Name+"\n"	 
						+ "该节点父值: " + this.Parent_Value+"\n"  
						+ "该节点左子节点名称: " + this.Left_Child.Name+"\n"  
							+ "该节点子节点数目: " + this.Child_Count.ToString()+"\n"; 
					 
			 
					} 
					else 
					{ 
					 
						temp="\n"+"该节点名称: "	+ this.GetName() + "\n"  
							+ "该节点父节点名称: "	+ this.Parent.Name+"\n"	 
						    + "该节点父值: " + this.Parent_Value+"\n"  
						    + "该节点左子节点名称: " + this.Left_Child.Name+"\n"  
							+ "该节点右兄弟节点名称: " + this.Right_Sibling.Name+"\n" 
							+ "该节点子节点数目: " + this.Child_Count.ToString()+"\n"; 
					} 
				} 
				else 
				{ 
					if(this.Child_Count==0) 
					{ 
						temp="\n"+"该节点名称: "	+ this.GetName() + "\n" ; 
					} 
					else 
					{ 
						temp="\n"+"该节点名称: "	+ this.GetName() + "\n"  
							+ "该节点左子节点名称: " + this.Left_Child.GetName()+"\n"  
							+ "该节点子节点数目: " + this.Child_Count.ToString()+"\n"; 
				 
					} 
					 
				} 
				 
				 
			} 
			return temp; 
		} 
	 
	 
		public void clear() 
		{ 
		    this.Child_Count=0; 
			this.Name=null; 
			this.Parent=null; 
			this.Left_Child=null; 
			this.Right_Sibling=null; 
			this.Parent_Value=null; 
		} 
	 
	 
	} 
}