C#实现二叉树外带中序遍历(转载)

using System; namespace BinaryTree {      // Binary Tree的结点类      class Node     {          public  int Data {  getset; }          public Node LeftSubNode {  getset; }          public Node RightSubNode {  getset; }          // 结点为自己追加子结点(与向左/向右追加结点,形成递归)          public  void Append(Node subNode)         {              if (subNode.Data <=  this.Data)             {                  this.AppendLeft(subNode);             }              else             {                  this.AppendRight(subNode);             }         }          // 向左追加          public  void AppendLeft(Node subNode)         {              if ( this.LeftSubNode ==  null)             {                  this.LeftSubNode = subNode;             }              else             {                  this.LeftSubNode.Append(subNode);             }         }          // 向右追加          public  void AppendRight(Node subNode)         {              if ( this.RightSubNode ==  null)             {                  this.RightSubNode = subNode;             }              else             {                  this.RightSubNode.Append(subNode);             }         }          // 结点显示自己的数据          public  void ShowData()         {             Console.WriteLine( "Data={0}"this.Data);         }     }      // BinaryTree类      class Tree     {          // 根结点          public Node Root {  getset; }          // 以根结点为起点,插入结点          public  void Insert(Node newNode)         {              if ( this.Root ==  null)             {                  this.Root = newNode;             }              else             {                  this.Root.Append(newNode);             }         }          // 重载,默认以根结点为起点遍历          public  void MidTravel()         {              this.MidTravel( this.Root);         }                   // 中序遍历(递归)          public  void MidTravel(Node node)         {              if (node.LeftSubNode !=  null)             {                  this.MidTravel(node.LeftSubNode);             }             node.ShowData();              if (node.RightSubNode !=  null)             {                  this.MidTravel(node.RightSubNode);             }         }     }      class Program     {          static  void Main( string[] args)         {             Tree tree =  new Tree();                          tree.Insert( new Node { Data = 3 });             tree.Insert( new Node { Data = 6 });             tree.Insert( new Node { Data = 2 });             tree.Insert( new Node { Data = 7 });             tree.Insert( new Node { Data = 18 });                          tree.MidTravel();         }     } }

转载于:https://www.cnblogs.com/guoxiaowen/archive/2008/08/07/1262571.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值