C#二叉搜索樹插入算法實例分析
更新時間:2015年04月27日 11:49:20 作者:lele
這篇文章主要介紹了C#二叉搜索樹插入算法,實例分析了C#二叉樹操作的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了C#二叉搜索樹插入算法。分享給大家供大家參考。具體實現(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; }
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C#中序列化實現(xiàn)深拷貝,實現(xiàn)DataGridView初始化刷新的方法
下面小編就為大家?guī)硪黄狢#中序列化實現(xiàn)深拷貝,實現(xiàn)DataGridView初始化刷新的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02C#如何正確實現(xiàn)一個自定義異常Exception
這篇文章主要為大家詳細(xì)介紹了C#如何正確實現(xiàn)一個自定義異常Exception,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09