|
| 1 | +/** |
| 2 | + * Author: Adrito Mukherjee |
| 3 | + * Findind Lowest Common Ancestor By Binary Lifting implementation in JavaScript |
| 4 | + * The technique requires preprocessing the tree in O(N log N) using dynamic programming) |
| 5 | + * It can be used to find Lowest Common Ancestor of two nodes in O(log N) |
| 6 | + * Tutorial on Lowest Common Ancestor: https://www.geeksforgeeks.org/lca-in-a-tree-using-binary-lifting-technique |
| 7 | + */ |
| 8 | + |
| 9 | +import { BinaryLifting } from './BinaryLifting' |
| 10 | + |
| 11 | +class LCABinaryLifting extends BinaryLifting { |
| 12 | + constructor (root, tree) { |
| 13 | + super(root, tree) |
| 14 | + this.depth = new Map() // depth[node] stores the depth of node from root |
| 15 | + this.depth.set(root, 1) |
| 16 | + this.dfsDepth(root, root) |
| 17 | + } |
| 18 | + |
| 19 | + dfsDepth (node, parent) { |
| 20 | + // DFS to find depth of every node in the tree |
| 21 | + for (const child of this.connections.get(node)) { |
| 22 | + if (child !== parent) { |
| 23 | + this.depth.set(child, this.depth.get(node) + 1) |
| 24 | + this.dfsDepth(child, node) |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + getLCA (node1, node2) { |
| 30 | + // We make sure that node1 is the deeper node among node1 and node2 |
| 31 | + if (this.depth.get(node1) < this.depth.get(node2)) { |
| 32 | + [node1, node2] = [node2, node1] |
| 33 | + } |
| 34 | + // We check if node1 is the ancestor of node2, and if so, then return node1 |
| 35 | + const k = this.depth.get(node1) - this.depth.get(node2) |
| 36 | + node1 = this.kthAncestor(node1, k) |
| 37 | + if (node1 === node2) { |
| 38 | + return node1 |
| 39 | + } |
| 40 | + |
| 41 | + for (let i = this.log - 1; i >= 0; i--) { |
| 42 | + if (this.up.get(node1).get(i) !== this.up.get(node2).get(i)) { |
| 43 | + node1 = this.up.get(node1).get(i) |
| 44 | + node2 = this.up.get(node2).get(i) |
| 45 | + } |
| 46 | + } |
| 47 | + return this.up.get(node1).get(0) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function lcaBinaryLifting (root, tree, queries) { |
| 52 | + const graphObject = new LCABinaryLifting(root, tree) |
| 53 | + const lowestCommonAncestors = [] |
| 54 | + for (const [node1, node2] of queries) { |
| 55 | + const lca = graphObject.getLCA(node1, node2) |
| 56 | + lowestCommonAncestors.push(lca) |
| 57 | + } |
| 58 | + return lowestCommonAncestors |
| 59 | +} |
| 60 | + |
| 61 | +export default lcaBinaryLifting |
0 commit comments