check for bst
C++
// Java implementation to check if given Binary tree
// is a BST or not
/* Class containing left and right child of current
node and key value*/
class Node
{
int data;
Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
public class BinaryTree
{
// Root of the Binary Tree
Node root;
// To keep tract of previous node in Inorder Traversal
Node prev;
boolean isBST() {
prev = null;
return isBST(root);
}
/* Returns true if given search tree is binary
search tree (efficient version) */
boolean isBST(Node node)
{
// traverse the tree in inorder fashion and
// keep a track of previous node
if (node != null)
{
if (!isBST(node.left))
return false;
// allows only distinct values node
if (prev != null && node.data <= prev.data )
return false;
prev = node;
return isBST(node.right);
}
return true;
}
/* Driver program to test above functions */
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(4);
tree.root.left = new Node(2);
tree.root.right = new Node(5);
tree.root.left.left = new Node(1);
tree.root.left.right = new Node(3);
if (tree.isBST())
System.out.println("IS BST");
else
System.out.println("Not a BST");
}
}
// C++ program to check if a given tree is BST.
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, pointer to
left child and a pointer to right child */
struct Node
{
int data;
struct Node* left, *right;
};
// Returns true if given tree is BST.
bool isBST(Node* root, Node* l=NULL, Node* r=NULL)
{
// Base condition
if (root == NULL)
return true;
// if left node exist then check it has
// correct data or not i.e. left node's data
// should be less than root's data
if (l != NULL and root->data <= l->data)
return false;
// if right node exist then check it has
// correct data or not i.e. right node's data
// should be greater than root's data
if (r != NULL and root->data >= r->data)
return false;
// check recursively for every node.
return isBST(root->left, l, root) and
isBST(root->right, root, r);
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
/* Driver program to test above functions*/
int main()
{
struct Node *root = newNode(3);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(4);
if (isBST(root,NULL,NULL))
cout << "Is BST";
else
cout << "Not a BST";
return 0;
}
Also in C++:
- Title
- sfml base program
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- pair c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- find vector in c++
- Category
- C++
- Title
- traverse map c++
- Category
- C++
- Title
- preorder traversal
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- getline not working c++
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- c++ raw string
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- compare string c++
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- double pointers C++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- c++ excel blank cells
- Category
- C++
- Title
- c++ while true loop
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- c++ char print width
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- how to swap string characters in c++
- Category
- C++
- Title
- capitalize first letter c++
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- c++ write string
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- c++ not greater than
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++