[数据结构与算法]二叉树查找结点和最大最小值

由于BST的属性,所以查找最大与最小值的代码几乎是微不足道的事情。人们总可以在根节点左子树的最左侧的节点上找到BST内的最小值,另一方面,则会在跟节点有字数的最右侧节点上找到BST内的最大值。
   public int FindMin() 
        {
            Node current = root;
            while (!(current.Left == null)) 
            {
                current = current.Left;
            }
            return current.Data;
        }
        public int FindMax()
        {
            Node current = root;
            while (!(current.Right == null))
            {
                current = current.Right;
            }
            return current.Data;
        }
        public Node Find(int key) 
        {
            Node current = root;
            while (current.Data != key) 
            {
                if (key < current.Data)
                {
                    current = current.Left;
                }
                else 
                {
                    current = current.Right;
                }
                if (current == null)
                {
                    return null;
                }

            }
            return current;
        }
    原文作者:查找算法
    原文地址: https://blog.csdn.net/david_520042/article/details/52766302
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞