C#實(shí)現(xiàn)二叉排序樹(shù)代碼實(shí)例
二叉排序樹(shù),又稱(chēng)為二叉查找樹(shù)。它或者是一顆空樹(shù),或者是具有下列性質(zhì)的二叉樹(shù):
- 若它的左子樹(shù)不為空。則左子樹(shù)上所有的結(jié)點(diǎn)的值均小于跟的結(jié)點(diǎn)值
- 若它的右子樹(shù)部位空,則右子樹(shù)的所有結(jié)點(diǎn)值均大于它的根結(jié)點(diǎn)的值
- 它的左右子樹(shù)也分別是二叉排序樹(shù)
1,排序方便
2,查找方便
3,便于插入和刪除

C#鏈?zhǔn)酱鎯?chǔ)二叉排序樹(shù),實(shí)現(xiàn)簡(jiǎn)單的排序,以及查找,具體代碼如下:
namespace _2_1_3二叉排序樹(shù)
{
/// <summary>
/// 結(jié)點(diǎn)類(lèi)
/// </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二叉排序樹(shù)
{
/// <summary>
/// 二叉排序樹(shù)
/// </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>
/// 中序遍歷二叉樹(shù)
/// </summary>
public void MiddleBianli()
{
MiddleBianli(root);
}
//遞歸方式中序遍歷樹(shù)
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
{
//利用二叉排序樹(shù)的便利
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),即無(wú)子樹(shù)情況
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;
}
//只有右子樹(shù)的情況
if (node.LeftChild == null && node.RightChild != null)
{
node.Data = node.RightChild.Data;
node.RightChild = null;
return;
}
//只有左子樹(shù)的情況
if (node.LeftChild != null && node.RightChild == null)
{
node.Data = node.LeftChild.Data;
node.LeftChild = null;
return;
}
//刪除的結(jié)點(diǎn)有左,右子樹(shù)
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二叉排序樹(shù)
{
/// <summary>
/// 測(cè)試類(lèi)
/// </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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Unity3D動(dòng)態(tài)對(duì)象優(yōu)化代碼分享
這篇文章主要介紹了Unity3D動(dòng)態(tài)對(duì)象優(yōu)化代碼分享的相關(guān)資料,需要的朋友可以參考下2015-03-03
桌面浮動(dòng)窗口(類(lèi)似惡意廣告)的實(shí)現(xiàn)詳解
本篇文章是對(duì)桌面浮動(dòng)窗口的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
利用C#/VB.NET實(shí)現(xiàn)PPT轉(zhuǎn)換為HTML
利用PowerPoint可以很方便的呈現(xiàn)多媒體信息,且信息形式多媒體化,表現(xiàn)力強(qiáng)。但難免在某些情況下我們會(huì)需要將PowerPoint轉(zhuǎn)換為HTML格式,本文就為大家整理了轉(zhuǎn)換方法,希望對(duì)大家有所幫助2023-05-05
WPF自定義控件實(shí)現(xiàn)ItemsControl魚(yú)眼效果
這篇文章主要為大家詳細(xì)介紹了WPF如何通過(guò)自定義控件實(shí)現(xiàn)ItemsControl魚(yú)眼效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01
使用GPS經(jīng)緯度定位附近地點(diǎn)(某一點(diǎn)范圍內(nèi)查詢(xún))
目前的工作是需要手機(jī)查找附近N米以?xún)?nèi)的商戶(hù),致想法是已知一個(gè)中心點(diǎn),一個(gè)半徑,求圓包含于圓拋物線(xiàn)里所有的點(diǎn),經(jīng)緯度是一個(gè)點(diǎn),半徑是一個(gè)距離,不能直接加減,下面提供C#的解決方法2013-12-12
C#基于Socket套接字的網(wǎng)絡(luò)通信封裝
這篇文章主要為大家詳細(xì)介紹了C#基于Socket套接字的網(wǎng)絡(luò)通信封裝本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖片旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下2021-11-11
c#實(shí)現(xiàn)字符串反序輸出字符串的實(shí)例
下面小編就為大家分享一篇c#實(shí)現(xiàn)字符串反序輸出字符串的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法
這篇文章主要介紹了C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法,涉及C#數(shù)組遍歷與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06

