11
11
12
12
class BinaryLifting {
13
13
constructor ( root , tree ) {
14
- this . connections = { }
15
- this . up = { } // up[node][i] stores the 2^i-th parent of node
14
+ this . root = root
15
+ this . connections = new Map ( )
16
+ this . up = new Map ( ) // up[node][i] stores the 2^i-th parent of node
16
17
for ( const [ i , j ] of tree ) {
17
18
this . addEdge ( i , j )
18
19
}
19
- // LOG should be such that 2^LOG is greater than total number of nodes in the tree
20
- this . LOG = 0
21
- while ( ( 1 << this . LOG ) <= Object . keys ( this . connections ) . length ) {
22
- this . LOG ++
23
- }
20
+ this . log = Math . ceil ( Math . log2 ( this . connections . size ) )
24
21
this . dfs ( root , root )
25
22
}
26
23
27
24
addNode ( node ) {
28
25
// Function to add a node to the tree (connection represented by set)
29
- this . connections [ node ] = new Set ( )
26
+ this . connections . set ( node , new Set ( ) )
30
27
}
31
28
32
29
addEdge ( node1 , node2 ) {
33
30
// Function to add an edge (adds the node too if they are not present in the tree)
34
- if ( ! ( node1 in this . connections ) ) {
31
+ if ( ! this . connections . has ( node1 ) ) {
35
32
this . addNode ( node1 )
36
33
}
37
- if ( ! ( node2 in this . connections ) ) {
34
+ if ( ! this . connections . has ( node2 ) ) {
38
35
this . addNode ( node2 )
39
36
}
40
- this . connections [ node1 ] . add ( node2 )
41
- this . connections [ node2 ] . add ( node1 )
37
+ this . connections . get ( node1 ) . add ( node2 )
38
+ this . connections . get ( node2 ) . add ( node1 )
42
39
}
43
40
44
41
dfs ( node , parent ) {
45
- this . up [ node ] = { }
46
- this . up [ node ] [ 0 ] = parent
47
- for ( let i = 1 ; i < this . LOG ; i ++ ) {
48
- this . up [ node ] [ i ] = this . up [ this . up [ node ] [ i - 1 ] ] [ i - 1 ]
42
+ this . up . set ( node , new Map ( ) )
43
+ this . up . get ( node ) . set ( 0 , parent )
44
+ for ( let i = 1 ; i < this . log ; i ++ ) {
45
+ this . up
46
+ . get ( node )
47
+ . set ( i , this . up . get ( this . up . get ( node ) . get ( i - 1 ) ) . get ( i - 1 ) )
49
48
}
50
- for ( const child of this . connections [ node ] ) {
49
+ for ( const child of this . connections . get ( node ) ) {
51
50
if ( child !== parent ) this . dfs ( child , node )
52
51
}
53
52
}
54
53
55
54
kthAncestor ( node , k ) {
56
- for ( let i = 0 ; i < this . LOG ; i ++ ) {
55
+ if ( k >= this . connections . size ) {
56
+ return this . root
57
+ }
58
+ for ( let i = 0 ; i < this . log ; i ++ ) {
57
59
if ( k & ( 1 << i ) ) {
58
- node = this . up [ node ] [ i ]
60
+ node = this . up . get ( node ) . get ( i )
59
61
}
60
62
}
61
63
return node
@@ -72,29 +74,4 @@ function binaryLifting (root, tree, queries) {
72
74
return ancestors
73
75
}
74
76
75
- export { binaryLifting }
76
-
77
- // binaryLifting(
78
- // 0,
79
- // [
80
- // [0, 1],
81
- // [0, 3],
82
- // [0, 5],
83
- // [5, 6],
84
- // [1, 2],
85
- // [1, 4],
86
- // [4, 7],
87
- // [7, 11],
88
- // [7, 8],
89
- // [8, 9],
90
- // [9, 10]
91
- // ],
92
- // [
93
- // [10, 4],
94
- // [10, 7],
95
- // [7, 2],
96
- // [11, 3]
97
- // ]
98
- // )
99
-
100
- // [4, 0, 1, 1]
77
+ export default binaryLifting
0 commit comments