網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
與鏈表、堆棧和隊(duì)列不一樣,二叉查找樹(shù)不是線性數(shù)據(jù)結(jié)構(gòu),是二維數(shù)據(jù)結(jié)構(gòu)。每個(gè)節(jié)點(diǎn)都包含一個(gè)LeftNode和RightNode,二叉查找樹(shù)把比節(jié)點(diǎn)數(shù)據(jù)項(xiàng)小的數(shù)據(jù)放在LeftNode,把比節(jié)點(diǎn)數(shù)據(jù)項(xiàng)大的數(shù)據(jù)放在RightNode。
關(guān)于節(jié)點(diǎn)的類。
public class TreeNode<T>
{
public T Element { get; set; }
public TreeNode<T> LeftNode { get; set; }
public TreeNode<T> RightNode { get; set; }
public TreeNode(T element)
{
this.Element = element;
LeftNode = RightNode = null;
}
public override string ToString()
{
string nodeString = "[" + this.Element + " ";
if (this.LeftNode == null && this.RightNode == null)
{
nodeString += " (葉節(jié)點(diǎn)) ";
}
if (this.LeftNode != null)
{
nodeString += "左節(jié)點(diǎn):" + this.LeftNode.ToString();
}
if (this.RightNode != null)
{
nodeString += "右節(jié)點(diǎn):" + this.RightNode.ToString();
}
nodeString += "]";
return nodeString;
}
}
以上,把比節(jié)點(diǎn)數(shù)據(jù)項(xiàng)Element小的數(shù)據(jù)所在節(jié)點(diǎn)賦值給LeftNode,把比節(jié)點(diǎn)數(shù)據(jù)項(xiàng)Element大的數(shù)據(jù)所在節(jié)點(diǎn)賦值給RightNode。
創(chuàng)建一個(gè)泛型二叉樹(shù)查找類,維護(hù)著一個(gè)根節(jié)點(diǎn),并提供各種對(duì)節(jié)點(diǎn)的操作方法。
public class BinarySearchTree<T>
{
public TreeNode<T> Root { get; set; }
public BinarySearchTree()
{
this.Root = null;
}
//把某個(gè)數(shù)據(jù)項(xiàng)插入到二叉樹(shù)
public void Insert(T x)
{
this.Root = Insert(x, this.Root);
}
//把某個(gè)數(shù)據(jù)項(xiàng)從二叉樹(shù)中刪除
public void Remove(T x)
{
this.Root = Remove(x, this.Root);
}
//刪除二叉樹(shù)中的最小數(shù)據(jù)項(xiàng)
public void RemoveMin()
{
this.Root = RemoveMin(this.Root);
}
//獲取二叉樹(shù)中的最小數(shù)據(jù)項(xiàng)
public T FindMin()
{
return ElemntAt(FindMin(this.Root));
}
//獲取二叉樹(shù)中的最大數(shù)據(jù)項(xiàng)
public T FindMax()
{
return ElemntAt(FindMax(this.Root));
}
//獲取二叉樹(shù)中的某個(gè)數(shù)據(jù)項(xiàng)
public T Find(T x)
{
return ElemntAt(Find(x, this.Root));
}
//清空
public void MakeEmpty()
{
this.Root = null;
}
//判斷二叉樹(shù)是否為空,是否存在
public bool IsEmpty()
{
return this.Root == null;
}
//獲取某個(gè)節(jié)點(diǎn)的數(shù)據(jù)項(xiàng)
private T ElemntAt(TreeNode<T> t)
{
return t == null ? default(T) : t.Element;
}
/// <summary>
/// 查找節(jié)點(diǎn)
/// </summary>
/// <param name="x">要查找數(shù)據(jù)項(xiàng)</param>
/// <param name="t">已存在的節(jié)點(diǎn)</param>
/// <returns>返回節(jié)點(diǎn)</returns>
private TreeNode<T> Find(T x, TreeNode<T> t)
{
while (t != null)//當(dāng)沒(méi)有找到匹配數(shù)據(jù)項(xiàng),不斷調(diào)整查找范圍,即t的值
{
if ((x as IComparable).CompareTo(t.Element) < 0)
{
t = t.LeftNode;
}
else if ((x as IComparable).CompareTo(t.Element) > 0)
{
t = t.RightNode;
}
else //如果找到數(shù)據(jù)項(xiàng),就返回當(dāng)前t的值
{
return t;
}
}
return null;
}
//獲取最小的節(jié)點(diǎn),
private TreeNode<T> FindMin(TreeNode<T> t)
{
if (t != null)
{
while (t.LeftNode != null)//不斷循環(huán)二叉樹(shù)的左半邊樹(shù)
{
t = t.LeftNode; //不斷設(shè)置t的值
}
}
return t;
}
//獲取最大的節(jié)點(diǎn)
private TreeNode<T> FindMax(TreeNode<T> t)
{
if (t != null)
{
while (t.RightNode != null)
{
t = t.RightNode;
}
}
return t;
}
/// <summary>
/// 插入節(jié)點(diǎn)
/// </summary>
/// <param name="x">要插入的數(shù)據(jù)項(xiàng)</param>
/// <param name="t">已經(jīng)存在的節(jié)點(diǎn)</param>
/// <returns>返回已存在的節(jié)點(diǎn)</returns>
protected TreeNode<T> Insert(T x, TreeNode<T> t)
{
if (t == null)
{
t = new TreeNode<T>(x);
}
else if ((x as IComparable).CompareTo(t.Element) < 0)
{
//等號(hào)右邊的t.LeftNode是null,因此會(huì)創(chuàng)建一個(gè)TreeNode實(shí)例給t.LeftNode
t.LeftNode = Insert(x, t.LeftNode);
}
else if ((x as IComparable).CompareTo(t.Element) > 0)
{
t.RightNode = Insert(x, t.RightNode);
}
else
{
throw new Exception("插入了相同元素~~");
}
return t;
}
//刪除最小的節(jié)點(diǎn)
//返回當(dāng)前根節(jié)點(diǎn)
protected TreeNode<T> RemoveMin(TreeNode<T> t)
{
if (t == null)
{
throw new Exception("節(jié)點(diǎn)不存在~~");
}
else if (t.LeftNode != null)
{
//通過(guò)遞歸不斷設(shè)置t.LeftNode,直到t.LeftNode=null
t.LeftNode = RemoveMin(t.LeftNode);
return t;
}
else //當(dāng)t.LeftNode=null的時(shí)候,就把t.RightNode當(dāng)作最小節(jié)點(diǎn)返回
{
return t.RightNode;
}
}
//刪除某數(shù)據(jù)項(xiàng),返回當(dāng)前根節(jié)點(diǎn)
protected TreeNode<T> Remove(T x, TreeNode<T> t)
{
if (t == null)
{
throw new Exception("節(jié)點(diǎn)不存在~~");
}
else if((x as IComparable).CompareTo(t.Element) < 0)
{
t.LeftNode = Remove(x, t.LeftNode);
}
else if ((x as IComparable).CompareTo(t.Element) > 0)
{
t.RightNode = Remove(x, t.RightNode);
}
else if (t.LeftNode != null && t.RightNode != null)
{
t.Element = FindMin(t.RightNode).Element;
t.RightNode = RemoveMin(t.RightNode);
}
else
{
t = (t.LeftNode != null) ? t.LeftNode : t.RightNode;
}
return t;
}
public override string ToString()
{
return this.Root.ToString();
}
}
客戶端創(chuàng)建二叉查找樹(shù)的實(shí)例,并調(diào)用實(shí)例方法插入隨機(jī)數(shù)據(jù)。
BinarySearchTree<int> intTree = new BinarySearchTree<int>();
Random r = new Random(DateTime.Now.Millisecond);
string trace = "";
//插入5個(gè)隨機(jī)數(shù)
for (int i = 0; i < 5; i++)
{
int randomInt = r.Next(1, 500);
intTree.Insert(randomInt);
trace += randomInt + " ";
}
Console.WriteLine("最大的節(jié)點(diǎn):" + intTree.FindMax());
Console.WriteLine("最小的節(jié)點(diǎn):" + intTree.FindMin());
Console.WriteLine("根節(jié)點(diǎn):" + intTree.Root.Element);
Console.WriteLine("插入節(jié)點(diǎn)的依次順序是:" + trace);
Console.WriteLine("打印樹(shù)為:" + intTree);
Console.ReadKey();
原文鏈接:https://www.cnblogs.com/darrenji/p/3897426.html
相關(guān)推薦
- 2022-05-10 springMVC 文件上傳和下載
- 2023-02-07 C語(yǔ)言可變參數(shù)與內(nèi)存管理超詳細(xì)講解_C 語(yǔ)言
- 2022-07-31 oracle定時(shí)任務(wù)定時(shí)無(wú)效的原因分析與解決_oracle
- 2022-08-13 Android自定義ProgressBar實(shí)現(xiàn)漂亮的進(jìn)度提示框_Android
- 2022-10-29 STDC分割網(wǎng)絡(luò):onnx推理
- 2022-06-30 Python中隱藏的五種實(shí)用技巧分享_python
- 2024-01-16 Oracle的取整函數(shù)
- 2022-07-18 通過(guò)注冊(cè)表實(shí)現(xiàn)程序開(kāi)機(jī)自啟動(dòng)的方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支