C#实现二叉查找树





    /// <summary>
    /// 二叉树节点的定义
    /// </summary>
    public class Node
    {
        //本身的数据
        public int data;
        //左右节点
        public Node left;
        public Node right;

        public void DisplayData()
        {
            Console.Write(data + "  ");
        }
    }

    public class BinarySearchTree
    {

        Node rootNode = null;

        public void Insert(int data)
        {
            Node parent;
            //包装数据进节点
            Node newNode = new Node();
            newNode.data = data;
            //根节点
            if (rootNode == null)
            {
                rootNode = newNode;
            }
            //找到合适的子节点位置插入
            else
            {
                Node currentNode = rootNode;
                while (true)
                {
                    parent = currentNode;
                    if (newNode.data < currentNode.data)
                    {
                        currentNode = currentNode.left;
                        if (currentNode == null)
                        {
                            parent.left = newNode;
                            break;
                        }
                    }
                    else
                    {
                        currentNode = currentNode.right;
                        if (currentNode == null)
                        {
                            parent.right = newNode;
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 中序遍历
        /// </summary>
        /// <param name="theRoot"></param>
        public void InOrder(Node theRoot)
        {
            if (theRoot != null)
            {
                InOrder(theRoot.left);
                theRoot.DisplayData();
                InOrder(theRoot.right);
            }
        }
        //先序遍历
        public void PreOrder(Node theRoot)
        {
            if (theRoot != null)
            {
                theRoot.DisplayData();
                PreOrder(theRoot.left);
                PreOrder(theRoot.right);
            }
        }
        //后序遍历
        public void PostOrder(Node theRoot)
        {
            if (theRoot != null)
            {
                PostOrder(theRoot.left);
                PostOrder(theRoot.right);
                theRoot.DisplayData();
            }
        }


        public void FindMax()
        {
            Node current = rootNode;
            //找到最右边的即可
            while (current.right != null)
            {
                current = current.right;
            }
            Console.WriteLine("max:" + current.data);
        }

        public void FindMin()
        {
            Node current = rootNode;
            //找到最左边的节点即可
            while (current.left != null)
            {
                current = current.left;
            }
            Console.WriteLine("min:" + current.data);
        }

        public Node Search(int i)
        {
            Node current = rootNode;
            while (true)
            {
                if (i < current.data)
                {
                    if (current.left == null) break;
                    current = current.left;
                }
                else if (i > current.data)
                {
                    if (current == null) break;
                    current = current.right;
                }
                else
                {
                    return current;
                }
            }

            if (current.data != i)
            {
                return null;
            }
            return current;
        }

        public Node Delete(int key)
        {
            Node parent = rootNode;
            Node current = rootNode;
            //首先找到需要被删除的节点和其父节点
            while (true)
            {
                if (key < current.data)
                {
                    if (current.left == null) break;
                    parent = current;
                    current = current.left;
                }
                else if (key > current.data)
                {
                    if (current == null) break;
                    parent = current;
                    current = current.right;
                }
                else
                {
                    break;
                }
            }

            //找到被删除的节点后,分四种情况

            //1.当是子节点的时候,直接删除
            if (current.left == null && current.right == null)
            {
                if (current == rootNode && rootNode.left == null && rootNode.right == null)
                {
                    rootNode = null;
                }
                else if (current.data < parent.data)
                {
                    parent.left = null;
                }
                else
                {
                    parent.right = null;
                }
            }
             //所删除的节点只有左节点的时
            else if (current.left != null && current.right == null)
            {
                if (current.data < parent.data)
                    parent.left = current.left;
                else
                    parent.right = current.left;
            }
            //所删除的节点只有右节点时
            else if (current.left == null && current.right != null)
            {
                if (current.data < parent.data)
                    parent.left = current.right;
                else
                    parent.right = current.right;
            }
            //当删除的节点有左右节点的时候
            else
            {
                //current是被删除的节点,temp是被删左子树最右边的节点
                Node temp;
                //先判断是父节点的左节点还是右节点
                if (current.data < parent.data)
                {
                    parent.left = current.left;
                    temp = current.left;
                    //寻找被删除节点最深的右孩子
                    while (temp.right != null)
                    {
                        temp = temp.right;
                    }
                    temp.right = current.right;
                }
                //右节点
                else if (current.data > parent.data)
                {
                    parent.right = current.left;
                    temp = current.left;
                    //寻找被删除节点最深的左孩子
                    while (temp.left != null)
                    {
                        temp = temp.left;
                    }
                    temp.right = current.right;点击打开链接
                }
                //当被删节点是根节点,并且有两个孩子时
                else
                {
                    temp = current.left;
                    while (temp.right != null)
                    {
                        temp = temp.right;
                    }
                    temp.right = rootNode.right;
                    rootNode = current.left;
                }
            }

            return current;
        }
    }




转载自 :  点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值