Question: Java Program Help: I have a LinkedBinaryTree class that will follow the question. In that class, Add a method named eulerTourBinary as described on page

Java Program Help: I have a LinkedBinaryTree class that will follow the question. In that class, Add a method named eulerTourBinary as described on page 349 of the textbook. Write this method so that it will print out a traditional parenthesized arithmetic expression. I don't understand because it's not a code fragment.

Your program should: - Ask the user to enter the absolute path and filename (as a single String) of the file that contains a list of arithmetic expressions. Each expression will be on a single line in the input text file delimited by and end of line character. - Read arithmetic expressions from an input file until the EOF is reached. o See file format and example at end of assignment. - For each expression your program should: o Print out the expression that was read from the file. o Determine if the expression is valid. Print an invalid expression message for invalid expressions. For each valid expression Represent the expression in a binary expression tree Evaluate the expression and display the results of the evaluation Display the contents of the binary expression tree using: o A preorder traversal o An inorder traversal o A postorder traversal o The eulerTourBinary method that you added for this Lab Each traversal should be appropriately labelled and print out on a single line Summary, one LinkedBinaryTree class and one Client Class

Here's the LinkedBinaryTree Class:

import java.util.Iterator;

import java.util.LinkedList;

import java.util.Queue;

public class LinkedBinaryTree extends AbstractBinaryTree

{ static class Node implements Position

{ //declaring nodes and variables private E element;

private Node left;

private Node right;

private Node parent;

public Node(E e, Node above, Node leftChild, Node rightChild)

{ element = e; parent = above; left = leftChild; right = rightChild; }

//setters and getters for the elements of the tree

public E getElement()

{ return element; }

public Node getParent()

{ return parent; }

public Node getLeft()

{ return left; }

public Node getRight()

{ return right; }

public void setElement(E e)

{ element = e; }

public void setParent(Node parentNode)

{ parent = parentNode; }

public void setLeft(Node leftChild)

{ left = leftChild; }

public void setRight(Node rightChild)

{ right = rightChild; }

}

protected Node createNode(E e, Node parent,

Node left, Node right)

{

return new Node(e, parent, left, right); }

protected Node root = null;

private int size = 0;

public LinkedBinaryTree() { }

protected Node validate(Position p) throws IllegalArgumentException {

if (!(p instanceof Node))

{ throw new IllegalArgumentException("Not valid position type");

}

Node node = (Node) p;

if (node.getParent() == node) {

throw new IllegalArgumentException("p is no longer in the tree");

}

return node; }

public int size()

{ return size; }

@Override

public Iterator iterator()

{

return null; }

@Override

public Iterable> positions()

{ return null; }

public Position root()

{ return root; }

//methods declaring argument exceptions to maintain specific values in the trees

public Position parent(Position p) throws IllegalArgumentException

{ Node node = validate(p);

return node.getParent(); }

public Position left(Position p) throws IllegalArgumentException {

Node node = validate(p);

return node.getLeft(); }

public Position right(Position p) throws IllegalArgumentException {

Node node = validate(p);

return node.getRight();

} public Position addRoot(E e) throws IllegalStateException {

if (!isEmpty()) {

throw new IllegalStateException("Tree is not empty"); }

root = createNode(e, null, null, null);

size = 1; return root; }

//add method in order to add a left child to the tree

public Position addLeft(Position p, E e)

throws IllegalArgumentException {

Node parent = validate(p);

if (parent.getLeft() != null) {

throw new IllegalArgumentException("p already has a left child");

} Node child = createNode(e, parent, null, null);

parent.setLeft(child);

size++;

return child; }

//add method in order to add a right child to the tree

public Position addRight(Position p, E e) throws IllegalArgumentException {

Node parent = validate(p);

if (parent.getRight() != null) {

throw new IllegalArgumentException("p already has a right child");

}

Node child = createNode(e, parent, null, null);

parent.setRight(child);

size++;

return child; }

public E set(Position p, E e) throws IllegalArgumentException

{ Node node = validate(p);

E temp = node.getElement();

node.setElement(e);

return temp;

}

public void attach(Position p, LinkedBinaryTree t1,

LinkedBinaryTree t2) throws IllegalArgumentException {

Node node = validate(p);

if (isInternal(p)) {

throw new IllegalArgumentException("p must be a leaf");

//indicating p as a leaf

}

size += t1.size() + t2.size();

if (!t1.isEmpty())

{ t1.root.setParent(node);

node.setLeft(t1.root);

t1.root = null;

t1.size = 0;

}

if (!t2.isEmpty()) {

t2.root.setParent(node);

node.setRight(t2.root);

t2.root = null;

t2.size = 0; } }

//removal method of a node from the tree traversal

public E remove(Position p) throws IllegalArgumentException {

Node node = validate(p);

if (numChildren(p) == 2) {

throw new IllegalArgumentException("p has two children");

}

Node child = (node.getLeft() != null ? node.getLeft() : node.getRight());

if (child != null) {

child.setParent(node.getParent());

}

if (node == root) {

root = child;

} else

{ Node parent = node.getParent()

; if (node == parent.getLeft())

{ parent.setLeft(child); } else

{ parent.setRight(child); }

}

size--;

E temp = node.getElement();

node.setElement(null);

node.setLeft(null);

node.setRight(null);

node.setParent(node);

return temp; }

//Tree Traversal Methods

//Defining inOrder Method

public void inOrder(Node root)

{ if (root != null) {

inOrder(root.left);

System.out.printf("%s ", root.getElement());

inOrder(root.right); } }

//Defining preOrder method

public void preOrder(Node root)

{ if (root != null)

{ System.out.printf("%s ", root.getElement());

inOrder(root.left);

inOrder(root.right); } }

//Defining postOrder Method

public void postOrder(Node root)

{ if (root != null) {

inOrder(root.left);

inOrder(root.right);

System.out.printf("%s ", root.getElement()); } }

//Defining BreathFirst method

public void BreathFirst(Node root)

{ Queue> queue = new LinkedList>();

if (root == null)

{ return; }

queue.clear();

queue.add(root);

while (!queue.isEmpty()) {

Node node = queue.remove();

System.out.print(node.element + " ");

if (node.left != null) {

queue.add(node.left); }

if (node.right != null)

{ queue.add(node.right); } } }

//parethesize method for traversal in client class

public void parenthesize(Tree T, Position p) {

System.out.print(p.getElement());

if (T.isInternal(p)) {

boolean firstTime = true;

for (Position c : T.children(p))

{ System.out.print((firstTime ? " (" : ", "));

firstTime = false;

parenthesize(T, c); }

System.out.print(")"); } } }

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!