
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
A program to check if a binary tree is BST or not in C ?
A binary tree is a tree data structure in which there are two child nodes for each node. The child nodes being two are referred as, left and right child.
A BST is a tree structure in which left subtree contains nodes with values lesser than root and right subtree contains nodes with values greater that root.
Here, we will check if a binary tree is a BST or not −
To check for this we have to check for the BST condition on the binary tree. For a root node check for left child should be less that root, right child should be greater that root for all nodes of the tree in which the child's exist.
PROGRAM TO CHECK IF A BINARY TREE IS A BST
#include<bits/stdc++.h> #include<iostream> using namespace std; class node { public: int data; node* left; node* right; node(int data) { this->data = data; this->left = NULL; this->right = NULL; } }; 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==NULL) return 1; if (node->data < min || node->data > max) return 0; return isBSTUtil(node->left, min, node->data-1) && isBSTUtil(node->right, node->data+1, max); } int main() { node *root = new node(8); root->left = new node(3); root->right = new node(10); root->left->left = new node(1); root->left->right = new node(6); if(isBST(root)) cout<<"The given tree is a BST"; else cout<<"The given tree is Not a BST"; return 0; }
Output
The given tree is a BST
Code Explained
The above code check for a BST. The main method, creates a tree and call the isBST() method. This method checks if the left and right child follow the BST rule also that the subtrees formed are BST’s too by using the isBSTuntil() method.
- Related Articles
- C++ Program to Check if a Binary Tree is a BST
- Program to check whether a binary tree is BST or not in Python
- Check if a Binary Tree (not BST) has duplicate value in C++
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Check if a binary tree is sorted levelwise or not in C++
- Check if a binary tree is sorted level-wise or not in C++
- Program to check whether a binary tree is complete or not in Python
- Program to check if a matrix is Binary matrix or not in C++
- Check if a Tree is Isomorphic or not in C++
- C++ Program to Check if a Directed Graph is a Tree or Not Using DFS
- Program to find out if a BST is present in a given binary tree in Python
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- Check if a binary tree is subtree of another binary tree in C++
- Largest BST in a Binary Tree in C++
