14
14
15
15
public class TreeCenterLongestPathImpl {
16
16
17
- private static int [] dfs (List <List <Integer >> graph , boolean [] visited , int [] prev , int at , int parent ) {
17
+ private static int [] dfs (
18
+ List <List <Integer >> graph , boolean [] visited , int [] prev , int at , int parent ) {
18
19
19
20
// Already visited this node
20
21
if (visited [at ]) return new int [] {0 , parent };
@@ -40,7 +41,7 @@ private static int[] dfs(List<List<Integer>> graph, boolean[] visited, int[] pre
40
41
return new int [] {bestDist , index };
41
42
}
42
43
43
- public static List <Integer > treeCenter (List <List <Integer >> graph ) {
44
+ public static List <Integer > findTreeCenters (List <List <Integer >> graph ) {
44
45
List <Integer > centers = new ArrayList <>();
45
46
if (graph == null ) return centers ;
46
47
@@ -68,13 +69,13 @@ public static List<Integer> treeCenter(List<List<Integer>> graph) {
68
69
}
69
70
70
71
if (path .size () % 2 == 0 ) {
71
- centers .add (path .get (path .size ()/ 2 - 1 ));
72
+ centers .add (path .get (path .size () / 2 - 1 ));
72
73
}
73
- centers .add (path .get (path .size ()/ 2 ));
74
+ centers .add (path .get (path .size () / 2 ));
74
75
return centers ;
75
76
}
76
77
77
- /** ********** TESTING ********* */
78
+ /** ********** TESTING ********* */
78
79
79
80
// Create an empty tree as a adjacency list.
80
81
public static List <List <Integer >> createEmptyTree (int n ) {
@@ -101,29 +102,29 @@ public static void main(String[] args) {
101
102
addUndirectedEdge (graph , 6 , 8 );
102
103
103
104
// Centers are 2
104
- System .out .println (treeCenter (graph )+ "\n " );
105
+ System .out .println (findTreeCenters (graph ) + "\n " );
105
106
106
107
// Centers are 0
107
108
List <List <Integer >> graph2 = createEmptyTree (1 );
108
- System .out .println (treeCenter (graph2 )+ "\n " );
109
+ System .out .println (findTreeCenters (graph2 ) + "\n " );
109
110
110
111
// Centers are 0,1
111
112
List <List <Integer >> graph3 = createEmptyTree (2 );
112
113
addUndirectedEdge (graph3 , 0 , 1 );
113
- System .out .println (treeCenter (graph3 )+ "\n " );
114
+ System .out .println (findTreeCenters (graph3 ) + "\n " );
114
115
115
116
// Centers are 1
116
117
List <List <Integer >> graph4 = createEmptyTree (3 );
117
118
addUndirectedEdge (graph4 , 0 , 1 );
118
119
addUndirectedEdge (graph4 , 1 , 2 );
119
- System .out .println (treeCenter (graph4 )+ "\n " );
120
+ System .out .println (findTreeCenters (graph4 ) + "\n " );
120
121
121
122
// Centers are 1,2
122
123
List <List <Integer >> graph5 = createEmptyTree (4 );
123
124
addUndirectedEdge (graph5 , 0 , 1 );
124
125
addUndirectedEdge (graph5 , 1 , 2 );
125
126
addUndirectedEdge (graph5 , 2 , 3 );
126
- System .out .println (treeCenter (graph5 )+ "\n " );
127
+ System .out .println (findTreeCenters (graph5 ) + "\n " );
127
128
128
129
// Centers are 2,3
129
130
List <List <Integer >> graph6 = createEmptyTree (7 );
@@ -133,6 +134,6 @@ public static void main(String[] args) {
133
134
addUndirectedEdge (graph6 , 3 , 4 );
134
135
addUndirectedEdge (graph6 , 4 , 5 );
135
136
addUndirectedEdge (graph6 , 4 , 6 );
136
- System .out .println (treeCenter (graph6 )+ "\n " );
137
+ System .out .println (findTreeCenters (graph6 ) + "\n " );
137
138
}
138
139
}
0 commit comments