import
java.util.*;
public
class
Main
{
static
Vector<Vector<Integer>> ans =
new
Vector<Vector<Integer>>();
static
class
Node {
public
int
val;
public
Vector<Node> child;
public
Node(
int
key)
{
val = key;
child =
new
Vector<Node>();
}
}
static
Node newNode(
int
key)
{
Node temp =
new
Node(key);
return
temp;
}
static
Vector<Integer> UtilityFun(Node root, Vector<Vector<Integer>> ans)
{
if
(root ==
null
)
{
Vector<Integer> temp =
new
Vector<Integer>();
temp.add(
0
);
temp.add(
0
);
return
temp;
}
int
totalNodes =
1
, totalLevels =
1
;
for
(
int
i =
0
; i < root.child.size(); i++) {
Vector<Integer> info = UtilityFun(root.child.get(i), ans);
totalNodes += info.get(
0
);
totalLevels = Math.max(totalLevels,
1
+ info.get(
1
));
}
int
currentAverageWidth = totalNodes / totalLevels;
Vector<Integer> temp =
new
Vector<Integer>();
temp.add(root.val);
temp.add(currentAverageWidth);
ans.add(temp);
temp =
new
Vector<Integer>();
temp.add(totalNodes);
temp.add(totalLevels);
return
temp;
}
static
Vector<Vector<Integer>> findAvgWidth(Node root)
{
if
(root ==
null
)
return
new
Vector<Vector<Integer>>();
UtilityFun(root, ans);
return
ans;
}
static
void
display(Vector<Vector<Integer>> ans)
{
for
(
int
i =
0
; i < ans.size(); i++) {
System.out.print(ans.get(i).get(
0
) +
":"
+ ans.get(i).get(
1
) +
", "
);
}
}
public
static
void
main(String[] args)
{
Node root = newNode(
1
);
(root.child).add(newNode(
2
));
(root.child).add(newNode(
3
));
(root.child.get(
0
).child).add(newNode(
4
));
(root.child.get(
1
).child).add(newNode(
5
));
(root.child.get(
0
).child).add(newNode(
6
));
(root.child.get(
1
)).child.add(newNode(
7
));
(root.child.get(
1
).child).add(newNode(
8
));
findAvgWidth(root);
display(ans);
}
}