Data Sturcture and Algorithm Week 12
Data Sturcture and Algorithm Week 12
Semester – II Semester
EA2331201010152
Ans. Binary Search Tree (BST) Properties:
A binary search tree (BST) is a fundamental data structure that efficiently organizes
and searches for sorted elements. It's a rooted tree (has a single root node) where
each node follows these key properties:
• Ordering Constraint: The value of a node is always greater than all the values
in its left subtree and less than all the values in its right subtree. This ordering
rule applies to every node in the BST.
Intuitive Explanation:
Imagine the BST as a search tree where you start at the root node. If the value you're
searching for is less than the root's value, you move to the left subtree, where all
elements are guaranteed to be less than the root. Conversely, if the search value is
greater than the root, you move to the right subtree, where elements are greater
than the root. This process of traversing left or right based on the comparison values
guides you efficiently towards the target node or indicates its absence in the tree.
To check if a given binary tree adheres to the BST properties, we can employ a
recursive validation algorithm. Here's a breakdown of the steps:
1. Base Case: If the current node being examined is None (representing an empty
subtree), it's considered a valid BST by default (an empty subtree inherently
follows the BST rules).
2. Recursive Checks on Subtrees:
o Left Subtree: Recursively call the validation function on the left child of
the current node. During this recursive call, pass an additional argument
specifying the upper bound (maximum allowed value) for the left subtree.
This upper bound is typically the value of the current node (parent node),
ensuring elements in the left subtree are strictly less than the parent.
EA2331201010152
o Right Subtree: Similarly, perform a recursive validation call on the right
child of the current node. Here, pass the lower bound (minimum allowed
value) as the current node's value, guaranteeing elements in the right
subtree are strictly greater than the parent.
3. Validation and Returning a Boolean:
o If both the recursive checks for the left and right subtrees return True
(indicating valid BSTs), and the current node's value falls within the
allowed range (considering the upper and lower bounds passed during
subtree checks), the entire tree rooted at the current node is a valid BST.
Return True in this case.
o If any of the following conditions occur, the tree rooted at the current
node is not a valid BST, and we return False:
▪ The left subtree validation fails (returns False).
▪ The right subtree validation fails (returns False).
▪ The current node's value violates the BST property (e.g., a node in
the left subtree has a value greater than the current node or vice
versa).
Example:
Consider a BST with the root node having a value of 50. During validation:
• The left subtree's maximum allowed value would be 49 (less than the root).
• The right subtree's minimum allowed value would be 51 (greater than the root).
• The recursive validation would ensure that all nodes in the left subtree have
values less than 49 and all nodes in the right subtree have values greater than
51.
EA2331201010152