C#實(shí)現(xiàn)二叉排序樹代碼實(shí)例
二叉排序樹,又稱為二叉查找樹。它或者是一顆空樹,或者是具有下列性質(zhì)的二叉樹:
- 若它的左子樹不為空。則左子樹上所有的結(jié)點(diǎn)的值均小于跟的結(jié)點(diǎn)值
- 若它的右子樹部位空,則右子樹的所有結(jié)點(diǎn)值均大于它的根結(jié)點(diǎn)的值
- 它的左右子樹也分別是二叉排序樹
1,排序方便
2,查找方便
3,便于插入和刪除
C#鏈?zhǔn)酱鎯Χ媾判驑?,?shí)現(xiàn)簡單的排序,以及查找,具體代碼如下:
namespace _2_1_3二叉排序樹 { /// <summary> /// 結(jié)點(diǎn)類 /// </summary> class BSNode { //結(jié)點(diǎn) public BSNode LeftChild { get; set; } public BSNode RightChild { get; set; } public BSNode Parent { get; set; } public int Data { get; set; } // 構(gòu)造方法 public BSNode(){} public BSNode(int item) { this.Data = item; } } } using System; namespace _2_1_3二叉排序樹 { /// <summary> /// 二叉排序樹 /// </summary> class BSTree { BSNode root = null; /// <summary> /// 添加數(shù)據(jù) /// </summary> public void Add(int item) { //創(chuàng)建新結(jié)點(diǎn) BSNode newNode = new BSNode(item); if (root == null) //若為空,則創(chuàng)建為根結(jié)點(diǎn) { root = newNode; } else { BSNode temp = root; while (true) { if (item >= temp.Data) //放在temp結(jié)點(diǎn)的右邊 { if (temp.RightChild == null) { temp.RightChild = newNode; newNode.Parent = temp; break; } else { temp = temp.RightChild; } } else //放在temp結(jié)點(diǎn)的左邊 { 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) { //葉子結(jié)點(diǎn),即無子樹情況 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; } //刪除的結(jié)點(diǎn)有左,右子樹 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("中序遍歷的結(jié)果:"); 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("刪除根結(jié)點(diǎn)后的結(jié)果:"); tree.Delete(62); tree.MiddleBianli(); Console.ReadKey(); } } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
上一篇:C# winform程序?qū)崿F(xiàn)開機(jī)自啟動并且識別是開機(jī)啟動還是雙擊啟動
欄 目:C#教程
下一篇:如何獲取C#中方法的執(zhí)行時(shí)間以及其代碼注入詳解
本文標(biāo)題:C#實(shí)現(xiàn)二叉排序樹代碼實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5018.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什