forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCABinaryLifting.js
More file actions
Latest commit
61 lines (54 loc) · 1.93 KB
/
Copy pathLCABinaryLifting.js
File metadata and controls
61 lines (54 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Author: Adrito Mukherjee
* Finding Lowest Common Ancestor By Binary Lifting implementation in JavaScript
* The technique requires preprocessing the tree in O(N log N) using dynamic programming)
* It can be used to find Lowest Common Ancestor of two nodes in O(log N)
* Tutorial on Lowest Common Ancestor: https://www.geeksforgeeks.org/lca-in-a-tree-using-binary-lifting-technique
*/
import{BinaryLifting}from'./BinaryLifting'
classLCABinaryLiftingextendsBinaryLifting{
constructor(root,tree){
super(root,tree)
this.depth=newMap()// depth[node] stores the depth of node from root
this.depth.set(root,1)
this.dfsDepth(root,root)
}
dfsDepth(node,parent){
// DFS to find depth of every node in the tree
for(constchildofthis.connections.get(node)){
if(child!==parent){
this.depth.set(child,this.depth.get(node)+1)
this.dfsDepth(child,node)
}
}
}
getLCA(node1,node2){
// We make sure that node1 is the deeper node among node1 and node2
if(this.depth.get(node1)<this.depth.get(node2)){
;[node1,node2]=[node2,node1]
}
// We check if node1 is the ancestor of node2, and if so, then return node1
constk=this.depth.get(node1)-this.depth.get(node2)
node1=this.kthAncestor(node1,k)
if(node1===node2){
returnnode1
}
for(leti=this.log-1;i>=0;i--){
if(this.up.get(node1).get(i)!==this.up.get(node2).get(i)){
node1=this.up.get(node1).get(i)
node2=this.up.get(node2).get(i)
}
}
returnthis.up.get(node1).get(0)
}
}
functionlcaBinaryLifting(root,tree,queries){
constgraphObject=newLCABinaryLifting(root,tree)
constlowestCommonAncestors=[]
for(const[node1,node2]ofqueries){
constlca=graphObject.getLCA(node1,node2)
lowestCommonAncestors.push(lca)
}
returnlowestCommonAncestors
}
exportdefaultlcaBinaryLifting