Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree satisfying BST property But I think it is not BST?

Tags:

c

algorithm

tree

Today i am doing a problem on Binary Trees during which I found a structure of BSTree which is satisfying property : "Every node has smaller value on its left child and greater value on its right child". But it is not a BST ( in my opinion ) because root has smaller value than one of its grand child.Please Explain me all this .

Binary tree :

      7
     /  \
    4    10
   / \
  2   8

Tell me is this BST or Not ? Explain .

like image 443
Ajay Yadav Avatar asked Sep 07 '12 04:09

Ajay Yadav


Video Answer


1 Answers

A more correct definition of a BST can be found here:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

So, although your tree satisfies the specific case of every node having a smaller value to its left and a larger value to its right, it does not satisfy the more general case involving the left and right subtrees, and is therefore not a BST.

like image 69
tojrobinson Avatar answered Oct 10 '22 18:10

tojrobinson