【问题标题】:How to verify if a given tree is a Binary Search Tree or not [duplicate]如何验证给定树是否是二叉搜索树[重复]
【发布时间】:2012-04-27 14:16:47
【问题描述】:

我想知道给定的二叉树是否是二叉搜索树。

我不知道该怎么做?

我唯一知道的是BST的中序遍历会给你升序输出。

那么,这是我们需要验证的唯一条件还是我们应该检查的其他任何内容。

如果还有一些其他必要条件需要检查,它们是什么?为什么需要检查这些条件?因为,我认为,INORDER 遍历本身可以很容易地告诉你给定的树是否是 BST。

【问题讨论】:

  • the definition of a Binary Search Tree 开头。给定的树是否符合这个定义? (请注意,可以通过递归应用规则来导出中序遍历规则。另请注意,不同的树,例如 RB,可能有资格作为 BST 另一种树类型。)
  • (或者更确切地说,RB树是一个 BST等)
  • 你能不能通过下面的链接……我问的问题几乎和那个差不多。但是在那个链接中,人们提出了一些其他的方法……除了 INORDER 遍历。我不知道为什么需要检查那些额外的条件。 stackoverflow.com/questions/499995/…
  • 您提供的链接中的所有答案几乎都是相同的“有序行走(不同语言)并检查左子树是否小于根和右子树大于根”。这正是@Andreas Brinck 在下面的回答中所说的。

标签: algorithm data-structures


【解决方案1】:

是的,如果树的中序遍历为您提供了一个严格单调的值列表,足以确定该树是 BST。

【讨论】:

  • 你能举一个'单调值列表'的例子吗? :(
【解决方案2】:

根据二叉搜索树的定义,如果二叉树的每个节点都满足以下条件,那么它就是一棵二叉搜索树:

  • 节点的左子树应该只包含键小于节点键的节点
  • 节点的右子树应该只包含键大于节点键的节点
  • 左右子树也必须是二叉搜索树。

如果中序遍历是升序,则以上条件均成立。

【讨论】:

    【解决方案3】:

    实际上 - 仅进行顺序遍历是不够的 - 您还需要验证每个节点的值是否遵循树的规则。在 BST 的情况下,左子值小于节点值,右子值大于节点值。这是Java中的递归示例。

    private static boolean isBST(Node current, Comparable more, Comparable less) {
        if (current == null) 
            return true;
    
        if (less != null && current.value.compareTo(less) > 0)
            return false;
    
        if (more != null && current.value.compareTo(more) < 0) 
            return false;
    
        return isBST(current.left, more, current.value) && 
                isBST(current.right, current.value, less);
    }
    
    public static boolean isBST(BinarySearchTree tree) {
        return isBST(tree.getRoot(), null, null);
    }
    

    【讨论】:

      【解决方案4】:
      While doing In-Order traversal, we can keep track of previously visited node
      

      代码:

      bool isBST(struct node* root)
      {
          static struct node *prev = NULL;
      
          // traverse the tree in inorder fashion and keep track of prev node
          if (root)
          {
              if (!isBST(root->left))
                return false;
      
              // Allows only distinct valued nodes 
              if (prev != NULL && root->data <= prev->data)
                return false;
      
              prev = root;
      
              return isBST(root->right);
          }
      
          return true;
      }
      

      【讨论】:

        【解决方案5】:

        一个简单但优雅的Java递归解决方案:

        public static boolean isBST(TreeNode node, int leftData, int rightData)
        {
            if (node == null) return true;
        
            if (node.getData() > leftData || node.getData() <= rightData) return false;
        
            return (isBST(node.left, node.getData(), rightData)
                   && isBST(node.right, leftData,  node.getData()));
        
        }
        

        对该函数的初始调用可能是这样的:

        if (isBST(root, Integer.MAX_VALUE, Integer.MIN_VALUE))
            System.out.println("This is a BST.");
        else
            System.out.println("This is NOT a BST!");
        

        本质上,我们不断创建一个有效范围(从 [MIN_VALUE, MAX_VALUE] 开始),并在递归下降时不断缩小它以针对每个节点。

        来源:http://exceptional-code.blogspot.com/2011/08/binary-search-trees-primer.html

        【讨论】:

          猜你喜欢
          • 2017-09-28
          • 1970-01-01
          • 2021-10-01
          • 1970-01-01
          • 2012-06-05
          • 2011-03-24
          • 1970-01-01
          • 2010-10-04
          相关资源
          最近更新 更多