什么是“分钟”?和“最大”在这个函数中检查二叉树是否是有效的 BST?

发布于 2025-01-01 03:34:22 字数 586 浏览 0 评论 0原文

下面的代码来自 查找二叉树是否是二叉搜索树

bool IsValidBST(BinaryNode node, int MIN, int MAX) 
{
     if(node == null)
     return true;

    if(node.element > MIN 
         && node.element < MAX
         && IsValidBST(node.left,MIN,node.element)
         && IsValidBST(node.right,node.element,MAX))
         return true;
     else 
         return false;
}

这里如何使用 MINMAX ?它们代表什么价值观?为什么它们需要通过该函数传递?

The below code is from Finding if a Binary Tree is a Binary Search Tree.

bool IsValidBST(BinaryNode node, int MIN, int MAX) 
{
     if(node == null)
     return true;

    if(node.element > MIN 
         && node.element < MAX
         && IsValidBST(node.left,MIN,node.element)
         && IsValidBST(node.right,node.element,MAX))
         return true;
     else 
         return false;
}

How are MIN and MAX being used here? What values do they represent? And why would they need to be passed through the function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞美少年 2025-01-08 03:34:22

[MIN, MAX] 表示当前节点中有效值的范围。对于第一个节点,这将是 INT_MIN 和 INT_MAX,因为它可以具有任意值,但对于其子节点,我们需要检查 BST 属性 - 所有左子节点不应大于当前节点,所有右子节点不应小于当前节点。这就是为什么我们分别更改它们的 MIN 和 MAX 值。

注意:如果在树中可以有重复值,请将检查更改为:

 if(node.element >= MIN 
   && node.element <= MAX
   && IsValidBST(node.left,MIN,node.element)
   && IsValidBST(node.right,node.element,MAX))

否则您将得到无效结果。

[MIN, MAX] represents the range in which a valid value in the current node could be. For the first node this would be INT_MIN and INT_MAX because it can have arbitrary values, but for its children we need to check the BST property - all the left children should not be greater then the current node and all the right ones should not be less. That is why we change the MIN and MAX values for them respectvely.

NOTE: if in the tree you can have repeating values, change the check to:

 if(node.element >= MIN 
   && node.element <= MAX
   && IsValidBST(node.left,MIN,node.element)
   && IsValidBST(node.right,node.element,MAX))

Or you will get invalid results.

却一份温柔 2025-01-08 03:34:22

您应该按如下方式调用函数。其中INT_MIN和INT_MAX是定义的常量。

IsValidBST (root, INT_MIN, INT_MAX)

但这种方法并不适用于所有数据。意思是我们不知道最小值和最大值的数据,例如字符串或任何任意用户定义的类型。

要查明给定的 BT 对于任何数据类型是否都是 BST,您需要使用以下方法。
1.使用中序遍历调用递归函数直到叶节点末尾
2. 自己建立最小值和最大值。

前提是,树元素必须定义小于/大于运算符。

#define MIN (FirstVal, SecondVal) ((FirstVal) < (SecondVal)) ? (FirstVal):(SecondVal)
#define MAX (FirstVal, SecondVal) ((FirstVal) > (SecondVal)) ? (FirstVal):(SecondVal)

template <class T>
bool IsValidBST (treeNode &root)
{

   T min,  max;
   return IsValidBST (root, &min, &max);
}

template <class T>
bool IsValidBST (treeNode *root, T *MIN , T *MAX)
{
   T leftMin, leftMax, rightMin, rightMax;
   bool isValidBST;

   if (root->leftNode == NULL && root->rightNode == NULL)
   {
      *MIN = root->element;
      *MAX = root->element;
      return true;
   }

  isValidBST = IsValidBST (root->leftNode, &leftMin, &leftMax);

  if (isValidBST)
    isValidBST = IsValidBST (root->rightNode, &rightMin, &rightMax);

  if (isValidBST)
  {
     *MIN = MIN (leftMIN, rightMIN);
     *Max = MAX (rightMax, leftMax);
  }

  return isValidBST;
}

You should call function as below. In which INT_MIN and INT_MAX are constant defined.

IsValidBST (root, INT_MIN, INT_MAX)

But this is approach will not work for all data. Means, data for which we don't know MIN and MAX values, like strings or any arbitrary user defined types.

To find out whether given BT is BST for any datatype, you need go with below approach.
1. call recursive function till the end of leaf node using inorder traversal
2. Build your min and max values yourself.

Provided that, tree element must have less than / greater than operator defined.

#define MIN (FirstVal, SecondVal) ((FirstVal) < (SecondVal)) ? (FirstVal):(SecondVal)
#define MAX (FirstVal, SecondVal) ((FirstVal) > (SecondVal)) ? (FirstVal):(SecondVal)

template <class T>
bool IsValidBST (treeNode &root)
{

   T min,  max;
   return IsValidBST (root, &min, &max);
}

template <class T>
bool IsValidBST (treeNode *root, T *MIN , T *MAX)
{
   T leftMin, leftMax, rightMin, rightMax;
   bool isValidBST;

   if (root->leftNode == NULL && root->rightNode == NULL)
   {
      *MIN = root->element;
      *MAX = root->element;
      return true;
   }

  isValidBST = IsValidBST (root->leftNode, &leftMin, &leftMax);

  if (isValidBST)
    isValidBST = IsValidBST (root->rightNode, &rightMin, &rightMax);

  if (isValidBST)
  {
     *MIN = MIN (leftMIN, rightMIN);
     *Max = MAX (rightMax, leftMax);
  }

  return isValidBST;
}
絕版丫頭 2025-01-08 03:34:22

它们必须通过函数传递,以便以越来越小的范围递归调用,就像这里一样。

当然,对于第一次调用,您可以从树中确定 MIN 和 MAX,但对于后续调用,您必须像这样传递下去。

They have to be passed through the function in order for it to be called recursively with increasing reduced ranges as it is here.

Of course, for the first call, you could determine MIN and MAX from the tree, but for subsequent ones you must pass down like this.

本宫微胖 2025-01-08 03:34:22

这里MIN MAX指定树中存储的最小值和树中存储的最大值。

I think simplest way to check the valid BST tree is traverse inorder and check if
the values are in sorted order or not? if all the values are in sorted order that
means its valid BST.

实现
http://justprogrammng.blogspot .com/2012/06/check-if-tree-is-bst-on-code-interview.html

Here MIN MAX specifies the minimum value stored in tree and the maximum value stored in the tree.

I think simplest way to check the valid BST tree is traverse inorder and check if
the values are in sorted order or not? if all the values are in sorted order that
means its valid BST.

Implementation
http://justprogrammng.blogspot.com/2012/06/check-if-tree-is-bst-on-code-interview.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文