0% found this document useful (0 votes)
1 views

Data Sturcture and Algorithm Week 12

Uploaded by

hm896980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Data Sturcture and Algorithm Week 12

Uploaded by

hm896980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT WEEK – 12

Name – HIMANSHU RAJ

Register No. - EA2331201010152

Program - B.C.A.-Data Science

Subject – Data Structure & Algorithm (DSA)

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.

Recursive Validation Approach:

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).

Applying the Algorithm to the Image:

1. Identify the root node in the image.


2. Call the recursive validation function on the root node, passing the maximum
allowed value for the left subtree (which could be positive or negative infinity
depending on the problem context) and the minimum allowed value for the
right subtree (which could be positive infinity).
3. The recursive function will traverse the entire tree, checking each node's value
against its left and right subtrees' bounds.
4. If all checks throughout the recursion succeed, the tree is a valid BST. If any
check fails, the tree is not a valid BST.

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

You might also like