Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Java program to read the text description of trees and construct a tre

ID: 3792083 • Letter: D

Question

Develop a Java program to read the text description of trees and construct a tree. Your class Tree should provide implement the Iterable interface. The iterable interface requires that the class return an Iterator object of a class that implements the Iterator interface: Interface Iterator { boolean hasNext (); void remove() E next(); } The Iterator will return elements on the Tree using a PREORDER traversal. main program – used to test the Tree class: 1. Constructs tree using its string specification supplied as args[0] 2. Will output with result of a preorder traversal of the tree by calling the Iterator interface. static void main (String[] args) { Tree tree = new Tree (args[0]); for (Iterator e = tree.iterator (); e.hasNext();) { System.out.println(e.next()); } } Your program should perform error checking. That is, it should print an error message and exit if the tree specification is incorrect. or any of the subtrees specification is bad. Submit your program, and a report providing a time and space complexity analysis of your (i) Tree Building and (2) Traversing algorithms.

Explanation / Answer

There are a couple of tree data structures in Java, such as Default MutableTreeNode in JDK Swing, Tree in Stanford parser package, and other toy codes. But none of these are sufficient yet small enough for general purpose.

Java tree project attempts to provide another general-purpose tree data structure in Java.

The difference between this and others are:

Totally free. We can use it anywhere .
Small but general enough. I put everything of the data structure in one class file, so it would be easy to copy/paste.
Not just a toy, dozens of Java tree codes that can only handle binary trees or limited operations. This TreeNode is much more than that. It provides different ways of visiting nodes, such as preorder, postorder, breadthfirst, leaves, path to root, etc. Moreover, iterators are provided too for the sufficiency.


Iterable tree structure:

public class TreeNode<T> implements Iterable<TreeNode<T>> {

T data;

TreeNode<T> parent; List<TreeNode<T>> children;

public TreeNode(T data) {

this.data = data;

this.children = new LinkedList<TreeNode<T>>();

}

public TreeNode<T> addChild(T child) {

TreeNode<T> childNode = new TreeNode<T>(child);

childNode.parent = this;

this.children.add(childNode);

return childNode; }

// other features ... }

usage:

TreeNode<String> root = new TreeNode<String>("root");

{ TreeNode<String> node0 = root.addChild("node0");

TreeNode<String> node1 = root.addChild("node1");

TreeNode<String> node2 = root.addChild("node2");

{

TreeNode<String> node20 = node2.addChild(null);

TreeNode<String> node21 = node2.addChild("node21");

{

TreeNode<String> node210 = node20.addChild("node210");

} } }