How to prove a binary tree, is it really a binary search tree?

Given a simple binary tree, how can we prove that the tree is a binary search tree? When we cross a binary tree, how do we know if node is turned on, is it the left or right child of its parent? I came up with one solution in which I would pass some flag in a recursive function call that could track whether a node is the left or right child of its parent, and we also need one parent node pointer through which we can compare: -

if(flag == 'L' && node->value < node->parent->value)
   then continue recursion;
else{
   print "not a binary search tree"
   exit;
}

Similarly, if a condition is Rnecessary. Besides this, can you think of any other effective way?

Thanks in advance:)

+3
source share
4

:

currentNode.Left.max() < currentNode.Value currentNode.Left.isBinarySearchTree(). , .

Edit:

( max() isBinarySearchTree. , :

. .., , O (1) .

max() isInRange(m,M), , () (m,m+1,...,M).

isInRange(m,M) :

bool isInRange(m,M){
 if (m < currentNode.Value <= M){
  return (currentNode.Left.isInRange(m, currentNode.Value) && currentNode.Right.isInrange(currentNode.Value+1, M));
 }
 return false;
}

root.isInRange(globalmin, globalmax).

, , .

+5

, , .

( Lisp):

(defun binary-search-tree-p (node)
  (let ((last-value nil))
    (labels ((check (value)
               (if (or (null last-value)        ; first checked value
                       (>= value last-value))
                   (setf last-value value)
                   nil))
             (traverse (node)
               (if (null node)
                   t
                   (and (traverse (left node))        ; \
                        (check (value node))          ;  > in-order traversal
                        (traverse (right node))))))   ; /
      (traverse node))))
+5

/ ? ,

+3

, , . , :

(10
   (7
     nil
     (20 nil nil)
   )
   nil
)

(7) (10) (7 <= 10), (20) 7 (20 >= 7). BST ( ), 20 10.

To fix this, you can do a workaround by passing extra arguments that specify a valid interval for the subtree.

// The valid interval for the subtree root value is (lower_bound, upper_bound).
bool IsBST(const node_t* tree, int lower_bound, int upper_bound) {
  if (tree == NULL) return true;  // An empty subtree is OK.
  if (tree->value <= lower_bound) return false;  // Value in the root is too low.
  if (tree->value >= upper_bound) return false;  // Value in the root is too high.

  // Values at the left subtree should be strictly lower than tree->value
  // and be inside the root valid interval.
  if (!IsBST(tree->left, lower_bound, tree->value))
    return false;

  // Values at the left subtree should be strictly greater than tree->value
  // and be inside the root valid interval.
  if (!IsBST(tree->right, tree->value, upper_bound))
    return false;

  // Everything is OK, it is a valid BST.
  return true;  
}

Note that, while maintaining the original valid interval, the function will find that the value 20 is not valid at this position, since it is not inside (7, 10). The first call must be made at infinite intervals, for example:

IsBST(tree, INT_MIN, INT_MAX);

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1757663/


All Articles