From ffb45c604aa33998023ea56be89b50e6a12821a1 Mon Sep 17 00:00:00 2001 From: Kamini Prajapati Date: Mon, 28 Oct 2024 16:39:05 +0530 Subject: [PATCH 1/2] 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 From 0b77c5c35be927a4c0ec36b3e6258207e33d58c7 Mon Sep 17 00:00:00 2001 From: Kamini Prajapati Date: Tue, 29 Oct 2024 16:09:57 +0530 Subject: [PATCH 2/2] made changes --- .../Check if a Tree is a BST or not/README.md | 75 +++++++++++++++---- 1 file changed, 62 insertions(+), 13 deletions(-) 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 index 89e67e9e..8e94356f 100644 --- 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 @@ -1,19 +1,68 @@ -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. +Check if a Tree is a Binary Search Tree (BST) -Explanation of the Code +This section describes the implementation of a function in C that verifies whether a given binary tree satisfies the properties of a Binary Search Tree (BST). -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. +Problem Statement -isBST Function: -->This function initializes the range for the root node using INT_MIN and INT_MAX, covering all integer values. +A Binary Search Tree (BST) is a binary tree where for every node: -Main Function: -->We create a sample tree and test if it’s a BST by calling isBST. +->The value of all nodes in the left subtree is less than the node’s value. +->The value of all nodes in the right subtree is greater than the node’s value. -This approach runs in O(n) +Given a binary tree, our goal is to determine if it meets these properties. -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 +Solution Approach + +The implementation uses recursion to verify that each node in the tree is within a specified range of allowable values: + +1. Recursively Traverse the Tree: For each node, ensure that its value lies within the defined minimum and maximum range. +2. Update Range for Subtrees: + (i) For the left subtree, the maximum allowable value is updated to the current node's value. + (ii) For the right subtree, the minimum allowable value is updated to the current node's value. +3. Return Boolean: + If all nodes respect the BST properties, the function returns true; otherwise, it returns false. + + +Code Explanation +The implementation consists of two main functions: + +1. Helper Function: isBSTUtil + +(i) This function takes a node, a minimum value, and a maximum value as input. +(ii) For each node, it checks: + -> If the node is NULL, it returns true (an empty subtree is valid). + -> If the node’s value is within the valid range (between min and max). +(iii) It recursively calls itself for the left and right children, updating the valid range: + -> For the left child, it narrows the max range to the current node's value. + -> For the right child, it expands the min range to the current node's value. + +3. Main Function: isBST + + (i) This function initializes the range using INT_MIN and INT_MAX to cover all possible integer values. + (ii) It calls isBSTUtil on the root node. + +Consider the following binary tree: + + 10 + / \ + 5 20 + / \ + 3 8 +In this example: + +-> The function will verify that all nodes in the left subtree (5, 3, 8) are less than 10. +-> All nodes in the right subtree (20) are greater than 10. +-> Hence, this tree is a valid BST, and the function will return true. + +Testing +The provided code includes a simple test case in the main function to demonstrate functionality. To further test, use additional edge cases like: +->An empty tree. +->A tree with only one node. +->Trees where nodes violate BST properties. + +This function is highly efficient and is designed for validating whether a binary tree structure adheres to BST rules in O(n) time complexity. + + +Complexity Analysis +1. Time Complexity: O(n), where n is the number of nodes, as we need to visit each node once. +2. Space Complexity: O(h), where h is the height of the tree, due to the recursive stack during traversal. \ No newline at end of file