【问题标题】:Why the solution for checking the validity of binary tree is not working?为什么检查二叉树有效性的解决方案不起作用?
【发布时间】:2016-02-21 20:08:50
【问题描述】:

我正在解决一个检查二叉树是否是有效二叉树的问题。这意味着某个节点的左节点的值较小,而右节点的值大于该节点的值。该程序使用递归方法执行检查并返回布尔值来确定树的有效性。我提供的代码如下,

class Node {

      Node left;
      Node right; 
       int data;

     public Node(int data_ ){

       this.data = data_;
   }
}

public class myTest{


private static List<Integer> lis = new ArrayList<Integer>();

public static List<Integer> inOrder(Node root){

    if (root != null){

        inOrder(root.left);

        lis.add(root.data);
        System.out.println( root.data );
        inOrder(root.right);
    }

    return lis;
}

public static boolean isBST( Node root, int min, int max  ){

    if (root ==  null )
        return true;

    return ( root.data >= min) 
        && ( root.data <= max ) 
        && isBST(root.left, min, root.data) 
        && isBST( root.right, root.data, max );
}

public static void main(String[] args){  

// form the bst
 Node root = new Node(5);
   root.left = new Node(3);
   root.right = new Node(7); 

   root.left.left = new Node(1);
   root.left.right = new Node(2);

   root.right.left = new Node(6);
   root.right.right = new Node(9);

   // get the max and min value from the tree
   int max = 0;
   int min  = 0;

   if (root != null ){

      List<Integer> li  = inOrder(root);
      min =  li.get(0);
      max = li.get( li.size() -1 );
   }


   // determine the validity 
   boolean bol_ = isBST(root, min, max );

   if ( bol_)
     System.out.println("valid BST ");

    else 
        System.out.println("Not valid BST ");
  }

  }

提供的测试树是有效的,但程序告诉它不是。我没有看到的内部问题是什么?此外,改进代码的边缘情况是什么?说,我是否需要检查树内的所有值是否都是不同的整数以及如何做到这一点?

【问题讨论】:

  • 您的树无效,即 root.left.right.data 小于 root.left.data (2
  • 糟糕,我现在看到了,感谢您的提及。您是否看到任何极端情况或任何改进代码的建议?
  • 嗯,我没有仔细阅读代码,但我会尝试分享一些观察结果:1)我不会将 inOrder() 之类的静态方法与静态变量(例如lis,我要么将列表作为参数传递,要么同时创建两个实例变量。 2)有人可能会争辩说node.left.data == node.data 可能不代表一棵有效的树,因为这可能表明一个节点及其左(或右)子节点是相同的。至少你应该检查一下。
  • 我同意您所说的传递 List 参数。顺便说一句,我在哪里做这个 node.left.data == node.data ?
  • 您没有明确地这样做,但您正在检查root.data &gt;= min 等,其中min 可能隐含地是父节点的值。我宁愿只检查更低和更大,并且在初次调用时我会通过 min - 1max + 1

标签: java recursion binary-search-tree


【解决方案1】:

我稍微改变了方法,现在它如下,

public static boolean isBST( Node root, int min, int max  ){

    if (root ==  null )
        return true;

    return ( root.data > min) 
        && ( root.data < max ) 
        && isBST(root.left, min, root.data) 
        && isBST( root.right, root.data, max );
}

虽然调用如下,

boolean bol_ = isBST(root, min - 1 , max +1 );

【讨论】:

    【解决方案2】:

    因为这个:

    root.left = 新节点(3);
    root.left.right = 新节点(2);

    -> 在右侧必须有高于 root 的值 - 像这样

    root.left = 新节点(2);
    root.left.right = 新节点(3);

    【讨论】:

    • 感谢您的回答。你是对的,@Thomas 早先指出了我不接受的原因
    【解决方案3】:

    正如@Thomas 指出的那样,您的树不是有效的 BST。

    不过,我想给你一些建议。当您尝试评估一棵树是否是二叉树或不是递归时,有很多边缘情况。一种更简单的解决方案是按顺序遍历树,将元素存储在 ArrayList 中,然后检查列表是否已排序。这种实现更容易/更简单,并且运行时间复杂度相同,但空间复杂度更大:两者都是 O(n)。

    【讨论】:

    • 我认为您指出了一个很好的解决方案。正如您在我的代码中看到的那样,我还制作了一个包含 inOrderTraverse 值的 ArrayList,我可以为此目的使用该方法。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 2012-05-16
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2015-09-12
    • 1970-01-01
    相关资源
    最近更新 更多