C#實現二叉樹遍曆
using System ; using System.Collections.Generic; using System .Text; namespace structure { class Program { class nodes<T> { T data; nodes<T> Lnode,rnode,pnode; public T Data { get {return data;} set{data =value;} } public nodes<T>LNode { get {return Lnode ;} set {Lnode =value;} } public nodes<T>RNode { get {return rnode ;} set {rnode =value;} } public nodes<T>PNode { get {return pnode ;} set {pnode =value;} } public nodes(){} public nodes(T data) { this.data =data ; } } //構造一棵已知的二叉樹 static nodes<string>BinTree() { nodes<string>[] binTree=new nodes<string>[8]; //創建節點 binTree [0]=new nodes<string> ("A"); binTree [1]=new nodes<string> ("B"); binTree [2]=new nodes<string> ("C"); binTree [3]=new nodes<string> ("D"); binTree [4]=new nodes<string> ("E"); binTree [5]=new nodes<string> ("F"); binTree [6]=new nodes<string> ("G"); binTree [7]=new nodes<string> ("H"); //使用層次遍曆二叉樹的思想,構造一個已知的二叉樹 binTree [0].LNode=binTree [1]; binTree [0].RNode=binTree [2]; binTree [1].RNode =binTree [3]; binTree [2].LNode=binTree [4]; binTree [2].RNode=binTree [5]; binTree [3].LNode =binTree [6]; binTree[3].RNode=binTree [7]; //返回二叉樹根節點 return binTree [0]; } //先序遍曆 static void PreOrder<T>(nodes<T> rootNode) { if(rootNode !=null ) { Console.WriteLine(rootNode.Data); PreOrder <T>(rootNode.LNode ); PreOrder <T>(rootNode.RNode); } } //中序遍曆二叉樹 static void MidOrder<T>(nodes<T> rootNode) { if (rootNode != null) { MidOrder<T>(rootNode.LNode); Console.WriteLine(rootNode.Data); MidOrder<T>(rootNode.RNode); } } //後序遍曆二叉樹 static void AfterOrder<T>(nodes<T> rootNode) { if (rootNode != null) { AfterOrder<T>(rootNode.LNode); AfterOrder<T>(rootNode.RNode); Console.WriteLine(rootNode.Data); } } //層次遍曆二叉樹 static void LayerOrder<T>(nodes<T> rootNode) { nodes<T>[] Nodes = new nodes<T>[20]; int front = -1; int rear = -1; if (rootNode != null) { rear++; Nodes[rear] = rootNode; } while (front != rear) { front++; rootNode = Nodes[front]; Console.WriteLine(rootNode.Data); if (rootNode.LNode != null) { rear++; Nodes[rear] = rootNode.LNode; } if (rootNode.RNode != null) { rear++; Nodes[rear] = rootNode.RNode; } } } //測試的主方法 static void Main(string[] args) { nodes<string> rootNode = BinTree(); Console.WriteLine("先序遍曆方法遍曆二叉樹:"); PreOrder<string>(rootNode); Console.WriteLine("中序遍曆方法遍曆二叉樹:"); MidOrder<string>(rootNode); Console.WriteLine("後序遍曆方法遍曆二叉樹:"); AfterOrder<string>(rootNode); Console.WriteLine("層次遍曆方法遍曆二叉樹:"); LayerOrder<string>(rootNode); Console.Read(); } } }
最後更新:2017-04-02 04:00:24