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

 import java.util.*; public class PoD { public static void main( String

[] args ) { Scanner in = new Scanner( System.in ); String[]

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 children;

/** 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(); } }

}

You are going to finish off Tree.java to complete the leafCount) method of the Node class. Input You will note that most of the Tree class and, within it, the Node class have been implemented for you. In PoD,java, we have read in details to create a Tree with several nodes Processing You are going to complete the details the leafCount() method. Details of what this method should do follows: For a given node N If N is a leaf, the leaf count is 1 Otherwise Let C1, . . . , Cn be the children of N The leaf count is leafCount(ci)+... +lea fCount(C) Your method will return this leaf count. Output Output will be handled by the main method and has already been taken care of for you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!