這篇文章主要介紹了C#如何實現二叉排序樹,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創新互聯公司2013年至今,先為金城江等服務建站,金城江等地企業,進行企業商務咨詢服務。為金城江企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。二叉排序樹,又稱為二叉查找樹。它或者是一顆空樹,或者是具有下列性質的二叉樹:
若它的左子樹不為空。則左子樹上所有的結點的值均小于跟的結點值
若它的右子樹部位空,則右子樹的所有結點值均大于它的根結點的值
它的左右子樹也分別是二叉排序樹
1,排序方便
2,查找方便
3,便于插入和刪除
C#鏈式存儲二叉排序樹,實現簡單的排序,以及查找,具體代碼如下:
namespace _2_1_3二叉排序樹 { /// <summary> /// 結點類 /// </summary> class BSNode { //結點 public BSNode LeftChild { get; set; } public BSNode RightChild { get; set; } public BSNode Parent { get; set; } public int Data { get; set; } // 構造方法 public BSNode(){} public BSNode(int item) { this.Data = item; } } } using System; namespace _2_1_3二叉排序樹 { /// <summary> /// 二叉排序樹 /// </summary> class BSTree { BSNode root = null; /// <summary> /// 添加數據 /// </summary> public void Add(int item) { //創建新結點 BSNode newNode = new BSNode(item); if (root == null) //若為空,則創建為根結點 { root = newNode; } else { BSNode temp = root; while (true) { if (item >= temp.Data) //放在temp結點的右邊 { if (temp.RightChild == null) { temp.RightChild = newNode; newNode.Parent = temp; break; } else { temp = temp.RightChild; } } else //放在temp結點的左邊 { if (temp.LeftChild == null) { temp.LeftChild = newNode; newNode.Parent = temp; break; } else { temp = temp.LeftChild; } } } } } /// <summary> /// 中序遍歷二叉樹 /// </summary> public void MiddleBianli() { MiddleBianli(root); } //遞歸方式中序遍歷樹 private void MiddleBianli(BSNode node) { if (node == null) return; MiddleBianli(node.LeftChild); Console.Write(node.Data + " "); MiddleBianli(node.RightChild); } /// <summary> ///查找方法-1 /// </summary> public bool Find1(int item) { return Find(item, root); } private bool Find(int item, BSNode node) { if (node == null) { return false; } if (node.Data == item) { return true; } else { //利用二叉排序樹的便利 if (item > node.Data) { return Find(item, node.RightChild); } else { return Find(item, node.LeftChild); } } } /// <summary> /// 查找方法-2 /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Find2(int item) { BSNode temp = root; while (true) { if (temp == null) return false; if (temp.Data == item) return true; if (item > temp.Data) { temp = temp.RightChild; } else { temp = temp.LeftChild; } } } public bool Delete(int item) { BSNode temp = root; while (true) { if (temp == null) return false; if (temp.Data == item) { Delete(temp); return true; } if (item > temp.Data) { temp = temp.RightChild; } else { temp = temp.LeftChild; } } } public void Delete(BSNode node) { //葉子結點,即無子樹情況 if (node.LeftChild == null && node.RightChild == null) { if (node.Parent == null) { root = null; } else if (node.Parent.LeftChild == node) { node.Parent.LeftChild = null; } else if (node.Parent.RightChild == node) { node.Parent.RightChild = null; } return; } //只有右子樹的情況 if (node.LeftChild == null && node.RightChild != null) { node.Data = node.RightChild.Data; node.RightChild = null; return; } //只有左子樹的情況 if (node.LeftChild != null && node.RightChild == null) { node.Data = node.LeftChild.Data; node.LeftChild = null; return; } //刪除的結點有左,右子樹 BSNode temp = node.RightChild; while (true) { if (temp.LeftChild != null) { temp = temp.LeftChild; } else { break; } } node.Data = temp.Data; Delete(temp); } } } using System; namespace _2_1_3二叉排序樹 { /// <summary> /// 測試類 /// </summary> class Program { static void Main(string[] args) { BSTree tree = new BSTree(); int[] data = {62,58,28,47,73,99,35,51,93,37 }; foreach (int item in data) { tree.Add(item); } Console.Write("中序遍歷的結果:"); tree.MiddleBianli(); Console.WriteLine(); Console.WriteLine("Find-1方法查找62是否存在:" + tree.Find1(62)); Console.WriteLine("Find-2方法查找62是否存在:" + tree.Find2(62)); Console.WriteLine("Find-1方法查找63是否存在:" + tree.Find1(63)); Console.WriteLine("Find-2方法查找63是否存在:" + tree.Find2(63)); Console.WriteLine("刪除根結點后的結果:"); tree.Delete(62); tree.MiddleBianli(); Console.ReadKey(); } } }
C#是一個簡單、通用、面向對象的編程語言,它由微軟Microsoft開發,繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優雅的語法風格、創新的語言特性和便捷的面向組件編程從而成為.NET開發的選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關鍵功能。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#如何實現二叉排序樹”這篇文章對大家有幫助,同時也希望大家多多支持創新互聯成都網站設計公司,關注創新互聯成都網站設計公司行業資訊頻道,更多相關知識等著你來學習!
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、網站設計器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網頁標題:C#如何實現二叉排序樹-創新互聯
URL網址:http://vcdvsql.cn/article10/cdigdo.html
成都網站建設公司_創新互聯,為您提供網站收錄、自適應網站、定制開發、品牌網站設計、網站導航、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯