雜七雜八——C#實現二叉樹,外帶中序遍歷

發現用C#語法實現數據結構的時候,代碼顯得乾淨利落,嘻嘻。
 
  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. }
  105. // 水之真諦
  106. // [url]http://blog.csdn.net/FantasiaX[/url]
相關文章
相關標籤/搜索