- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLowestCommonAncestor.java
More file actions
Latest commit
158 lines (131 loc) · 4.57 KB
/
Copy pathLowestCommonAncestor.java
File metadata and controls
158 lines (131 loc) · 4.57 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @Author Gyeong Lemidia, https://github.com/lemidia
*
* LCA Algorithm code written in java
* Euler Tour + Sparse Table for RMQ(Range Minimum Query)
*/
publicclassLowestCommonAncestor {
// Below is code for Sparse Table
staticintmin (inta, intb) {
returna < b ? a : b;
}
staticbooleanisValid(intj, inti, intn) {
returnj + (int)Math.pow(2, i) <= n;
}
staticdoublebaseLog(doublex, doublebase){
returnMath.log(x) / Math.log(base);
}
staticvoidconstruct(intsparseTable[][], intindexTable[][], introw, intn, intarr[]) {
for (inti = 0; i < n; i++){
sparseTable[0][i] = arr[i];
indexTable[0][i] = i;
}
for (inti = 1; i <= row; i++) {
for (intj = 0; j < n; j++) {
if (isValid(j, i, n)) {
if (sparseTable[i-1][j] < sparseTable[i-1][j+(int)Math.pow(2, i-1)]) {
sparseTable[i][j] = sparseTable[i-1][j];
indexTable[i][j] = indexTable[i-1][j];
}else {
sparseTable[i][j] = sparseTable[i-1][j+(int)Math.pow(2, i-1)];
indexTable[i][j] = indexTable[i-1][j+(int)Math.pow(2, i-1)];
}
}
}
}
}
staticintminQuery(intsparseTable[][], intl, intr) {
intp = (int)baseLog(r-l+1, 2);
intk = (int)Math.pow(2, p);
returnmin(sparseTable[p][l], sparseTable[p][r-k+1]);
}
staticintminQueryIndex(intsparseTable[][], intindexTable[][], intl , intr) {
intp = (int)baseLog(r-l+1, 2);
intk = (int)Math.pow(2, p);
if (sparseTable[p][l] < sparseTable[p][r-k+1]) {
returnindexTable[p][l];
}else {
returnindexTable[p][r-k+1];
}
}
// Code for Sparse Table End
// Below is code for LCA
staticinttourIndex = 0;
staticintmax (inta, intb) {
returna > b ? a : b;
}
staticclassTreeNode {
intindex;
TreeNodechildren[];
publicTreeNode(intindex) {
this.index = index;
children = newTreeNode[2];
}
}
/*
Sample Tree used in this code.
0
/ \
/ \
1 2
/ \ / \
/ \ / \
3 4 5 6
LCA of 1,2 = 0
LCA of 1,0 = 0
LCA of 3,4 = 1
LCA of 4,5 = 0
LCA of 3,3 = 3
*/
// Do Euler Tour and marking index num for each node
staticvoiddfs (intdepth[], intnodes[], intlast[], TreeNodenode, intcurrentDepth) {
depth[tourIndex] = currentDepth;
nodes[tourIndex] = node.index;
last[node.index] = tourIndex;
tourIndex++;
for (TreeNodechild : node.children) {
if (child != null){
dfs(depth, nodes, last, child, currentDepth+1);
depth[tourIndex] = currentDepth;
nodes[tourIndex] = node.index;
last[node.index] = tourIndex;
tourIndex++;
}
}
}
staticintLCA (inta, intb, intnodes[], intlast[], intsparseTable[][], intindexTable[][]) {
a = last[a];
b = last[b];
intminIndex = minQueryIndex(sparseTable, indexTable, min(a, b), max(a, b));
returnnodes[minIndex];
}
// Code for LCA End
// Main method
publicstaticvoidmain(String[] args) {
// Construct Sample Tree
TreeNoderoot = newTreeNode(0);
root.children[0] = newTreeNode(1);
root.children[1] = newTreeNode(2);
root.children[0].children[0] = newTreeNode(3);
root.children[0].children[1] = newTreeNode(4);
root.children[1].children[0] = newTreeNode(5);
root.children[1].children[1] = newTreeNode(6);
// Setup Array information for LCA
intnodeCnt = 7;
intdepth [] = newint[2*nodeCnt-1];
intnodes [] = newint[2*nodeCnt-1];
intlast [] = newint[nodeCnt];
if (root != null)
dfs(depth, nodes, last, root, 0);
// Setup Sparse Table for Range Min Query
// depth 배열로 Sparse Table 을 구축한다.
intcolCnt = 2*nodeCnt-1;
introw = (int)baseLog(colCnt, 2);
intsparseTable[][] = newint[row+1][colCnt];
intindexTable[][] = newint[row+1][colCnt];
construct(sparseTable, indexTable, row, colCnt, depth);
intresult = LCA(4, 5, nodes, last, sparseTable, indexTable);
// Output : 0
System.out.print(result);
}
}