C#实现二叉树,外带中序遍历

来源:互联网 发布:nginx是什么 编辑:程序博客网 时间:2024/05/16 11:56
  1. using System;
  2. namespace BinaryTree
  3. {
  4.     // Binary Tree的结点类
  5.     class Node
  6.     {
  7.         public int Data { getset; }
  8.         public Node LeftSubNode { getset; }
  9.         public Node RightSubNode { getset; }
  10.         // 结点为自己追加子结点(与向左/向右追加结合,形成递归)
  11.         public void Append(Node subNode)
  12.         {
  13.             if (subNode.Data <= this.Data)
  14.             {
  15.                 this.AppendLeft(subNode);
  16.             }
  17.             else
  18.             {
  19.                 this.AppendRight(subNode);
  20.             }
  21.         }
  22.         // 向左追加
  23.         public void AppendLeft(Node subNode)
  24.         {
  25.             if (this.LeftSubNode == null)
  26.             {
  27.                 this.LeftSubNode = subNode;
  28.             }
  29.             else
  30.             {
  31.                 this.LeftSubNode.Append(subNode);
  32.             }
  33.         }
  34.         // 向右追加
  35.         public void AppendRight(Node subNode)
  36.         {
  37.             if (this.RightSubNode == null)
  38.             {
  39.                 this.RightSubNode = subNode;
  40.             }
  41.             else
  42.             {
  43.                 this.RightSubNode.Append(subNode);
  44.             }
  45.         }
  46.         // 结点显示自己的数据
  47.         public void ShowData()
  48.         {
  49.             Console.WriteLine("Data={0}"this.Data);
  50.         }
  51.     }
  52.     // Binary Tree类
  53.     class Tree
  54.     {
  55.         // 根结点
  56.         public Node Root { getset; }
  57.         // 以某结点为起点,插入结点
  58.         public void Insert(Node newNode)
  59.         {
  60.             if (this.Root == null)
  61.             {
  62.                 this.Root = newNode;
  63.             }
  64.             else
  65.             {
  66.                 this.Root.Append(newNode);
  67.             }
  68.         }
  69.         // 重载,默认以根结点为起点插入
  70.         public void MidTravel()
  71.         {
  72.             this.MidTravel(this.Root);
  73.         }
  74.         
  75.         // 中序遍历(递归)
  76.         public void MidTravel(Node node)
  77.         {
  78.             if (node.LeftSubNode != null)
  79.             {
  80.                 this.MidTravel(node.LeftSubNode);
  81.             }
  82.             node.ShowData();
  83.             if (node.RightSubNode != null)
  84.             {
  85.                 this.MidTravel(node.RightSubNode);
  86.             }
  87.         }
  88.     }
  89.     class Program
  90.     {
  91.         static void Main(string[] args)
  92.         {
  93.             Tree tree = new Tree();
  94.             
  95.             tree.Insert(new Node { Data = 3 });
  96.             tree.Insert(new Node { Data = 6 });
  97.             tree.Insert(new Node { Data = 2 });
  98.             tree.Insert(new Node { Data = 7 });
  99.             tree.Insert(new Node { Data = 18 });
  100.             
  101.             tree.MidTravel();
  102.         }
  103.     }
  104. }