import java.util.*; //For the LinkedList /** * TreeNode - A node within a tree structure that contains a Person as data and a linkedlist of children * @author David Smith **/ public class TreeNode { //////////////////////Instance variables/fields/attributes/////////////////////////// /**The Person data within the node**/ private Person data; /**the LinkedList of TreeNode representing the children**/ private LinkedList children; //////////////////////////////////Constructors/////////////////////////////////////// /** * Constructor * @param data the Person data for the node **/ public TreeNode(Person data) { this.data = data; children = new LinkedList(); } ////////////////////////////////Accessors/Getters///////////////////////////////////// /** * Retrieves the data * @return the Person in the node **/ public Person getData() { return data; } /** * Retrieves the LinkedList of TreeNode that are the children * @return the Person in the node **/ public LinkedList getChildren() { return children;} ///////////////////////////////Modifiers/Setters///////////////////////////////////// /** * Set the data to the incoming parameter * @param data the new data **/ public void setData(Person data) { this.data = data; } /** * Set the children list to the incoming parameter * @param children the new children list **/ public void setChildren(LinkedList children) { this.children = children; } ///////////////////////////////Other Methods///////////////////////////////////// /** * Add a new child to the children list * @param tn a new TreeNode child **/ public void addChild(TreeNode tn) { children.add(tn); } /** * Return a String representation of the TreeNode * @return a String representation of the TreeNode **/ public String toString() { String res = "TreeNode: " + data.toString(); for (int i = 0; i < children.size(); i++) { res += "\n" + children.get(i).getData(); } return res; } public static void main(String[] args) { TreeNode root = new TreeNode(new Person("fred","Fred",1982,-1,"Flossie","Mr")); root.addChild(new TreeNode(new Person("fred","Fred",1982,-1,"Flossie","Mr"))); root.addChild(new TreeNode(new Person("fred","Fred",1982,-1,"Flossie","Mr"))); root.addChild(new TreeNode(new Person("fred","Fred",1982,-1,"Flossie","Mr"))); System.out.println(root); } }