Skip to content

Commit d164c32

Browse files
committed
made the requested changes
1 parent 20ca12d commit d164c32

File tree

2 files changed

+23
-46
lines changed

2 files changed

+23
-46
lines changed

Graphs/BinaryLifting.js

+22-45
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,53 @@
1111

1212
class BinaryLifting {
1313
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
1617
for (const [i, j] of tree) {
1718
this.addEdge(i, j)
1819
}
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))
2421
this.dfs(root, root)
2522
}
2623

2724
addNode (node) {
2825
// 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())
3027
}
3128

3229
addEdge (node1, node2) {
3330
// 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)) {
3532
this.addNode(node1)
3633
}
37-
if (!(node2 in this.connections)) {
34+
if (!this.connections.has(node2)) {
3835
this.addNode(node2)
3936
}
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)
4239
}
4340

4441
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))
4948
}
50-
for (const child of this.connections[node]) {
49+
for (const child of this.connections.get(node)) {
5150
if (child !== parent) this.dfs(child, node)
5251
}
5352
}
5453

5554
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++) {
5759
if (k & (1 << i)) {
58-
node = this.up[node][i]
60+
node = this.up.get(node).get(i)
5961
}
6062
}
6163
return node
@@ -72,29 +74,4 @@ function binaryLifting (root, tree, queries) {
7274
return ancestors
7375
}
7476

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

Graphs/test/BinaryLifting.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { binaryLifting } from '../BinaryLifting'
1+
import binaryLifting from '../BinaryLifting'
22

33
// The graph for Test Case 1 looks like this:
44
//

0 commit comments

Comments
 (0)