How To Check If Title Is Clean Indiana
A binary search tree (BST) is a node based binary tree data construction which has the following properties.
- The left subtree of a node contains merely nodes with keys less than the node'south key.
- The right subtree of a node contains but nodes with keys greater than the node's cardinal.
- Both the left and right subtrees must also be binary search copse.
From the above backdrop it naturally follows that:
- Each node (detail in the tree) has a distinct fundamental.
METHOD 1 (Simple but Incorrect)
Following is a simple programme. For each node, cheque if the left node of it is smaller than the node and right node of it is greater than the node.
C++
int isBST( struct node* node)
{
if (node == Nada)
render one;
if (node->left != Zip && node->left->data > node->information)
return 0;
if (node->right != Nada && node->correct->data < node->data)
return 0;
if (!isBST(node->left) || !isBST(node->right))
return 0;
return i;
}
C
int isBST( struct node* node)
{
if (node == Naught)
render 1;
if (node->left != NULL && node->left->information > node->information)
return 0;
if (node->right != Zip && node->right->data < node->data)
render 0;
if (!isBST(node->left) || !isBST(node->right))
return 0;
return one;
}
Java
boolean isBST(Node node)
{
if (node == nil )
return true ;
if (node.left != zippo && node.left.data > node.data)
return simulated ;
if (node.correct != null && node.right.data < node.data)
return imitation ;
if (!isBST(node.left) || !isBST(node.right))
return false ;
return truthful ;
}
Python3
def isBST(node):
if (node = = None ):
return 1
if (node.left ! = None and node.left.data > node.data):
return 0
if (node.correct ! = None and node.right.information < node.data):
return 0
if (!isBST(node.left) or !isBST(node.correct)):
render 0
return 1
C#
bool isBST(Node node)
{
if (node == null )
return true ;
if (node.left != null && node.left.data > node.data)
return simulated ;
if (node.right != null && node.correct.information < node.data)
return false ;
if (!isBST(node.left) || !isBST(node.right))
return false ;
render truthful ;
}
Javascript
<script>
function isBST(node)
{
if (node == zero )
return true ;
if (node.left != null && node.left.information > node.information)
return fake ;
if (node.correct != null && node.right.information < node.data)
return false ;
if (!isBST(node.left) || !isBST(node.correct))
return false ;
return truthful ;
}
</script>
This approach is wrong as this will return true for below binary tree (and below tree is not a BST because four is in left subtree of 3)
METHOD 2 (Correct only not efficient)
For each node, bank check if max value in left subtree is smaller than the node and min value in correct subtree greater than the node.
C++
int isBST( struct node* node)
{
if (node == Cipher)
return i;
if (node->left != NULL && maxValue(node->left) >= node->data)
render 0;
if (node->right != Nothing && minValue(node->right) <= node->data)
return 0;
if (!isBST(node->left) || !isBST(node->right))
return 0;
return one;
}
C
int isBST( struct node* node)
{
if (node == Zilch)
return one;
if (node->left!=NULL && maxValue(node->left) > node->data)
render 0;
if (node->right!=NULL && minValue(node->correct) < node->data)
return 0;
if (!isBST(node->left) || !isBST(node->right))
return 0;
return one;
}
Java
int isBST(Node node)
{
if (node == null )
render 1 ;
if (node.left != zero && maxValue(node.left) >= node.data)
return 0 ;
if (node.right != null && minValue(node.correct) <= node.information)
return 0 ;
if (!isBST(node.left) || !isBST(node.right))
render 0 ;
return i ;
}
Python3
def isBST(node):
if (node = = None ):
return 1
if (node.left ! = None and maxValue(node.left) > = node.data):
return 0
if (node.right ! = None and minValue(node.right) < = node.information):
return 0
if (!isBST(node.left) or !isBST(node.right)):
return 0
return i
C#
bool isBST(Node node)
{
if (node == null )
return true ;
if (node.left != nix && maxValue(node.left) >= node.data)
return false ;
if (node.right != null && minValue(node.right) <= node.information)
return false ;
if (!isBST(node.left) || !isBST(node.right))
return imitation ;
return true ;
}
Javascript
<script>
function isBST(node)
{
if (node == null )
return true ;
if (node.left != null && maxValue(node.left) >= node.data)
return faux ;
if (node.correct != zippo && minValue(node.right) <= node.information)
render false ;
if (!isBST(node.left) || !isBST(node.correct))
return faux ;
return truthful ;
}
</script>
It is causeless that you have helper functions minValue() and maxValue() that return the min or max int value from a non-empty tree
METHOD 3 (Correct and Efficient):
Method 2 above runs slowly since information technology traverses over some parts of the tree many times. A improve solution looks at each node only once. The pull a fast one on is to write a utility helper office isBSTUtil(struct node* node, int min, int max) that traverses downwardly the tree keeping track of the narrowing min and max immune values as information technology goes, looking at each node only once. The initial values for min and max should be INT_MIN and INT_MAX — they narrow from at that place.
Notation: This method is not applicable if in that location are indistinguishable elements with value INT_MIN or INT_MAX.
Beneath is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node* left;
node* right;
node( int information)
{
this ->data = information;
this ->left = NULL;
this ->right = Nil;
}
};
int isBSTUtil(node* node, int min, int max);
int isBST(node* node)
{
return (isBSTUtil(node, INT_MIN, INT_MAX));
}
int isBSTUtil(node* node, int min, int max)
{
if (node==Zero)
return 1;
if (node->data < min || node->data > max)
render 0;
return
isBSTUtil(node->left, min, node->data-1) &&
isBSTUtil(node->right, node->data+i, max);
}
int main()
{
node *root = new node(4);
root->left = new node(two);
root->right = new node(5);
root->left->left = new node(one);
root->left->right = new node(3);
if (isBST(root))
cout<< "Is BST" ;
else
cout<< "Non a BST" ;
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
int isBSTUtil( struct node* node, int min, int max);
int isBST( struct node* node)
{
return (isBSTUtil(node, INT_MIN, INT_MAX));
}
int isBSTUtil( struct node* node, int min, int max)
{
if (node==Naught)
return i;
if (node->data < min || node->data > max)
return 0;
return
isBSTUtil(node->left, min, node->information-1) &&
isBSTUtil(node->correct, node->data+one, max);
}
struct node* newNode( int data)
{
struct node* node = ( struct node*)
malloc ( sizeof ( struct node));
node->information = data;
node->left = NULL;
node->correct = NULL;
return (node);
}
int main()
{
struct node *root = newNode(4);
root->left = newNode(2);
root->correct = newNode(5);
root->left->left = newNode(ane);
root->left->right = newNode(3);
if (isBST(root))
printf ( "Is BST" );
else
printf ( "Not a BST" );
getchar ();
return 0;
}
Java
course Node
{
int data;
Node left, right;
public Node( int item)
{
information = particular;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
boolean isBST() {
return isBSTUtil(root, Integer.MIN_VALUE,
Integer.MAX_VALUE);
}
boolean isBSTUtil(Node node, int min, int max)
{
if (node == null )
return truthful ;
if (node.data < min || node.data > max)
return imitation ;
render (isBSTUtil(node.left, min, node.data- 1 ) &&
isBSTUtil(node.right, node.data+ ane , max));
}
public static void main(Cord args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 4 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( v );
tree.root.left.left = new Node( 1 );
tree.root.left.right = new Node( three );
if (tree.isBST())
System.out.println( "IS BST" );
else
System.out.println( "Not a BST" );
}
}
Python3
INT_MAX = 4294967296
INT_MIN = - 4294967296
class Node:
def __init__( self , information):
cocky .information = data
self .left = None
self .right = None
def isBST(node):
return (isBSTUtil(node, INT_MIN, INT_MAX))
def isBSTUtil(node, mini, maxi):
if node is None :
return True
if node.data < mini or node.data > maxi:
return False
render (isBSTUtil(node.left, mini, node.data - 1 ) and
isBSTUtil(node.right, node.information + 1 , maxi))
root = Node( 4 )
root.left = Node( two )
root.correct = Node( 5 )
root.left.left = Node( 1 )
root.left.right = Node( three )
if (isBST(root)):
print ( "Is BST" )
else :
impress ( "Not a BST" )
C#
using Arrangement;
public form Node
{
public int data;
public Node left, right;
public Node( int item)
{
information = item;
left = right = nada ;
}
}
public class BinaryTree
{
public Node root;
public virtual bool BST
{
get
{
return isBSTUtil(root, int .MinValue, int .MaxValue);
}
}
public virtual bool isBSTUtil(Node node, int min, int max)
{
if (node == null )
{
render true ;
}
if (node.data < min || node.data > max)
{
return false ;
}
render (isBSTUtil(node.left, min, node.information - 1) && isBSTUtil(node.right, node.data + 1, max));
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(4);
tree.root.left = new Node(two);
tree.root.right = new Node(five);
tree.root.left.left = new Node(1);
tree.root.left.right = new Node(iii);
if (tree.BST)
{
Console.WriteLine( "IS BST" );
}
else
{
Console.WriteLine( "Non a BST" );
}
}
}
Javascript
<script>
class Node
{
constructor(item)
{
this .data=item;
this .left= this .right= null ;
}
}
permit root;
function isBST()
{
return isBSTUtil(root, Number.MIN_VALUE,
Number.MAX_VALUE);
}
function isBSTUtil(node,min,max)
{
if (node == cipher )
render true ;
if (node.data < min || node.data > max)
return imitation ;
return (isBSTUtil(node.left, min, node.data-1) &&
isBSTUtil(node.right, node.information+1, max));
}
root = new Node(4);
root.left = new Node(two);
root.right = new Node(5);
root.left.left = new Node(one);
root.left.right = new Node(three);
if (isBST())
certificate.write( "IS BST<br>" );
else
document.write( "Not a BST<br>" );
</script>
Output:
IS BST
Fourth dimension Complexity: O(n)
Auxiliary Space: O(i) if Function Call Stack size is not considered, otherwise O(n)
Simplified Method 3
We tin simplify method 2 using NULL pointers instead of INT_MIN and INT_MAX values.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left, *correct;
};
bool isBST(Node* root, Node* 50=NULL, Node* r=Zippo)
{
if (root == NULL)
return truthful ;
if (l != Goose egg and root->data <= l->data)
return imitation ;
if (r != Nil and root->data >= r->data)
render false ;
return isBST(root->left, l, root) and
isBST(root->right, root, r);
}
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = information;
node->left = node->right = Nada;
return (node);
}
int main()
{
struct Node *root = newNode(three);
root->left = newNode(two);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(4);
if (isBST(root,Nada,NULL))
cout << "Is BST" ;
else
cout << "Non a BST" ;
return 0;
}
Java
grade Sol
{
static class Node
{
int information;
Node left, right;
};
static boolean isBST(Node root, Node 50, Node r)
{
if (root == naught )
return true ;
if (fifty != zippo && root.data <= l.data)
return false ;
if (r != null && root.data >= r.information)
return false ;
return isBST(root.left, l, root) &&
isBST(root.right, root, r);
}
static Node newNode( int data)
{
Node node = new Node();
node.information = data;
node.left = node.right = zilch ;
return (node);
}
public static void main(String args[])
{
Node root = newNode( 3 );
root.left = newNode( two );
root.right = newNode( 5 );
root.left.left = newNode( 1 );
root.left.right = newNode( 4 );
if (isBST(root, null , null ))
System.out.print( "Is BST" );
else
Arrangement.out.print( "Not a BST" );
}
}
Python3
class newNode:
def __init__( self , key):
cocky .data = key
self .left = None
cocky .right = None
def isBST(root, 50 = None , r = None ):
if (root = = None ) :
return True
if (l ! = None and root.data < = l.data) :
return Faux
if (r ! = None and root.information > = r.data) :
render Imitation
return isBST(root.left, l, root) and \
isBST(root.right, root, r)
if __name__ = = '__main__' :
root = newNode( iii )
root.left = newNode( two )
root.correct = newNode( 5 )
root.right.left = newNode( 1 )
root.right.right = newNode( four )
if (isBST(root, None , None )):
print ( "Is BST" )
else :
print ( "Not a BST" )
C#
using Organization;
form GFG
{
public class Node
{
public int information;
public Node left, right;
};
static Boolean isBST(Node root, Node l, Node r)
{
if (root == naught )
return true ;
if (50 != null && root.information <= fifty.data)
render false ;
if (r != null && root.data >= r.data)
return imitation ;
return isBST(root.left, 50, root) &&
isBST(root.right, root, r);
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
public static void Main(String []args)
{
Node root = newNode(3);
root.left = newNode(2);
root.correct = newNode(5);
root.left.left = newNode(1);
root.left.right = newNode(iv);
if (isBST(root, cypher , null ))
Console.Write( "Is BST" );
else
Console.Write( "Non a BST" );
}
}
Javascript
<script>
form Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = information;
}
}
function isBST(root, fifty, r)
{
if (root == aught )
return true ;
if (fifty != null && root.data <= l.data)
return simulated ;
if (r != null && root.data >= r.data)
return fake ;
return isBST(root.left, l, root) &&
isBST(root.correct, root, r);
}
function newNode(data)
{
permit node = new Node(data);
render (node);
}
let root = newNode(3);
root.left = newNode(2);
root.correct = newNode(5);
root.left.left = newNode(ane);
root.left.right = newNode(four);
if (isBST(root, null , null ))
document.write( "Is BST" );
else
document.write( "Not a BST" );
</script>
Output:
Not a BST
Thanks to Abhinesh Garhwal for suggesting in a higher place solution.
METHOD four(Using In-Order Traversal)
Thanks to LJW489 for suggesting this method.
i) Practice In-Order Traversal of the given tree and store the consequence in a temp array.
ii) This method assumes that there are no duplicate values in the tree
iii) Check if the temp array is sorted in ascending order, if it is, so the tree is BST.
Time Complexity: O(northward)
We can avoid the employ of a Auxiliary Array. While doing In-Order traversal, we can keep runway of previously visited node. If the value of the currently visited node is less than the previous value, and so tree is non BST. Cheers to ygos for this infinite optimization.
C++
bool isBST(node* root)
{
static node *prev = NULL;
if (root)
{
if (!isBST(root->left))
return false ;
if (prev != Nada &&
root->data <= prev->data)
return false ;
prev = root;
return isBST(root->correct);
}
return true ;
}
C
bool isBST( struct node* root)
{
static struct node *prev = Cipher;
if (root)
{
if (!isBST(root->left))
return fake ;
if (prev != NULL && root->data <= prev->data)
return false ;
prev = root;
return isBST(root->right);
}
return true ;
}
Coffee
course Node
{
int data;
Node left, right;
public Node( int detail)
{
data = item;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
Node prev;
boolean isBST() {
prev = naught ;
return isBST(root);
}
boolean isBST(Node node)
{
if (node != goose egg )
{
if (!isBST(node.left))
return false ;
if (prev != nothing && node.information <= prev.data )
render faux ;
prev = node;
return isBST(node.right);
}
return true ;
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( iv );
tree.root.left = new Node( 2 );
tree.root.right = new Node( v );
tree.root.left.left = new Node( 1 );
tree.root.left.right = new Node( 3 );
if (tree.isBST())
Organization.out.println( "IS BST" );
else
System.out.println( "Non a BST" );
}
}
Python3
class Node:
def __init__( cocky , val):
self .information = val
self .left = None
self .right = None
prev = None
def isbst(root):
global prev
prev = None
return isbst_rec(root)
def isbst_rec(root):
global prev
if root is None :
return Truthful
if isbst_rec(root.left) is Faux :
render False
if prev is non None and prev.data > root.information:
return False
prev = root
return isbst_rec(root.right)
root = Node( 4 )
root.left = Node( 2 )
root.correct = Node( 5 )
root.left.left = Node( 1 )
root.left.right = Node( 3 )
if isbst(root):
print ( "is BST" )
else :
print ( "not a BST" )
C#
using Arrangement;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = particular;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
Node prev;
Boolean isBST()
{
prev = zip ;
return isBST(root);
}
Boolean isBST(Node node)
{
if (node != naught )
{
if (!isBST(node.left))
return false ;
if (prev != zip &&
node.data <= prev.data )
render false ;
prev = node;
return isBST(node.right);
}
return truthful ;
}
public static void Main(String []args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(4);
tree.root.left = new Node(two);
tree.root.right = new Node(five);
tree.root.left.left = new Node(1);
tree.root.left.correct = new Node(three);
if (tree.isBST())
Console.WriteLine( "IS BST" );
else
Console.WriteLine( "Not a BST" );
}
}
Javascript
<script>
class Node
{
constructor(detail)
{
this .data = detail;
this .left = this .correct= goose egg ;
}
}
permit root;
permit prev;
function isBST()
{
prev = zip ;
return _isBST(root);
}
office _isBST(node)
{
if (node != cypher )
{
if (!_isBST(node.left))
return fake ;
if (prev != null && node.data <= prev.information )
render false ;
prev = node;
render _isBST(node.correct);
}
return true ;
}
root = new Node(4);
root.left = new Node(2);
root.right = new Node(5);
root.left.left = new Node(1);
root.left.right = new Node(3);
if (isBST())
certificate.write( "IS BST" );
else
certificate.write( "Not a BST" );
</script>
The utilize of a static variable can besides be avoided by using a reference to the prev node as a parameter.
C++
#include <$.25/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left, *right;
Node( int data)
{
this ->data = data;
left = correct = Nil;
}
};
bool isBSTUtil( struct Node* root, Node *&prev)
{
if (root)
{
if (!isBSTUtil(root->left, prev))
return false ;
if (prev != NULL && root->information <= prev->data)
return false ;
prev = root;
return isBSTUtil(root->right, prev);
}
return true ;
}
bool isBST(Node *root)
{
Node *prev = Zippo;
return isBSTUtil(root, prev);
}
int main()
{
struct Node *root = new Node(3);
root->left = new Node(two);
root->right = new Node(five);
root->left->left = new Node(1);
root->left->right = new Node(iv);
if (isBST(root))
cout << "Is BST" ;
else
cout << "Non a BST" ;
return 0;
}
Java
import coffee.io.*;
form GFG {
public static class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = information;
left = right = nothing ;
}
};
static Node prev;
static Boolean isBSTUtil(Node root)
{
if (root != null )
{
if (!isBSTUtil(root.left))
return false ;
if (prev != null &&
root.information <= prev.data)
render faux ;
prev = root;
return isBSTUtil(root.right);
}
return truthful ;
}
static Boolean isBST(Node root)
{
return isBSTUtil(root);
}
public static void main (String[] args)
{
Node root = new Node( 3 );
root.left = new Node( 2 );
root.correct = new Node( 5 );
root.left.left = new Node( ane );
root.left.right = new Node( 4 );
if (isBST(root))
System.out.println( "Is BST" );
else
System.out.println( "Not a BST" );
}
}
Python3
import math
grade Node:
def __init__( self , data):
self .information = information
self .left = None
self .right = None
def isBSTUtil(root, prev):
if (root ! = None ):
if (isBSTUtil(root.left, prev) = = True ):
render False
if (prev ! = None and
root.data < = prev.data):
return False
prev = root
render isBSTUtil(root.correct, prev)
return Truthful
def isBST(root):
prev = None
return isBSTUtil(root, prev)
if __name__ = = '__main__' :
root = Node( three )
root.left = Node( 2 )
root.correct = Node( 5 )
root.right.left = Node( 1 )
root.correct.correct = Node( 4 )
if (isBST(root) = = None ):
print ( "Is BST" )
else :
print ( "Non a BST" )
C#
using Organization;
public class GFG
{
public class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = information;
left = right = null ;
}
};
static Node prev;
static Boolean isBSTUtil(Node root)
{
if (root != null )
{
if (!isBSTUtil(root.left))
return false ;
if (prev != null &&
root.data <= prev.information)
return false ;
prev = root;
return isBSTUtil(root.right);
}
render true ;
}
static Boolean isBST(Node root)
{
return isBSTUtil(root);
}
public static void Main(String[] args)
{
Node root = new Node(three);
root.left = new Node(2);
root.right = new Node(five);
root.left.left = new Node(1);
root.left.right = new Node(4);
if (isBST(root))
Console.WriteLine( "Is BST" );
else
Console.WriteLine( "Non a BST" );
}
}
Javascript
<script>
class Node
{
constructor(data)
{
this .left = null ;
this .correct = null ;
this .data = data;
}
}
let prev;
function isBSTUtil(root)
{
if (root != nada )
{
if (!isBSTUtil(root.left))
return false ;
if (prev != null && root.data <= prev.data)
return false ;
prev = root;
return isBSTUtil(root.right);
}
return true ;
}
role isBST(root)
{
return isBSTUtil(root);
}
allow root = new Node(3);
root.left = new Node(2);
root.right = new Node(5);
root.left.left = new Node(1);
root.left.right = new Node(four);
if (isBST(root))
document.write( "Is BST" );
else
document.write( "Non a BST" );
</script>
Output:
Not a BST
Sources:
http://en.wikipedia.org/wiki/Binary_search_tree
http://cslibrary.stanford.edu/110/BinaryTrees.html
Please write comments if you find any bug in the to a higher place programs/algorithms or other ways to solve the same problem.
Source: https://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
Posted by: overcashsheastes.blogspot.com

0 Response to "How To Check If Title Is Clean Indiana"
Post a Comment