Question: import java.util.*; public class PoD { public static void main( String [] args ) { Scanner in = new Scanner( System.in ); String[] familyMembers; Tree

![[] args ) { Scanner in = new Scanner( System.in ); String[]](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f3a2d3cba7f_96366f3a2d37dfb7.jpg)
import java.util.*;
public class PoD { public static void main( String [] args ) { Scanner in = new Scanner( System.in );
String[] familyMembers;
Tree grandparent = new Tree("Grandma"); Tree childA = new Tree("Child A"); Tree childB = new Tree("Child B"); Tree childC = new Tree("Child C");
grandparent.addSubtree(childA); grandparent.addSubtree(childB); grandparent.addSubtree(childC);
familyMembers = in.nextLine().split(","); for (int i=0; i System.out.println("Total family size: " + grandparent.size()); System.out.println("Number of grandchildren: " + grandparent.leafCount()); in.close(); System.out.print("END OF OUTPUT"); } } ================================================================================================== import java.util.List; import java.util.ArrayList; public class Tree { private Node root; class Node { public Object data; public List /** Computes the number of leaves of the subtree whose root is this node. @return the number of leaves in the subtree */ public int leafCount() { // ******************************************** // FINISH THIS METHOD // ******************************************** } /** Computes the size of the subtree whose root is this node. @return the number of nodes in the subtree */ public int size() { int sum = 0; for (Node child : children) { sum = sum + child.size(); } return 1 + sum; } } /** Constructs an empty tree. */ public Tree() { root = null; } /** Constructs a tree with one node and no children. @param rootData the data for the root */ public Tree(Object rootData) { root = new Node(); root.data = rootData; root.children = new ArrayList(); } /** Adds a subtree as the last child of the root. */ public void addSubtree(Tree subtree) { root.children.add(subtree.root); } /** Counts the number of leaves in the tree @return the number of leaves in the tree */ public int leafCount() { if (root == null) { return 0; } else { return root.leafCount(); } } /** Computes the size of this tree. @return the number of nodes in the tree */ public int size() { if (root == null) { return 0; } else { return root.size(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
