C# 完全二叉樹
阿新 • • 發佈:2020-08-12
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace 快排 { class Tree { private int _data; private Tree _leftChild; private Tree _rightChild; public Tree(int value) { this._data = value; this._leftChild = null; this._rightChild = null; } public int data { get { return this._data; } set { this._data = value; } } public Tree leftChild { get { return this._leftChild; }set { this._leftChild = value; } } public Tree rightChild { get { return this._rightChild; } set { this._rightChild = value; } } } class text { private static int dataCount = 100; //資料量 static Tree comTree = new Tree(0); //完全二叉樹的根節點 static Tree sortTree = new Tree(0); //排序二叉樹的根節點 static List<Tree> comList = new List<Tree>(); static int[] ranData = new int[dataCount]; static int comLeafCount = 0; static int sortLeafCount = 0; static void Main(string[] args) { Random ran = new Random(); for(int i=0;i<dataCount;i++) { ranData[i] = ran.Next(1, 200); } do { switch (menu()) { case 1: { comTree = new Tree(0); buildComTree(); Console.WriteLine("完全二叉樹構建完成!按任意鍵繼續...\n"); Console.ReadKey(); break; } case 2: { sortTree = new Tree(0); buildSortTree(); Console.WriteLine("排序二叉樹構建完成!按任意鍵繼續...\n"); Console.ReadKey(); break; } case 3: { if(sortTree.data==0) { Console.WriteLine("排序二叉樹尚未被構建!按任意鍵繼續...\n"); } else { InOrder(sortTree); Console.WriteLine(); Console.WriteLine("\n中序序列輸出完畢!按任意鍵繼續...\n"); } Console.ReadKey(); break; } case 4: { if (sortTree.data != 0) { sortLeafCount = 0; countSortLeaf(sortTree); Console.WriteLine("leafCount:" + sortLeafCount + "\n"); } if ( comTree.data != 0 ) { comLeafCount = 0; countComLeaf(comTree); Console.WriteLine("leafCount:" + comLeafCount + "\n"); } Console.WriteLine("輸出完畢!按任意鍵繼續...\n"); Console.ReadKey(); break; } case 5: { if (sortTree.data != 0) { int leafDepth = 0; leafDepth = depth(sortTree); Console.WriteLine("sortTreeDepth:" + leafDepth + "\n"); } if (comTree.data != 0) { int leafDepth = 0; leafDepth = depth(comTree); Console.WriteLine("comTreeDepth:" + leafDepth + "\n"); } Console.WriteLine("輸出完畢!按任意鍵繼續...\n"); Console.ReadKey(); break; } default: return; } } while (true); } /// <summary> /// 顯示選單,提示使用者輸入,處理輸入並作為返回值 /// </summary> static int menu() { int selected; Console.WriteLine("\n\n 選單\n\n"); Console.WriteLine("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n"); Console.WriteLine("1 .生成完全二叉樹\n"); Console.WriteLine("2 .生成二叉排序樹\n"); Console.WriteLine("3 .輸出中序遍歷序列\n"); Console.WriteLine("4 .計算葉子結點數\n"); Console.WriteLine("5 .計算二叉樹深度\n"); Console.WriteLine("6 .退出程式\n"); Console.WriteLine("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n\n"); do { try { selected = int.Parse(Console.ReadLine()); } catch (Exception) { Console.WriteLine("請輸入正確的選項!\n"); continue; } if(selected>=1 && selected<=7) { return selected; } else { Console.WriteLine("請輸入正確的選項!\n"); } } while (true); } /// <summary> /// 建立完全二叉樹 /// </summary> static void buildComTree() { comList.Clear(); comTree.data = ranData[0]; comList.Add(comTree); for(int i=1;i<dataCount;i++) { Tree comTree2 = new Tree(ranData[i]); comList.Add(comTree2); if(i%2==0) { comList[(i-1)/2].rightChild = comTree2; } else { comList[i/2].leftChild = comTree2; } } } /// <summary> /// 建立排序二叉樹 /// </summary> static void buildSortTree() { sortTree.data = ranData[0]; for(int i=1;i<dataCount;i++) { Tree newSort = new Tree(ranData[i]); Tree temp = sortTree; while(true) { if(temp.data<=newSort.data) { if (temp.rightChild == null) { temp.rightChild = newSort; break; } else { temp = temp.rightChild; } } else { if (temp.leftChild == null) { temp.leftChild = newSort; break; } else { temp = temp.leftChild; } } } } } /// <summary> /// 計算完全二叉樹的葉子數 /// </summary> static void countComLeaf(Tree root) { if (root != null) { if (root.leftChild == null && root.rightChild == null) { comLeafCount++; } else { if (root.leftChild != null) { countComLeaf(root.leftChild); } if (root.rightChild != null) { countComLeaf(root.rightChild); } } } } /// <summary> /// 計算排序二叉樹的葉子數 /// </summary> static void countSortLeaf(Tree root) { if (root != null) { if (root.leftChild == null && root.rightChild == null) { sortLeafCount++; } else { if (root.leftChild != null) { countSortLeaf(root.leftChild); } if (root.rightChild != null) { countSortLeaf(root.rightChild); } } } } /// <summary> /// 計算二叉樹的深度,並作為返回值 /// </summary> static int depth(Tree root) { int treeDepth = 0; if(root!=null) { int depthLeft = depth(root.leftChild); int depthRight = depth(root.rightChild); treeDepth = 1 + (depthLeft >= depthRight ? depthLeft : depthRight); } return treeDepth; } /// <summary> /// 用以輸出中序序列 /// </summary> /// <param name="root"></param> static void InOrder(Tree root) { if (root != null) { InOrder(root.leftChild); Console.Write(root.data+" "); InOrder(root.rightChild); } } } }