C#二叉搜索树插入算法实例分析
程序员文章站
2022-07-01 17:57:48
本文实例讲述了c#二叉搜索树插入算法。分享给大家供大家参考。具体实现方法如下:
public class binarytreenode
{
public b...
本文实例讲述了c#二叉搜索树插入算法。分享给大家供大家参考。具体实现方法如下:
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#程序设计有所帮助。