賽迪網 >> 技術應用子站 >> ASP
數據結構與演算法(C#實現)系列---N叉樹(一)
作者: 來源:cfan 發佈時間:2006.07.27
【Java專區】 【網路安全】 【網管專區】 【linux專區】 【數據庫專區】 【進入論壇】 【IT博客】 
【Eclipse】  【PHP】  【DB2】  【Ajax】  【Struts】  【Spring】 

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



Heavenkiller(原創)



N叉樹的每一節點度數都相同,為N



using System;



using System.Collections;





namespace DataStructure



{



///



/// NaryTree 的摘要說明。-----N叉樹



///



public class NaryTree:Tree



{



// member variables



protected object key;



protected uint degree;



protected ArrayList treeList=new ArrayList();



//protected uint height=0;//暫時默認為0





//create an empty tree whose attribute of degree is _degree



public NaryTree(uint _degree)



{



//



// TODO: 在此處添加構造函數邏輯



//



this.key=null;



this.degree=_degree;



this.treeList=null;



}



//構造一棵葉子結點的N叉樹



public NaryTree(uint _degree,object _key)



{



this.key=_key;



this.degree=_degree;



this.treeList=new ArrayList();



this.treeList.Capacity=(int)_degree;





for(int i=0;i


{





this.treeList.Add( this.GetEmptyInstance(_degree) );



}



}



//-----------------------------------------------------------------



protected virtual object GetEmptyInstance(uint _degree)



{ return new NaryTree(_degree); }



//-------------------------------------------------------------------



//judge whether the tree is an empty tree



public override bool IsEmpty()



{ return this.key==null; }



//判定是否是葉子結點。如果即不是空樹且每一棵子樹均為空樹,則為葉子結點



public override bool IsLeaf()



{



if(IsEmpty())



return false;



for(uint i=0;i


{



if( !(this[i].IsEmpty()) )



return false;



}



return true;



}



//-----------------------------------Inherited Attributes---------------------------------



public override object Key



{



get



{



return this.key;



}



}



//索引器



public override Tree this[uint _index]



{



get



{





if( _index>=this.degree )



throw new Exception("My:out of index!");//如果出界,則拋出異常



if( this.IsEmpty() )



return null;//如果是空樹,則索引器返回一個 null



return (Tree)this.treeList[(int)_index];



}



set



{



this.treeList[(int)_index]=value;



}






最全面的臺式機導購資訊,形成全方位的臺式機導購平臺
評論】 【推薦】 【 】 【列印】 【關閉
*姓  名: 更多資料 了解方案 認識廠商
*單位名稱:
*聯繫電話:
*電子郵件:
    
◆ 相關文章
· 加密與解密 2003-02-22
· 動態功能表 2004-11-14
· 從MP3中提取歌曲資訊 2004-08-20
· net中交易處理的解決方案 2004-11-23
· 億眾國際點對點文件傳輸程式 2004-04-26