From ffb45c604aa33998023ea56be89b50e6a12821a1 Mon Sep 17 00:00:00 2001 From: Kamini Prajapati Date: Mon, 28 Oct 2024 16:39:05 +0530 Subject: [PATCH] done --- .../Check if a Tree is a BST or not/Program.c | 51 +++++++++++++++++++ .../Check if a Tree is a BST or not/README.md | 19 +++++++ 2 files changed, 70 insertions(+) create mode 100644 Binary Tree Algorithms/Check if a Tree is a BST or not/Program.c create mode 100644 Binary Tree Algorithms/Check if a Tree is a BST or not/README.md diff --git a/Binary Tree Algorithms/Check if a Tree is a BST or not/Program.c b/Binary Tree Algorithms/Check if a Tree is a BST or not/Program.c new file mode 100644 index 00000000..cfdef368 --- /dev/null +++ b/Binary Tree Algorithms/Check if a Tree is a BST or not/Program.c @@ -0,0 +1,51 @@ +#include +#include +#include + +struct Node { + int data; + struct Node* left; + struct Node* right; +}; + +// Function to create a new node +struct Node* newNode(int data) { + struct Node* node = (struct Node*)malloc(sizeof(struct Node)); + node->data = data; + node->left = node->right = NULL; + return node; +} + +// Utility function to check BST properties +bool isBSTUtil(struct Node* node, int min, int max) { + if (node == NULL) + return true; + + // Node's value must lie between min and max + if (node->data <= min || node->data >= max) + return false; + + // Recursively check left and right subtrees + return isBSTUtil(node->left, min, node->data) && + isBSTUtil(node->right, node->data, max); +} + +// Main function to check if a tree is a BST +bool isBST(struct Node* root) { + return isBSTUtil(root, INT_MIN, INT_MAX); +} + +int main() { + struct Node* root = newNode(10); + root->left = newNode(5); + root->right = newNode(20); + root->left->left = newNode(3); + root->left->right = newNode(8); + + if (isBST(root)) + printf("The tree is a Binary Search Tree\n"); + else + printf("The tree is not a Binary Search Tree\n"); + + return 0; +} diff --git a/Binary Tree Algorithms/Check if a Tree is a BST or not/README.md b/Binary Tree Algorithms/Check if a Tree is a BST or not/README.md new file mode 100644 index 00000000..89e67e9e --- /dev/null +++ b/Binary Tree Algorithms/Check if a Tree is a BST or not/README.md @@ -0,0 +1,19 @@ +The function recursively verifies that each node’s value lies within a specific range, ensuring all nodes in the left subtree are less and all nodes in the right subtree are greater than the current node. + +Explanation of the Code + +isBSTUtil Function: +->This recursive utility function checks whether each node’s value is within a specific range (min to max). +->It checks if the current node's value violates the range conditions. +->It then recursively checks left and right subtrees with updated ranges to maintain BST constraints. + +isBST Function: +->This function initializes the range for the root node using INT_MIN and INT_MAX, covering all integer values. + +Main Function: +->We create a sample tree and test if it’s a BST by calling isBST. + +This approach runs in O(n) + +O(n) time complexity, where n is the number of nodes, and it uses O(h) space for recursion, where +h is the height of the tree. \ No newline at end of file