Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views3 pages

Assignment No 6

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

SY B.

Tech Data Structure Lab

Assignment No-06
Title:

Write a non-recursive version of finding the Least Common Ancestor (LCA) of any two
nodes in a Binary Search Tree (BST) and optimize your non-recursive search by balancing
the tree.

Theory:

What is a Binary Search Tree (BST)?

A Binary Search Tree is a hierarchical data structure in which each node has at most two
children, referred to as the left child and the right child. BSTs are structured to maintain a
specific ordering property: for any given node, all elements in the left subtree are smaller than
the node, and all elements in the right subtree are larger.

What is the Least Common Ancestor (LCA)?

The Least Common Ancestor of two nodes in a BST is defined as the deepest node that is an
ancestor of both nodes. In other words, it's the lowest node in the tree that has both nodes as
descendants.

Non-Recursive LCA Algorithm:

To find the Least Common Ancestor of two nodes in a BST, we can take advantage of the
ordering property of BSTs. The steps are as follows:

1. Start from the root of the tree.


2. Iterate through the tree, comparing the values of the two nodes with the current node:
o If both nodes are smaller than the current node, move to the left subtree.
o If both nodes are larger than the current node, move to the right subtree.
o If one node is smaller and the other is larger (or one is equal to the current
node), then the current node is the LCA.
3. Return the current node as the LCA.

Non-Recursive Algorithm:

struct Node {
int value;
Node* left;
Node* right;};

Node* find_LCA(Node* root, Node* node1, Node* node2) {

while (root) {

if (node1->value < root->value && node2->value < root->value) {


VPKBIET, Baramati
SY B.Tech Data Structure Lab

root = root->left; // Both nodes are in the left subtree

} else if (node1->value > root->value && node2->value > root->value) {

root = root->right; // Both nodes are in the right subtree

} else {

return root; // Current node is the LCA

return nullptr; // If no LCA is found

Optimizing the LCA Search:

To optimize the search, we need to ensure that the tree remains balanced. A balanced BST
ensures that the depth of the tree is logarithmic, keeping the search time efficient.

One popular approach for keeping the tree balanced is to use self-balancing trees such as:

 AVL Tree: Rotates nodes to ensure the height difference between subtrees is at most 1.
 Red-Black Tree: Ensures that no path is more than twice as long as any other, keeping
operations close to O(logn).

Applications of LCA in BST:

 Hierarchical Systems: In computing or organizational systems where decisions are based on


hierarchical relationships.
 Genetic Trees: LCA helps in determining common ancestors in family trees.
 Networking: Finding common routers in network pathfinding.

Examples :

Input: LCA of 10 and 14


VPKBIET, Baramati
SY B.Tech Data Structure Lab

Output: 12
Explanation: 12 is the closest node to both 10 and 14
which is a ancestor of both the nodes.

Input: LCA of 8 and 14


Output: 8
Explanation: 8 is the closest node to both 8 and 14
which is a ancestor of both the nodes.

Conclusion:

In this assignment, we developed a non-recursive algorithm to find the Least Common


Ancestor (LCA) in a Binary Search Tree. Furthermore, we discussed the importance of
balancing the tree to optimize the search process. By keeping the BST balanced, we ensure
logarithmic search times, which improves the overall efficiency.

VPKBIET, Baramati

You might also like