本人用C#2.0實現了二叉樹的定義,怎麼構造一顆已知的二叉樹,用幾種常規的算法(先序,中序,後序,層次)遍歷二叉樹。希望能給有需要人帶來幫助,也希望能得到大家的指點。有關C#數據結構的書在書店裏找到,網上也是極少,如果你有好的學習資源別忘了告訴我。先謝了。 本程序中將用到一棵已知的二叉樹如圖(二叉樹圖)所示。
下面簡單介紹一下幾種算法和思路:
先序遍歷:
1. 訪問根結點
2. 按先序遍歷左子樹;
3. 按先序遍歷右子樹;
4. 例如:遍歷已知二叉樹結果爲:A->B->D->G->H->C->E->F
中序遍歷:
1. 按中序遍歷左子樹;
2. 訪問根結點;
3. 按中序遍歷右子樹;
4. 例如遍歷已知二叉樹的結果:B->G->D->H->A->E->C->F
後序遍歷:
1. 按後序遍歷左子樹;
2. 按後序遍歷右子樹;
3. 訪問根結點;
4. 例如遍歷已知二叉樹的結果:G->H->D->B->E->F->C->A
層次遍歷:
1. 從上到下,從左到右遍歷二叉樹的各個結點(實現時需要借輔助容器);
2. 例如遍歷已知二叉樹的結果:A->B->C->D->E->F->G->H
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////代碼:
using System; using System.Collections.Generic; using System.Text; namespace structure { /// <summary> /// 二叉遍歷算法解決方案 /// </summary> class Program { //二叉樹結點數據結構的定義 二叉樹結點數據結構的定義 //二叉樹結點數據結構包括數據域 左右結點以及父結點成員 class nodes<T> { T data; nodes<T> Lnode, Rnode, Pnode; public T Data { set { data = value; } get { return data; } } public nodes<T> LNode { set { Lnode = value; } get { return Lnode; } } public nodes<T> RNode { set { Rnode = value; } get { return Rnode; } } public nodes<T> PNode { set { Pnode = value; } get { return Pnode; } } public nodes() { } public nodes(T data) { this.data = data; } } //先序編歷二叉樹 先序編歷二叉樹 static void PreOrder<T>(nodes<T> rootNode) { if (rootNode != null) { Console.WriteLine(rootNode.Data); PreOrder<T>(rootNode.LNode); PreOrder<T>(rootNode.RNode); } } //構造一棵已知的二叉樹 構造一棵已知的二叉樹 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 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.ReadLine(); } } }