C# 二叉排序树

二叉排序树:

  二叉排序树又称为二叉查找树。它或者是一棵空树,或者是具有下列性质的二叉树。若它的左子树不为空,则左子树上所有的结点的值均小于根结构的值;若它的右子树不为空,则右字数上所有结点的值均大于它的根结点的值;它的左右子树也分别为二叉排序树。

优点:

  1. 方便排序
  2. 方便查找
  3. 方便插入、删除

 结点类定义:

internal class BSNode
    {
        public BSNode leftChild { get; set; }
        public BSNode rightChild { get; set; }
        public BSNode parent { get; set; }
        public  int Data { get;  set; }
        public BSNode()
        {

        }
        public BSNode(int item)
        {
            this.Data = item;
        }
    }

 树类定义:

internal class BSTree
    {
        BSNode root = null;
        public void Add(int item)
        {
            BSNode newNode = new BSNode(item);
            if(root == null)
            {
                root = newNode;
            }
            else
            {
                BSNode temp = root;
                while(true)
                {
                    if (item >= temp.Data)//放在temp右边
                    {
                        if(temp.rightChild == null)
                        {
                            temp.rightChild = newNode;
                            newNode.parent = temp;
                            break;
                        }
                        else
                        {
                            temp = temp.rightChild;
                        }
                    }
                    else//放在temp左边
                    {
                        if(temp.leftChild == null)
                        {
                            temp.leftChild = newNode;
                            newNode.parent = temp;
                            break;
                        }
                        else
                        {
                            temp = temp.leftChild;
                        }
                    }
                }
            }
        }
        public void inorderTraversal()
        {
            inorderTraversal(root);
        }
        private void inorderTraversal(BSNode node)
        {
            if (node == null) return;
            inorderTraversal(node.leftChild);
            Console.Write(node.Data+" ");
            inorderTraversal(node.rightChild);
        }
        public bool Find(int item)
        {
            //return Find(item,root);
            BSNode temp = root;
            while (true)
            {
                if(temp == null)return false;
                if(temp.Data == item)return true;
                if (item > temp.Data)
                {
                    temp = temp.rightChild;
                }
                else
                {
                    temp = temp.leftChild;
                }
            }
        }
        private bool Find(int item,BSNode node)
        {
            if(node == null) return false;
            if (node.Data == item) 
            {
                return true;
            }
            else
            {
                //if (Find(item, node.leftChild)) return true;
                //if (Find(item, node.rightChild)) return true;
                if(item > node.Data)
                {
                    return Find(item, node.rightChild);
                }
                else
                {
                    return Find(item, node.leftChild);
                }

            }
        }
        public bool Delete (int item)
        {
            BSNode temp = root;
            while (true)
            {
                if (temp == null) return false;
                if (temp.Data == item)
                {
                    Delete(temp);
                    return true;

                }
                if (item > temp.Data)
                {
                    temp = temp.rightChild;
                }
                else
                {
                    temp = temp.leftChild;
                }
            }
        }
        public void Delete(BSNode node)
        {

            //叶子结点
            if (node.leftChild == null && node.rightChild == null)
            {
                if (node.parent == null) 
                {
                    root = null;
                }else if (node.parent.rightChild == node)
                {
                    node.parent.rightChild = null;
                }
                else if(node.parent.leftChild == node)
                {
                    node.parent.leftChild = null;
                }
                return;
            }
            //仅有左子树或者右子数的结点
            if (node.leftChild == null && node.rightChild != null)
            {
                node.Data = node.rightChild.Data;
                node.rightChild = null;
                return;
            }else if(node.rightChild == null && node.leftChild != null)
            {
                node.Data = node.leftChild.Data;
                node.rightChild = null;
                return;
            }
            //左右子树都有
            BSNode temp = node.rightChild;
            while(true)
            {
                if (temp.leftChild != null)
                {
                    temp = temp.leftChild;
                }
                else
                {
                    break;
                }
            }
            node.Data = temp.Data;
            Delete(temp);
        }
    }

主函数: 

internal class Program
    {
        static void Main(string[] args)
        {
            BSTree tree = new BSTree();
            int[] data = { 62, 58, 88, 47, 73, 99, 35, 51, 93, 37 };
            foreach (int t in data)
            {
                tree.Add(t);
            }
            tree.inorderTraversal();
            Console.WriteLine();
            Console.WriteLine(tree.Find(99));
            Console.WriteLine(tree.Find(999));
            tree.Delete(37);
            tree.inorderTraversal();
            Console.WriteLine();
            tree.Delete(73);
            tree.inorderTraversal();
        }
    }

结果:

上文:http://t.csdn.cn/JBaNVicon-default.png?t=N2N8http://t.csdn.cn/JBaNV

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值