MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / LCABinaryLifting

Class LCABinaryLifting

Graphs/LCABinaryLifting.js:11–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import { BinaryLifting } from './BinaryLifting'
10
11class 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
51function lcaBinaryLifting(root, tree, queries) {
52 const graphObject = new LCABinaryLifting(root, tree)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected