由于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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。