Question: I need some help with this assignment please! I don't understand it. Complete A4BST.printInLevelOrder() method - 3pts Complete A4BST.visuallyIdentical() method - 3pts A4BST.java /* *
I need some help with this assignment please! I don't understand it.
Complete A4BST.printInLevelOrder() method - 3pts
Complete A4BST.visuallyIdentical() method - 3pts
A4BST.java
/*
* Complete the printInLevelOrder() method
* Complete the visuallyIdentical(A4BST rhs) method
*/
public class A4BST
/*
* Grading:
* Correctly prints values in level order - 1.5pt
* Runs in O(N) - 1.5pt
*/
public String printInLevelOrder()
{
String content = "";
/*
* Add items from the tree to content one level at a time
* No line breaks are required between levels
* Ensure method runs in O(N) - does not revisit any node
*/
return content;
}
/*
* Grading:
* Correctly compares the structure of both trees - 3pts
*/
public boolean visuallyIdentical(A4BST
{
/*
* Check if the structure of the local tree and the rhs tree are identical
* This means they have the same left/right connections
* This means there are no extra connections in either tree
* The values at each position do not need to match, only the structure of the tree
* Think about if you drew both trees on paper, would they visually look the same (besides values)
*/
return false;
}
private Node root;
public A4BST()
{
root = null;
}
public String printTree()
{
return printTree(root);
}
private String printTree(Node current)
{
String content = "";
if(current != null)
{
content += "Current:"+current.data.toString();
if(current.left != null)
{
content += "; Left side:"+current.left.data.toString();
}
if(current.right != null)
{
content += "; Right side:"+current.right.data.toString();
}
content+=" ";
content+=printTree(current.left);
content+=printTree(current.right);
}
return content;
}
public String printInOrder()
{
return printInOrder(root);
}
private String printInOrder(Node current)
{
String content = "";
if(current != null)
{
content += printInOrder(current.left);
content += current.data.toString()+",";
content += printInOrder(current.right);
}
return content;
}
public boolean contains(E val)
{
Node result = findNode(val, root);
if(result != null)
return true;
else
return false;
}
private Node findNode(E val, Node current)
{
//base cases
if(current == null)
return null;
if(current.data.equals(val))
return current;
//recursive cases
int result = current.data.compareTo(val);
if(result < 0)
return findNode(val, current.right);
else
return findNode(val, current.left);
}
public E findMin()
{
Node result = findMin(root);
if(result == null)
return null;
else
return result.data;
}
private Node findMin(Node current)//used in findMin and delete
{
while(current.left != null)
{
current = current.left;
}
return current;
}
public E findMax()
{
Node current = root;
while(current.right != null)
{
current = current.right;
}
return current.data;
}
public void insert(E val)
{
root = insertHelper(val, root);
}
public Node insertHelper(E val, Node current)
{
if(current == null)
{
return new Node(val);
}
int result = current.data.compareTo(val);
if(result < 0)
{
current.right = insertHelper(val, current.right);
}
else if(result > 0)
{
current.left = insertHelper(val, current.left);
}
else//update
{
current.data = val;
}
return current;
}
public void remove(E val)
{
root = removeHelper(val, root);
}
private Node removeHelper(E val, Node current)
{
if(current.data.equals(val))
{
if(current.left == null && current.right == null)//no children
{
return null;
}
else if(current.left != null && current.right != null)//two children
{
Node result = findMin(current.right);
result.right = removeHelper(result.data, current.right);
result.left = current.left;
return result;
}
else//one child
{
return (current.left != null)? current.left : current.right;
}
}
int result = current.data.compareTo(val);
if(result < 0)
{
current.right = removeHelper(val, current.right);
}
else if(result > 0)
{
current.left = removeHelper(val, current.left);
}
return current;
}
private class Node
{
E data;
Node left, right;
public Node(E d)
{
data = d;
left = null;
right = null;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
A4Driver.java
public class A4Driver {
public static void main(String[] args) {
A4BST
tree1.insert(5);
tree1.insert(3);
tree1.insert(1);
tree1.insert(2);
tree1.insert(9);
tree1.insert(10);
tree1.insert(25);
A4BST
tree2.insert(8);
tree2.insert(5);
tree2.insert(1);
tree2.insert(3);
tree2.insert(15);
tree2.insert(20);
tree2.insert(25);
A4BST
tree3.insert(1);
tree3.insert(2);
tree3.insert(3);
tree3.insert(5);
tree3.insert(9);
tree3.insert(10);
tree3.insert(25);
System.out.println("Level Order");
System.out.println(tree1.printInLevelOrder().equals("5,3,9,1,10,2,25,"));//5, 3, 9, 1, 10, 2, 25
System.out.println(tree2.printInLevelOrder().equals("8,5,15,1,20,3,25,"));//8, 5, 15, 1, 20, 3, 25
System.out.println(tree3.printInLevelOrder().equals("1,2,3,4,5,9,10,25,"));//1, 2, 3, 4, 5, 9, 10, 25
System.out.println(" Visually Identical");
System.out.println(tree1.visuallyIdentical(tree2) == true);//true
System.out.println(tree1.visuallyIdentical(tree3) == false);//false
System.out.println(" Populate Methods");
String infix = "(a+b)";
String postfix = "ab+";
String prefix = "+ab";
System.out.println("Original:"+infix);
A4EquationTree eq1 = new A4EquationTree();
eq1.populateFromInfix(infix);
System.out.println(eq1.printInfix().equals(infix));
System.out.println(eq1.printPostfix().equals(postfix));
System.out.println(eq1.printPrefix().equals(prefix));
System.out.println("Original:"+postfix);
A4EquationTree eq2 = new A4EquationTree();
eq2.populateFromPostfix(postfix);
System.out.println(eq2.printInfix().equals(infix));
System.out.println(eq2.printPostfix().equals(postfix));
System.out.println(eq2.printPrefix().equals(prefix));
System.out.println("Original:"+prefix);
A4EquationTree eq3 = new A4EquationTree();
eq3.populateFromPrefix(prefix);
System.out.println(eq3.printInfix().equals(infix));
System.out.println(eq3.printPostfix().equals(postfix));
System.out.println(eq3.printPrefix().equals(prefix));
infix = "((a+(b*c))+(((d*e)+f)*g))";
postfix = "abc*+de*f+g*+";
prefix = "++a*bc*+*defg";
System.out.println("Original:"+infix);
eq1 = new A4EquationTree();
eq1.populateFromInfix(infix);
System.out.println(eq1.printInfix().equals(infix));
System.out.println(eq1.printPostfix().equals(postfix));
System.out.println(eq1.printPrefix().equals(prefix));
System.out.println("Original:"+postfix);
eq2 = new A4EquationTree();
eq2.populateFromPostfix(postfix);
System.out.println(eq2.printInfix().equals(infix));
System.out.println(eq2.printPostfix().equals(postfix));
System.out.println(eq2.printPrefix().equals(prefix));
System.out.println("Original:"+prefix);
eq3 = new A4EquationTree();
eq3.populateFromPrefix(prefix);
System.out.println(eq3.printInfix().equals(infix));
System.out.println(eq3.printPostfix().equals(postfix));
System.out.println(eq3.printPrefix().equals(prefix));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
