二叉树 排序
先看一下程序运行结果
using System; using System.Collections.Generic; using System.Linq; using System.Text; using BinaryTree; namespace BinaryTreeTest { class Program { static void Main(string[] args) { Tree<int> tree1 = new Tree<int>(10); tree1.Insert(5); tree1.Insert(11); tree1.Insert(5); tree1.Insert(-12); tree1.Insert(15); tree1.Insert(0); tree1.Insert(14); tree1.Insert(-8); tree1.Insert(10); tree1.Insert(8); tree1.Insert(8); tree1.WalkTree(); //Tree<string> tree2 = new Tree<string>("Hello"); //tree2.Insert("World"); //tree2.Insert("How"); //tree2.Insert("Are"); //tree2.Insert("You"); //tree2.Insert("Today"); //tree2.Insert("I"); //tree2.Insert("Hope"); //tree2.Insert("You"); //tree2.Insert("Are"); //tree2.Insert("Feeling"); //tree2.Insert("Well"); //tree2.Insert("!"); //tree2.WalkTree(); Console.Read(); } } }


public void WalkTree() { if (this.LeftTree != null) //这里表示递归到最后一个左节点,最后一个左节点也是最小的一个数字, { this.LeftTree.WalkTree(); } Console.WriteLine(this.NodeData.ToString());//打印这个数字 if (this.RightTree != null)//然后再看 最后一个左节 的右节点是否==null,!=null遍历最后一个左节的右节点 { this.RightTree.WalkTree(); } }
遍历结果, -12 , 0 , 5 , 5 , 10 ,11 ,15
全部代码
public class Tree<TItem> where TItem : IComparable<TItem> { public Tree(TItem nodeValue) { this.NodeData = nodeValue; this.LeftTree = null; this.RightTree = null; } public void Insert(TItem newItem) { TItem currentNodeValue = this.NodeData; //CompareTo //这个函数表示:将当前实例与同一类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例在排序顺序中的位置是位于另一个对象之前、之后还是与其位置相同。 //详细解释:http://msdn.microsoft.com/zh-cn/library/system.icomparable.compareto(v=vs.110).aspx if (currentNodeValue.CompareTo(newItem) > 0) { if (this.LeftTree == null) { this.LeftTree = new Tree<TItem>(newItem); } else { this.LeftTree.Insert(newItem); } } else { if (this.RightTree == null) { this.RightTree = new Tree<TItem>(newItem); } else { this.RightTree.Insert(newItem); } } } public void WalkTree() { if (this.LeftTree != null) //这里表示递归到最后一个左节点,最后一个左节点也是最小的一个数字, { this.LeftTree.WalkTree(); } Console.WriteLine(this.NodeData.ToString());//打印这个数字 if (this.RightTree != null)//然后再看 最后一个左节 的右节点是否==null,!=null遍历最后一个左节的右节点 { this.RightTree.WalkTree(); } } public TItem NodeData { get; set; } public Tree<TItem> LeftTree { get; set; } public Tree<TItem> RightTree { get; set; } }
开始代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using BinaryTree; namespace BinaryTreeTest { class Program { static void Main(string[] args) { Tree<int> tree1 = new Tree<int>(10); tree1.Insert(5); tree1.Insert(11); tree1.Insert(5); tree1.Insert(-12); tree1.Insert(15); tree1.Insert(0); tree1.Insert(14); tree1.Insert(-8); tree1.Insert(10); tree1.Insert(8); tree1.Insert(8); tree1.WalkTree(); Console.WriteLine("..................................."); Tree<string> tree2 = new Tree<string>("Hello"); tree2.Insert("World"); tree2.Insert("How"); tree2.Insert("Are"); tree2.Insert("You"); tree2.Insert("Today"); tree2.Insert("I"); tree2.Insert("Hope"); tree2.Insert("You"); tree2.Insert("Are"); tree2.Insert("Feeling"); tree2.Insert("Well"); tree2.Insert("!"); tree2.WalkTree(); Console.Read(); } } }
运行结果:


浙公网安备 33010602011771号