Skip to content

Commit 38d17a8

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Change id to index
1 parent 145c501 commit 38d17a8

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/main/java/com/williamfiset/algorithms/graphtheory/treealgorithms/LowestCommonAncestorEulerTour.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ public static class TreeNode {
2424
// Number of nodes in the subtree. Computed when tree is built.
2525
private int n;
2626

27-
private int id;
27+
private int index;
2828
private TreeNode parent;
2929
private List<TreeNode> children;
3030

3131
// Useful constructor for root node.
32-
public TreeNode(int id) {
33-
this(id, /*parent=*/ null);
32+
public TreeNode(int index) {
33+
this(index, /*parent=*/ null);
3434
}
3535

36-
public TreeNode(int id, TreeNode parent) {
37-
this.id = id;
36+
public TreeNode(int index, TreeNode parent) {
37+
this.index = index;
3838
this.parent = parent;
3939
children = new LinkedList<>();
4040
}
@@ -54,8 +54,8 @@ public int size() {
5454
return n;
5555
}
5656

57-
public int id() {
58-
return id;
57+
public int index() {
58+
return index;
5959
}
6060

6161
public TreeNode parent() {
@@ -79,9 +79,9 @@ public static TreeNode rootTree(List<List<Integer>> graph, int rootId) {
7979
// Do dfs to construct rooted tree.
8080
private static TreeNode buildTree(List<List<Integer>> graph, TreeNode node) {
8181
int subtreeNodeCount = 1;
82-
for (int neighbor : graph.get(node.id())) {
82+
for (int neighbor : graph.get(node.index())) {
8383
// Ignore adding an edge pointing back to parent.
84-
if (node.parent() != null && neighbor == node.parent().id()) {
84+
if (node.parent() != null && neighbor == node.parent().index()) {
8585
continue;
8686
}
8787

@@ -97,14 +97,14 @@ private static TreeNode buildTree(List<List<Integer>> graph, TreeNode node) {
9797

9898
@Override
9999
public String toString() {
100-
return String.valueOf(id);
100+
return String.valueOf(index);
101101
}
102102
}
103103

104104
private final int n;
105105
private final TreeNode root;
106106

107-
private int index = 0;
107+
private int tourIndex = 0;
108108
private boolean preprocessed;
109109

110110
// Populated when constructing Euler Tour.
@@ -139,10 +139,10 @@ private void preprocess() {
139139
}
140140

141141
private void visit(TreeNode node, long depth) {
142-
nodeOrder[index] = node;
143-
nodeDepth[index] = depth;
144-
last[node.id()] = index;
145-
index++;
142+
nodeOrder[tourIndex] = node;
143+
nodeDepth[tourIndex] = depth;
144+
last[node.index()] = tourIndex;
145+
tourIndex++;
146146
}
147147

148148
// Construct Euler Tour by populating the 'depth' and 'nodeOrder' arrays.
@@ -323,19 +323,19 @@ private static void example1() {
323323

324324
TreeNode lca = solver.lca(10, 15);
325325
System.out.printf("LCA of 10 and 15 = %s\n", lca);
326-
if (lca.id() != 5) {
326+
if (lca.index() != 5) {
327327
System.out.println("Error, expected lca to be 5");
328328
}
329329

330330
lca = solver.lca(13, 14);
331331
System.out.printf("LCA of 13 and 14 = %s\n", lca);
332-
if (lca.id() != 2) {
332+
if (lca.index() != 2) {
333333
System.out.println("Error, expected lca to be 2");
334334
}
335335

336336
lca = solver.lca(9, 11);
337337
System.out.printf("LCA of 9 and 11 = %s\n", lca);
338-
if (lca.id() != 0) {
338+
if (lca.index() != 0) {
339339
System.out.println("Error, expected lca to be 0");
340340
}
341341
}

src/test/java/com/williamfiset/algorithms/graphtheory/treealgorithms/LowestCommonAncestorEulerTourTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ private LowestCommonAncestorEulerTour.TreeNode createFirstTreeFromSlides() {
3535
public void testLcaTreeFromSlides1() {
3636
LowestCommonAncestorEulerTour.TreeNode root = createFirstTreeFromSlides();
3737
LowestCommonAncestorEulerTour fastSolver = new LowestCommonAncestorEulerTour(root);
38-
assertThat(fastSolver.lca(14, 13).id()).isEqualTo(2);
39-
assertThat(fastSolver.lca(10, 16).id()).isEqualTo(5);
40-
assertThat(fastSolver.lca(9, 11).id()).isEqualTo(0);
38+
assertThat(fastSolver.lca(14, 13).index()).isEqualTo(2);
39+
assertThat(fastSolver.lca(10, 16).index()).isEqualTo(5);
40+
assertThat(fastSolver.lca(9, 11).index()).isEqualTo(0);
4141
}
4242

4343
@Test
4444
public void testLcaTreeFromSlides2() {
4545
LowestCommonAncestorEulerTour.TreeNode root = createFirstTreeFromSlides();
4646
LowestCommonAncestorEulerTour fastSolver = new LowestCommonAncestorEulerTour(root);
47-
assertThat(fastSolver.lca(8, 9).id()).isEqualTo(3);
48-
assertThat(fastSolver.lca(4, 8).id()).isEqualTo(1);
49-
assertThat(fastSolver.lca(6, 13).id()).isEqualTo(2);
50-
assertThat(fastSolver.lca(7, 13).id()).isEqualTo(7);
51-
assertThat(fastSolver.lca(10, 5).id()).isEqualTo(5);
52-
assertThat(fastSolver.lca(2, 16).id()).isEqualTo(2);
47+
assertThat(fastSolver.lca(8, 9).index()).isEqualTo(3);
48+
assertThat(fastSolver.lca(4, 8).index()).isEqualTo(1);
49+
assertThat(fastSolver.lca(6, 13).index()).isEqualTo(2);
50+
assertThat(fastSolver.lca(7, 13).index()).isEqualTo(7);
51+
assertThat(fastSolver.lca(10, 5).index()).isEqualTo(5);
52+
assertThat(fastSolver.lca(2, 16).index()).isEqualTo(2);
5353
}
5454

5555
@Test
@@ -59,7 +59,7 @@ public void testLcaOfTheSameNodeIsItself() {
5959

6060
// Try all nodes
6161
for (int id = 0; id < root.size(); id++) {
62-
assertThat(fastSolver.lca(id, id).id()).isEqualTo(id);
62+
assertThat(fastSolver.lca(id, id).index()).isEqualTo(id);
6363
}
6464
}
6565

@@ -86,7 +86,7 @@ public void randomizedLcaQueriesVsOtherImpl() {
8686

8787
assertThat(lca1).isNotNull();
8888
assertThat(lca2).isNotNull();
89-
assertThat(lca1.id()).isEqualTo(lca2.id());
89+
assertThat(lca1.id()).isEqualTo(lca2.index());
9090
}
9191
}
9292
}

0 commit comments

Comments
 (0)