using System; using System.Collections; namespace DataStructure { /// /// BinaryTree 的摘要说明。 /// 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 } } |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924557/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8781179/viewspace-924557/
本文详细介绍了二叉树数据结构的实现原理及其关键方法,包括构造函数、获取空实例、深度优先遍历及比较方法等。通过具体的代码示例展示了二叉树的基本操作。
170

被折叠的 条评论
为什么被折叠?



