扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
using System;
using System.Collections;
namespace DataStructure
{
/// <summary>
/// BinaryTree 的摘要说明。
/// </summary>
public class BinaryTree:NaryTree
{
//构造二叉空树
public BinaryTree():base(2)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public BinaryTree(object _obj):base(2,_obj)
{}
//------------------------------------------------------------------
protected override object GetEmptyInstance(uint _degree)
{ return new BinaryTree(_degree); }
//------------------------------------------------------------------
//重写深度遍历
public override void DepthFirstTraversal(IPrePostVisitor _vis)
{
if ( !IsEmpty() )
{
_vis.PreVisit(this.Key);
this[0].DepthFirstTraversal(_vis);
_vis.Visit(this.Key);
this[1].DepthFirstTraversal(_vis);
_vis.PostVisit(this.Key);
}
}
//二叉树大小的比较
//先比较关键字,如果相等,再比较左子树,如果再相等,则比较右子树----如此递归
#region IComparable 成员
public override int CompareTo(object obj)
{
// TODO: 添加 BinaryTree.CompareTo 实现
//因为Comare()中已经进行了类型断定,故不会出现转型错误
BinaryTree tmpTree=(BinaryTree)obj;
if( this.IsEmpty() )
return tmpTree.IsEmpty()?0:-1;
if( tmpTree.IsEmpty() )
return 1;
int result=Comparer.Default.Compare(this,tmpTree);
if(result==0)
result=this[0].CompareTo(tmpTree[0]);
if(result==0)
result=this[1].CompareTo(tmpTree[1]);
return result;
}
#endregion
}
}
濠碘€冲€归悘澶愬箖閵娾晜濮滈悽顖涚摃閹烩晠宕氶崶鈺傜暠闁诡垰鍘栫花锛勬喆椤ゅ弧濡澘妫楅悡娆撳嫉閳ь剟寮0渚€鐛撻柛婵呮缁楀矂骞庨埀顒勫嫉椤栨瑤绻嗛柟顓у灲缁辨繈鏌囬敐鍕杽閻犱降鍨藉Σ鍕嚊閹跺鈧﹦绱旈幋鐐参楅柡鍫灦閸嬫牗绂掔捄铏规闁哄嫷鍨遍崑宥夋儍閸曨剚浠樺ù锝嗗▕閳ь剚鏌ㄧ欢鐐寸▕鐎b晝顏遍柕鍡嫹