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

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