知识大全 数据结构与算法(C#)系列-二叉树

Posted

篇首语:树木在森林中相依偎而生长, 星辰在银河中因辉映而璀璨。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 数据结构与算法(C#)系列-二叉树相关的知识,希望对你有一定的参考价值。

数据结构与算法(C#)系列-二叉树  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  using System;

  using System Collections;

    

  namespace DataStructure

  

  /// <summary>

  /// BinaryTree 的摘要说明

  /// </summary>

  public class BinaryTree:NaryTree

  

  //构造二叉空树

  public BinaryTree():base( )

  

  //

  // TODO: 在此处添加构造函数逻辑

  //

  

  public BinaryTree(object _obj):base( _obj)

  

  //

  protected override object GetEmptyInstance(uint _degree)

      return new BinaryTree(_degree);

  //

  //重写深度遍历

  public override void DepthFirstTraversal(IPrePostVisitor _vis)

  

  if ( !IsEmpty() )

  

  _vis PreVisit(this Key);

  this[ ] DepthFirstTraversal(_vis);

  _vis Visit(this Key);

  this[ ] 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()? : ;

  if( tmpTree IsEmpty() )

  return ;

    

  int result=Comparer Default Compare(this tmpTree);

  if(result== )

  result=this[ ] CompareTo(tmpTree[ ]);

  if(result== )

  result=this[ ] CompareTo(tmpTree[ ]);

    

  

  return result;

  

    

  #endregion

  

cha138/Article/program/net/201311/12790

相关参考