程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 數據結構與算法(C#實現)系列---N叉樹(二)

數據結構與算法(C#實現)系列---N叉樹(二)

編輯:C#入門知識

數據結構與算法(C#實現)系列---N叉樹(二)

Heavenkiller(原創)

public override uint Degree

         {

              get

              {

                   return this.degree;

              }

         }
                     

         //只用於空樹結點

         public virtual void AttachKey(object _obj)

         {

              if(!IsEmpty())

                   throw new Exception("My:this node must be a empty tree node!");

              this.key=_obj;

              this.treeList=new ArrayList();//產生一個degree長的數組,並將其初始化為空樹

              this.treeList.Capacity=(int)this.degree;

 

              for(int i=0;i<this.degree;i++)

              {

                   treeList.Add(new NaryTree(this.degree));

              }

              /*

              foreach(object tmpObj in this.treeList)

              {

                   tmpObj=new NaryTree(this.degree);

              }

              */

         }

         //只用於葉子結點,將葉子結點變為一個空結點,並返回葉子結點關鍵字的引用

         public virtual object DetachKey()

         {

              if(!IsLeaf())

                   throw new Exception("My:this node must be a leaf node!");

              object result=this.key;//store this leaf node temporary

              this.key=null;

              this.treeList=null;

 

              return result;

         }

         //將子樹連接到指定樹的第num個結點上,前提是這個結點必須是空結點,並且度數相同,否則拋出異常

         public virtual void AttachSubtree(uint num,NaryTree _naryTree)

         {

              if(this.IsEmpty())

                   throw new Exception("My:it cant be a empty tree!");

              if(!(this[num-1].IsEmpty()) | this.degree!=_naryTree.degree )

                   throw new Exception("My:this[i-1] must be empty and they should have the same degree!");

              this[num-1]=_naryTree;

         }

         //僅為非空樹定義,從給定樹中刪去它的第i棵子樹並連上一個空樹,度數相同,並且返回刪除的子樹引用

         public virtual NaryTree DetachSubtree(uint num)

         {

              if (IsEmpty())

                   throw new Exception("My:it cant be empty! ");

              NaryTree tmpTree=this;

              ((NaryTree)this[num-1]).key=null;

              ((NaryTree)this[num-1]).treeList=null;

 

              return this;

         }

     }

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved