I am looking for a constant implementation of the minimum common ancestor given by two nodes in a complete binary tree (parent x than child 2 * x and 2 * x + 1).
My problem is that there are a lot of nodes and a lot of queries in the tree. Is there an algorithm that preprocesses so that requests can be answered in constant time.
I looked at the LCA using RMQ , but I cannot use this method since I cannot use an array for this many nodes in the tree.
Can someone give me an effective implementation of the algorithm for quickly answering many queries, knowing that this is a complete binary tree, and the connection between the nodes is given above.
What I did was start with two given nodes and sequentially find their parents (node / 2) to save the hash list of visited nodes. when we reach a node that is already in the hash list, than node will be the lowest common ancestor.
But when there are a lot of queries, this algorithm takes a lot of time, as in the worst case I may need a height of 30 (maximum tree height) to reach the root (worst case).
source
share