We have seen various methods with different Time Complexities to calculate LCA in n-ary tree:-
Lets study another method which has faster query time than all the above methods. So, our aim will be to calculate LCA in constant time ~ O(1). Let’s see how we can achieve it.
Method 4 : Using Range Minimum Query
We have discussed LCA and RMQ for binary tree. Here we discuss LCA problem to RMQ problem conversion for n-ary tree.
Pre-requisites:- LCA in Binary Tree using RMQ RMQ using sparse table
Key Concept : In this method, we will be reducing our LCA problem to RMQ(Range Minimum Query) problem over a static array. Once, we do that then we will relate the Range minimum queries to the required LCA queries.
The first step will be to decompose the tree into a flat linear array. To do this we can apply the Euler walk . The Euler walk will give the pre-order traversal of the graph. So we will perform a Euler Walk on the tree and store the nodes in an array as we visit them. This process reduces the tree data-structure to a simple linear array.
Consider the below tree and the euler walk over it :-
Now lets think in general terms : Consider any two nodes on the tree. There will be exactly one path connecting both the nodes and the node that has the smallest depth value in the path will be the LCA of the two given nodes.
Now take any two distinct node say u and v in the Euler walk array. Now all the elements in the path from u to v will lie in between the index of nodes u and v in the Euler walk array. Therefore, we just need to calculate the node with the minimum depth between the index of node u and node v in the euler array.
For this we will maintain another array that will contain the depth of all the nodes corresponding to their position in the Euler walk array so that we can Apply our RMQ algorithm on it.
Given below is the euler walk array parallel to its depth track array.
Example :- Consider two nodes node 6 and node 7 in the euler array. To calculate the LCA of node 6 and node 7 we look the the smallest depth value for all the nodes in between node 6 and node 7 .
Therefore, node 1 has the smallest depth value = 0 and hence, it is the LCA for node 6 and node 7.
We will be maintaining three arrays 1)Euler Path 2)Depth array 3)First Appearance Index
Euler Path and Depth array are the same as described above
First Appearance Index FAI : The First Appearance index Array will store the index for the first position of every node in the Euler Path array. FAI[i] = First appearance of ith node in Euler Walk array.
The C++ Implementation for the above method is given below:-
LCA(6,7) : 1 LCA(6,4) : 2
Note : We are precalculating all the required power of 2’s and also precalculating the all the required log values to ensure constant time complexity per query. Else if we did log calculation for every query operation our Time complexity would have not been constant.
Time Complexity: The Conversion process from LCA to RMQ is done by Euler Walk that takes O(n) time.
Pre-processing for the sparse table in RMQ takes O(nlogn) time and answering each Query is a Constant time process. Therefore, overall Time Complexity is O(nlogn) – preprocessing and O(1) for each query.
This article is contributed by Nitish Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Query for ancestor-descendant relationship in a tree
- Segment Tree | Set 2 (Range Minimum Query)
- Iterative Segment Tree (Range Minimum Query)
- Segment Tree | Set 2 (Range Maximum Query with Node Update)
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Constant time range add operation on an array
- Connect nodes at same level using constant extra space
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Range sum query using Sparse Table
- Range query for count of set bits
- Difference Array | Range update query in O(1)
- Range and Update Query for Chessboard Pieces
- Range query for Largest Sum Contiguous Subarray
- Pair with minimum absolute difference after solving each query
- MO's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction)