C#二叉搜索樹插入算法實(shí)例分析
更新時(shí)間:2015年04月27日 11:49:20 作者:lele
這篇文章主要介紹了C#二叉搜索樹插入算法,實(shí)例分析了C#二叉樹操作的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了C#二叉搜索樹插入算法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
public class BinaryTreeNode
{
public BinaryTreeNode Left { get; set; }
public BinaryTreeNode Right { get; set; }
public int Data { get; set; }
public BinaryTreeNode(int data)
{
this.Data = data;
}
}
public void InsertIntoBST(BinaryTreeNode root, int data)
{
BinaryTreeNode _newNode = new BinaryTreeNode(data);
BinaryTreeNode _current = root;
BinaryTreeNode _previous = _current;
while (_current != null)
{
if (data < _current.Data)
{
_previous = _current;
_current = _current.Left;
}
else if (data > _current.Data)
{
_previous = _current;
_current = _current.Right;
}
}
if (data < _previous.Data)
_previous.Left = _newNode;
else
_previous.Right = _newNode;
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#在PDF中繪制不同風(fēng)格類型的文本方法實(shí)例
這篇文章主要給大家介紹了關(guān)于C#在PDF中繪制不同風(fēng)格類型的文本的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
解析使用C# lock同時(shí)訪問共享數(shù)據(jù)
本篇文章是對(duì)使用C# lock同時(shí)訪問共享數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法
下面小編就為大家?guī)硪黄狢#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
C#如何正確實(shí)現(xiàn)一個(gè)自定義異常Exception
這篇文章主要為大家詳細(xì)介紹了C#如何正確實(shí)現(xiàn)一個(gè)自定義異常Exception,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09

