设为首页 加入收藏

TOP

了解集合本质必须要知晓的概念04-二叉查找树(一)
2019-09-02 23:43:05 】 浏览:33
Tags:了解 集合 本质 须要 知晓 概念 04- 查找

与链表、堆栈和队列不一样,二叉查找树不是线性数据结构,是二维数据结构。每个节点都包含一个LeftNode和RightNode,二叉查找树把比节点数据项小的数据放在LeftNode,把比节点数据项大的数据放在RightNode。


关于节点的类。

    public class TreeNode<T>
    {
        public T Element { get; set; }
        public TreeNode<T>  LeftNode { get; set; }
        public TreeNode<T>  RightNode { get; set; }
        public TreeNode(T element)
        {
            this.Element = element;
            LeftNode = RightNode = null;
        }
        public override string ToString()
        {
            string nodeString = "[" + this.Element + " ";
            if (this.LeftNode == null && this.RightNode == null)
            {
                nodeString += " (叶节点) ";
            }
            if (this.LeftNode != null)
            {
                nodeString += "左节点:" + this.LeftNode.ToString();
            }
            if (this.RightNode != null)
            {
                nodeString += "右节点:" + this.RightNode.ToString();
            }
            nodeString += "]";
            return nodeString;
        }
    }

以上,把比节点数据项Element小的数据所在节点赋值给LeftNode,把比节点数据项Element大的数据所在节点赋值给RightNode。


创建一个泛型二叉树查找类,维护着一个根节点,并提供各种对节点的操作方法。

    public class BinarySearchTree<T>
    {
        public TreeNode<T> Root { get; set; }
        public BinarySearchTree()
        {
            this.Root = null;
        }
        //把某个数据项插入到二叉树
        public void Insert(T x)
        {
            this.Root = Insert(x, this.Root);
        }
        //把某个数据项从二叉树中删除
        public void Remove(T x)
        {
            this.Root = Remove(x, this.Root);
        }
        //删除二叉树中的最小数据项
        public void RemoveMin()
        {
            this.Root = RemoveMin(this.Root);
        }
        //获取二叉树中的最小数据项
        public T FindMin()
        {
            return ElemntAt(FindMin(this.Root));
        }
        //获取二叉树中的最大数据项
        public T FindMax()
        {
            return ElemntAt(FindMax(this.Root));
        }
        //获取二叉树中的某个数据项
        public T Find(T x)
        {
            return ElemntAt(Find(x, this.Root));
        }
        //清空
        public void MakeEmpty()
        {
            this.Root = null;
        }
        //判断二叉树是否为空,是否存在
        public bool IsEmpty()
        {
            return this.Root == null;
        }
        //获取某个节点的数据项
        private T ElemntAt(TreeNode<T> t)
        {
            return t == null ? default(T) : t.Element;
        }
        /// <summary>
        /// 查找节点
        /// </summary>
        /// <param name="x">要查找数据项</param>
        /// <param name="t">已存在的节点</param>
        /// <returns>返回节点</returns>
        private TreeNode<T> Find(T x, TreeNode<T> t)
        {
            while (t != null)//当没有找到匹配数据项,不断调整查找范围,即t的值
            {
                if ((x as IComparable).CompareTo(t.Element) < 0)
                {
                    t = t.LeftNode;
                }
                else if ((x as IComparable).CompareTo(t.Element) > 0)
                {
                    t = t.RightNode;
                }
                else //如果找到数据项,就返回当前t的值
                {
                    return t;
                }
            }
            return null;
        }
        //获取最小的节点,
        private TreeNode<T> FindMin(TreeNode<T> t)
        {
            if (t != null)
            {
                while (t.LeftNode != null)//不断循环二叉树的左半边树
                {
                    t = t.LeftNode; //不断设置t的值
                }
            }
            return t;
        }
        //获取最大的节点
        private TreeNode<T> FindMax(TreeNode<T> t)
        {
            if (t != null)
            {
                while (t.RightNode != null)
                {
                    t = t.RightNode;
                }
            }
            return t;
        }
        /// <summary>
        /// 插入节点
        /// </summary>
        /// <param name="x">要插入的数据项</param>
        /// <param name="t">已经存在的节点</param>
        /// <returns>返回已存在的节点</returns>
        protected TreeNode<T> Insert(T x, TreeNode<T> t)
        {
            if (t == null)
            {
                t = new TreeNode<T>(x);
            }
            else if ((x as IComparable).CompareTo(t.Element) < 0)
            {
                //等号右边的t.LeftNode是null,因此会创建一个TreeNode实例给t.LeftNode
                t.LeftNode = Insert(x, t.LeftNode);
            }
            else if ((x as IComparable).CompareTo(t.E
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇委托 delegate, 继承 下一篇关于async和await的一些误区

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目