简体   繁体   English

为什么二叉搜索树有效性的解决方案不起作用?

[英]Why the solution for binary search tree validity is not working?

I wrote a solution for checking the validity of the binary search tree using LinkedList and it's proving wrong information about the validity. 我写了一个使用LinkedList检查二进制搜索树有效性的解决方案,它证明了有关有效性的错误信息。 I checked with a valid BST and it returns that the tree is not valid. 我检查了有效的BST,它返回树无效。 The code is as following, 代码如下

public static boolean isValidBST_( Node root ){

  boolean bol = false;
  LinkedList<Node> queue = new LinkedList<Node>();

  queue.add(root);

  while( !queue.isEmpty() ){

    Node cur = queue.poll();

    if ( ( cur.left != null && cur.data > cur.left.data ) || (cur.right != null && cur.data < cur.right.data  ) ){

      return bol;
    }

    if ( cur.left != null ){

       queue.offer(cur.left);
    }

    if ( cur.right != null ){

      queue.offer(cur.right);
    }

  } // WHILE


  if (queue.isEmpty() ){

    bol = true;
  }

  return bol; 

} }

How can I improve the code ? 如何改善代码?

I make the call from main as following, 我从main拨打电话如下

public static void main (String[] args ){


 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(4);

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

 System.out.println("Does the inserted BST is valid ?  Answer:  " + isValidBST_(root));

}

You seem to have your checks the wrong way around: 您似乎以错误的方式进行检查:

if ( ( cur.left != null && cur.data > cur.left.data ) || (cur.right != null && cur.data < cur.right.data  ) )

will pass (and return false ) when the node's data is greater than the left nodes data or smaller than the right node's data. 当节点的数据大于左节点的数据或小于右节点的数据时,将通过(并返回false )。 You want the opposite. 您要相反。 Try reversing the inequalities. 尝试扭转不平等现象。

You asked 'how to improve the code'. 您问“如何改进代码”。 This seems to be crying out for recursion: 这似乎在呼唤递归:

boolean isValid(Node node) {
    if (node.left != null && (cur.data < node.left.data || !isValid(node.left)))
        return false;
    else if (node.right != null && (cur.data > node.right.data || !isValid(node.right)))
        return false;
    else
        return true;
}

If you want to stick with an iterative approach you can simplify by dropping your bol variable and removing the final check: 如果要坚持使用迭代方法,则可以通过删除bol变量并删除最终检查来简化操作:

public boolean isValid(Node root) {
    LinkedList<Node> nodesToCheck = new LinkedList<>();
    nodesToCheck.offer(root);
    while (!nodesToCheck.isEmpty()) {
        Node current = nodesToCheck.poll();
        if (current.left != null) {
            if (current.data < current.left.data)
                return false;
            nodesToCheck.offer(current.left);
        }
        if (current.right != null) {
            if (current.data > current.right.data)
                return false;
            nodesToCheck.offer(current.right);
        }
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM