What is a valid Binary Search Tree
Tags: javascript, career, tutorial
Published: 2019-12-07 19:50:37

How actually a Binary Search Tree works
Hello everyone, this is Aditya. N, i am here to share with you regarding what is a valid BST.

<h4><b>Prerequisites</b></h4>
<li>Familiar with Basic idea of Data Structures</li>
<li>What actually trees are how it is different from Binary Tree</li>
<li>I hope you are familiar with Data Structures and Trees Concept. If not don’t worry take a look at this article you got an idea what are they.</li>

Link: https://medium.com/@nagamalliaditya3/what-are-data-structures-846c3cacaff5

<h5><b>Dive Into Topic</b></h5>

<ul>Assume that you have a array of numbers and need to find whether the Binary tree is a valid Binary Search Tree or not.</ul>
<ul>Binary Search Tree means: In the name itself we are knowing it is fast access for Search the value from the tree.</ul>
<ul>What are the rules need to be satisfied to become a valid Binary Search Tree.</ul>

<h3>How Binary Search Trees Work</h3>

<li>1. Every parent/ root node has at most two children.</li>
<li>2. Every node to the left of a parent/ root node is always less than the parent / root node.</li>
<li>3. Every node to the right of a parent node is always greater than the parent / root node.</li>

Let’s Take an example and find out whether the Binary tree is a valid Binary Search Tree or not.

<h4>Example 1 </h4>

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/67mzjlez26vrqx09xeni.png)


<ul>The Top Node is a Root / Parent Node ie 10 in the given set, The left Child Node is 8 and the right Child node is 15.</ul>

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/3ngdzxiqpaslmunr3wwz.png)


<b>Step 2:</b> Check first the root node had child or not. If child nodes are there noted down and find out whether they followed the Rules or not.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/onpn0vtwdcmp2v1g2s3r.png)


<b>Step 3:</b> The left child node is less than Root node and the Right Child node is greater than the root node. Condition satisfied. Now, check it had any Sub child's are there or not. If it had repeat the process.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0rt1ktjrj3cjb83vin9p.png)


<b>step 4:</b>No the given example is not a valid binary search tree. Because, for every immediate root node the right child node must be greater than the immediate root node. In this case, 6 omits the condition means fails. 6 is less than 8. So, its not a Valid Binary Search Tree.

<h2>Example 2:</h2>
Both, left and right sub trees must also be binary search trees.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/xv6k4oprgii21vwvesjx.png)



<h2>Example 3:</h2>
Let’s consider this example and find out is this a valid BST ?

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/us1amwf545zcpc8xda6j.png)

<ul>In the given example Left Child Node and Right Child Node with Sub Nodes satisfies the basic principle. Here , we need to Observe the 1 is Less than Root Node 5 and 6 is Greater than 5,
6 had two childs 4 and 7 , 4 is less than 6 and 7 is greater than 6. Every condition we mentioned is satisfied but is it a valid BST ?</ul>


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/92bez75oy3xx2fx1de9v.png)

<li>It is Invalid Because, remember every child node in the right side of parent node should be greater than parent node.
4 is less than Root Node 5 that’s why it is a invalid Binary Search Tree.</li>


<h2>How Valid Binary Search Tree Looks Like</h2> 
Here is an example

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9311to00hpii3ida7gz7.png)

<h3>Conclusion</h3>

<ul>We understood, how a valid binary search tree looks and how to evaluate without confusion or unnecessary ambiguity. I am always passionate to learn new things and share to everyone. If any mistakes are there feel free to comment.
</ul>

<ul>Sorry for the images i made it through online avaliable resources. If you know any animated or good resources to make gifs and good online resources comment below.
</ul>