TT Exp-8
TT Exp-8
EXPERIMENT - 8
Aim: Write a program in C++ using recursion function to check if the binary tree is the binary search tree.
Theory: The program is designed to determine whether a given binary tree is a binary search tree
(BST). A binary search tree is a binary tree in which the value of each node in the left subtree is less
than the node's value, and the value of each node in the right subtree is greater than the node's value.
To achieve this, the program employs a recursive approach. The `isBSTUtil` function is a recursive
function that checks each node in the binary tree to ensure it satisfies the BST property. It traverses
the tree recursively, updating the valid range of values for each node based on its position in the tree.
The function returns true if the tree is empty or if all nodes in the tree satisfy the BST property;
otherwise, it returns false. The main function interacts with the user to create a binary tree based on
their input values and then calls the `isBST` function to determine whether the entered binary tree is
a binary search tree. This program demonstrates the use of recursion in checking the properties of a
binary tree, specifically whether it meets the criteria of being a binary search tree. \
Algorithm:
1. Define the TreeNode structure: Define a structure to represent each node of the binary tree, including its
value, left child, and right child.
2. Define the isBSTUtil Function: Define a recursive function `isBSTUtil` that takes a TreeNode pointer, along with
minimum and maximum integer values representing the valid range for the node's value. This function
recursively checks whether the given binary tree satisfies the properties of a binary search tree (BST). It
returns true if the tree is empty or if all nodes in the tree satisfy the BST property; otherwise, it returns false.
Base Case:
Recursive Steps:
- Recursively call `isBSTUtil` for the left subtree, updating the maximum value to be one less than the
current node's value.
- Recursively call `isBSTUtil` for the right subtree, updating the minimum value to be one more than the
current node's value.
3. Define the isBST Function: Define a function `isBST` that serves as a wrapper function for `isBSTUtil`. It takes a
TreeNode pointer representing the root of the binary tree and calls `isBSTUtil` with initial minimum and
maximum values as negative infinity and positive infinity, respectively.
4. Define the createBinaryTree Function (Optional): Optionally, define a function to allow user input for creating
a binary tree. This function prompts the user to enter values for each node and constructs the binary tree
accordingly.
- The function takes input for the root value and then iteratively prompts for parent-child node pairs
until the user decides not to enter more nodes.
- Call the `createBinaryTree` function to create the binary tree (optional if user input is required).
- Call the `isBST` function to determine whether the entered binary tree is a binary search tree.
Code Implementation:
int val;
TreeNode *left;
TreeNode *right;
(node == NULL)
return true;
return false;
INT_MIN, INT_MAX);
TreeNode* createBinaryTree() {
int val;
parentVal, childVal;
cout << "Enter parent value: ";
parent = root;
if (parent->left == NULL)
parent->left = child;
else
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
parent->right = child;
cout << "Do you want to enter more nodes? (y/n): ";
root;
int main() {
else
cout << "The binary tree is not a binary search tree." << endl; return
0;
Output:
Learning Outcomes:
2. Learnt about using recursion for identifying whether the tree is a binary search tree.