forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCABinaryLifting.test.js
80 lines (76 loc) · 1.3 KB
/
LCABinaryLifting.test.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import lcaBinaryLifting from '../LCABinaryLifting'
// The graph for Test Case 1 looks like this:
//
// 0
// /|\
// / | \
// 1 3 5
// / \ \
// 2 4 6
// \
// 7
// / \
// 11 8
// \
// 9
// \
// 10
test('Test case 1', () => {
const root = 0
const graph = [
[0, 1],
[0, 3],
[0, 5],
[5, 6],
[1, 2],
[1, 4],
[4, 7],
[7, 11],
[7, 8],
[8, 9],
[9, 10]
]
const queries = [
[1, 3],
[6, 5],
[3, 6],
[7, 10],
[8, 10],
[11, 2],
[11, 10]
]
const lowestCommonAncestors = lcaBinaryLifting(root, graph, queries)
expect(lowestCommonAncestors).toEqual([0, 5, 0, 7, 8, 1, 7])
})
// The graph for Test Case 2 looks like this:
//
// 0
// / \
// 1 2
// / \ \
// 3 4 5
// / / \
// 6 7 8
test('Test case 2', () => {
const root = 0
const graph = [
[0, 1],
[0, 2],
[1, 3],
[1, 4],
[2, 5],
[3, 6],
[5, 7],
[5, 8]
]
const queries = [
[1, 2],
[3, 4],
[5, 4],
[6, 7],
[6, 8],
[7, 8]
]
const lowestCommonAncestors = lcaBinaryLifting(root, graph, queries)
expect(lowestCommonAncestors).toEqual([0, 1, 0, 0, 0, 5])
})