Develop a Java program to read the text description of trees and construct a Bin
ID: 3789231 • Letter: D
Question
Develop a Java program to read the text description of trees and construct a Binary 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.
Explanation / Answer
Answer:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package treeproject;
/**
*
* @author ELIXIR BLACK
*/
public interface Iterator {
public boolean hasNext();
public void remove();
public void next();
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package treeproject;
/**
*
* @author ELIXIR BLACK
*/
public class Node {
String data;
Node left;
Node right;
public Node()
{
data = "null";
}
public Node(String d)
{
data = d;
}
public void setLeft(Node n)
{
left = n;
}
public void setRight(Node n)
{
right = n;
}
public String getData()
{
return data;
}
public Node getLeft()
{
return left;
}
public Node getRight()
{
return right;
}
public String getString()
{
return getData()+" Left Child: "+getLeft()+" Right Child: "+getRight();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package treeproject;
/**
*
* @author ELIXIT BLACK
*/
public class Tree implements Iterator {
public Node root;
public Node parent;
public Node focusNode;
public String data;
public Tree(String d)
{
data = d;
}
public void setData(String dat)
{
data = dat;
}
public String getData()
{
return data;
}
public void addNode()
{
Node newNode = new Node(getData());
if(root.data.equalsIgnoreCase("null"))
{
root = newNode;
}
else
{
focusNode = root;
parent = null;
while(true)
{
if(data.equals(focusNode.data))
{
focusNode = focusNode.left;
if(focusNode.data.equals("null"))
{
parent.left = newNode;
return;
}
else
{
focusNode = focusNode.right;
if(focusNode.data.equals("null"))
{
parent.right = newNode;
return;
}
}
}
}
}
}
public String preOrderTraversal(Node n)
{
preOrderTraversal(focusNode.left);
preOrderTraversal(focusNode.right);
return focusNode.data;
}
@Override
public boolean hasNext()
{
if(root!=null)
return true;
return false;
}
@Override
public void remove() {
}
@Override
public void next() {
System.out.println(root.data);
}
public String Iterator()
{
return preOrderTraversal(focusNode);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package treeproject;
/**
*
* @author ELIXIR BLACK
*/
public class TreeProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Tree tree = new Tree(args[0]);
tree.addNode();
tree.Iterator();
/*for(Iterator e = tree.Iterator(); e.hasNext())
{
}*/
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.