二叉搜索树插入算法C#


本文摘自PHP中文网,作者大家讲道理,侵删。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

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#测试代码执行时间的方法

c#分别用前序遍历、中序遍历和后序遍历打印二叉树

c# 四舍五入round函数使用

c#访问sqlserver的工具类sqlserverhelper

c#操作mysql的工具类mysqlhelper

c#中登陆账户使用的md5加密算法

c#将文件上传、下载(以二进制流保存到数据库)

c#自定读取配置文件类

c#读取host文件代码

二叉搜索树插入算法c#

更多相关阅读请进入《代码片段》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...