C Program to Check if a Tree is Binary Search Tree

This C Program Checks whether a Tree is a Binary Search Tree.

Here is source code of the C program to Check whether a Tree is a Binary Search Tree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Check whether a Tree is a Binary Search Tree 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int data;
  10.     struct node* left;
  11.     struct node* right;
  12. };
  13.  
  14. static struct node *prev = NULL; 
  15.  
  16. /*Function to check whether the tree is BST or not*/
  17. int is_bst(struct node* root)
  18. {
  19.     if (root)
  20.     {
  21.         if (!is_bst(root->left)) //moves towards the leftmost child of the tree and checks for the BST
  22.             return 0;
  23.         if (prev != NULL && root->data <= prev->data)
  24.             return 0;
  25.         prev = root;
  26.         return is_bst(root->right);    //moves the corresponding right child of the tree and checks for the BST
  27.     }
  28.     return 1;
  29. }
  30.  
  31. struct node* newNode(int data)
  32. {
  33.     struct node* node = (struct node*)malloc(sizeof(struct node));
  34.     node->data = data;
  35.     node->left = NULL;
  36.     node->right = NULL;
  37.  
  38.     return(node);
  39. }
  40.  
  41. int main()
  42. {
  43.   /*
  44.     The input tree is as shown below
  45.                 40
  46.                 / \
  47.             20        60
  48.             / \       \
  49.         10        30      80
  50.                           \
  51.                             90
  52.   */
  53.     struct node *root = newNode(40);
  54.     root->left        = newNode(20);
  55.     root->right       = newNode(60);
  56.     root->left->left  = newNode(10);
  57.     root->left->right = newNode(30);
  58.     root->right->right = newNode(80);
  59.     root->right->right->right = newNode(90);
  60.     if (is_bst(root))
  61.         printf("TREE 1 Is BST");
  62.     else
  63.         printf("TREE 1 Not a BST");
  64.     prev = NULL;
  65. /*
  66.     The input tree is as shown below
  67.                 50
  68.                 / \
  69.             20        30
  70.             / \      
  71.         70        80 
  72.         / \          \
  73.     10     40     60
  74. */
  75.     struct node *root1 = newNode(50);
  76.     root1->left  = newNode(20);
  77.     root1->right  = newNode(30);
  78.     root1->left->left  = newNode(70);
  79.     root1->left->right = newNode(80);
  80.     root1->left->left->right = newNode(40);
  81.     root1->left->left->leftt = newNode(90);
  82.     if (is_bst1(root1))
  83.         printf("TREE 2 Is BST");
  84.     else
  85.         printf("TREE 2 Not a BST");
  86.     return 0;
  87. }

$ cc tree8.c
$ a.out
TREE 1 Is BST
TREE 2 Not a BST

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.