Complete BinaryTree\'s visit() and Entry\'s apply() method so that we could proc
ID: 3818515 • Letter: C
Question
Complete BinaryTree's visit() and Entry's apply() method so that we could process the tree using the Visitor pattern. Please complete the highlighted methods using the code below. Code must pass test for binary tree, test that the node is interior to both children, test node is leaf, and test node is interior to one child. Please complete: public String visit(TreeVisitor<E> visitor) {} in Binary Tree File and public String apply(TreeVisitor<E> visitor, String data) {} in Entry file.
Binary Tree File
import java.util.AbstractCollection;
import java.util.Iterator;
/**
* Instances of this class represent a vanilla binary tree (e.g., and not a binary search tree).
*
* @author Matthew Hertz
* @param <E> Type of data stored in each node of this tree. Since this is not a BST, E can be anything.
*/
public class BinaryTree<E> extends AbstractCollection<E> {
/** Root node of this binary tree */
private Entry<E> root;
/** Number of nodes/elements within this binary tree. */
private int size;
/**
* Initializes this BinarySearchTree object to be empty, to contain only
* elements of type E, to be ordered by the Comparable interface, and to
* contain no duplicate elements.
*/
public BinaryTree() {
root = null;
size = 0;
}
public String visit(TreeVisitor<E> visitor) {
}
/**
* Returns the size of this BinarySearchTree object.
*
* @return the size of this BinarySearchTree object.
*/
@Override
public int size() {
return size;
}
/**
* Iterator method that has, at last, been implemented.
*
* @return an iterator positioned at the smallest element in this
* BinarySearchTree object.
*/
@Override
public Iterator<E> iterator() {
// Skipped until next week
throw new UnsupportedOperationException();
}
/**
* Determines if there is at least one element in this binary tree object that equals a specified element.
*
* @param obj - the element sought in this binary tree
* @return true if the object is an element in the binary tree; false othrwise.
*/
@Override
public boolean contains(Object obj) {
return getEntry(obj) != null;
}
/**
* Finds the BTNode object that houses a specified element, if there is such an
* BTNode.
*
* @param obj - the element whose BTNode is sought.
* @return the BTNode object that houses obj - if there is such an BTNode;
* otherwise, return null.
*/
protected Entry<E> getEntry(Object obj) {
return getEntry(root, obj);
}
/**
* Finds the BTNode object that houses a specified element in the subtree rooted at subtreeRoot, if there is such an
* BTNode.
*
* @param obj - the element whose BTNode is sought.
* @return the BTNode object that houses obj - if there is such an BTNode in the subtree at subtreeRoot;
* otherwise, return null.
*/
protected Entry<E> getEntry(Entry<E> subtreeRoot, Object obj) {
if (subtreeRoot == null) {
return null;
} else if ((subtreeRoot.getElement() == obj) ||
((subtreeRoot.getElement() != null) && subtreeRoot.getElement().equals(obj))) {
return subtreeRoot;
}
if (subtreeRoot.getLeft() != null) {
Entry<E> retVal = getEntry(subtreeRoot.getLeft(), obj);
if (retVal != null) {
return retVal;
}
}
if (subtreeRoot.getRight() != null) {
Entry<E> retVal = getEntry(subtreeRoot.getRight(), obj);
if (retVal != null) {
return retVal;
}
}
return null;
}
/**
* Finds the successor of a specified Entry object in this BinarySearchTree.
* The worstTime(n) is O(n) and averageTime(n) is constant.
*
* @param e - the Entry object whose successor is to be found.
* @return the successor of e, if e has a successor; otherwise, return null.
*/
protected Entry<E> successor(Entry<E> e) {
if (e == null) {
return null;
} else if (e.getRight() != null) {
// successor is leftmost Entry in right subtree of e
Entry<E> p = e.getRight();
while (p.getLeft() != null) {
p = p.getLeft();
}
return p;
} // e has a right child
else {
// go up the tree to the left as far as possible, then go up
// to the right.
Entry<E> p = e.getParent();
Entry<E> ch = e;
while ((p != null) && (ch == p.getRight())) {
ch = p;
p = p.getParent();
} // while
return p;
} // e has no right child
} // method successor
}
Entry File
public class Entry<E> {
/** Tree's element which is stored within this Node. */
private E element;
/** Left child of the current Node. */
private Entry<E> left;
/** Right child of the current Node. */
private Entry<E> right;
/** Parent in the binary tree for the current Node. */
private Entry<E> parent;
/**
* Initializes this Entry object. This default constructor is defined for future expansion purposes.
*/
public Entry() {}
/**
* Initializes this Entry object from element and parent.
*/
public Entry(E element, Entry<E> parent) {
this.element = element;
this.parent = parent;
}
/** Return the element stored in this node. */
public E getElement() {
return element;
}
/** Specify a new element to be stored at this node. */
public void setElement(E element) {
this.element = element;
}
/** Get the node's left child. */
public Entry<E> getLeft() {
return left;
}
/** Specify a node to be the left child of the current node. */
public void setLeft(Entry<E> left) {
this.left = left;
}
/** Get the node's right child. */
public Entry<E> getRight() {
return right;
}
/** Specify a node to be the right child of the current node. */
public void setRight(Entry<E> right) {
this.right = right;
}
/** Get the node's parent in the tree. This is null if the node is a root. */
public Entry<E> getParent() {
return parent;
}
/** Specify a node to be the parent of the current node. */
public void setParent(Entry<E> parent) {
this.parent = parent;
}
/**
* Implement the node's portion of the Visitor pattern. This allows the TreeVisitor to work and return the result of
* the traversal.
*
* @param visitor TreeVisitor instance doing the work.
* @param data Data being passed along as part of this visiting traveral.
* @return The result returned by the TreeVisitor.
*/
public String apply(TreeVisitor<E> visitor, String data) {
}
}
Package also contains:
public interface TreeVisitor<E> {
public String visitLeaf(Entry<E> leaf, String message);
public String visitInterior(Entry<E> node, String message);
}
Explanation / Answer
import java.util.Scanner;
/* Class BTNode */
class BTNode
{
BTNode left, right;
int information;
/* Constructor */
open BTNode()
{
left = invalid;
right = invalid;
information = 0;
}
/* Constructor */
open BTNode(int n)
{
left = invalid;
right = invalid;
information = n;
}
/* Function to set left hub */
open void setLeft(BTNode n)
{
left = n;
}
/* Function to set right hub */
open void setRight(BTNode n)
{
right = n;
}
/* Function to get left hub */
open BTNode getLeft()
{
return left;
}
/* Function to get right hub */
open BTNode getRight()
{
return right;
}
/* Function to set information to hub */
open void setData(int d)
{
information = d;
}
/* Function to get information from hub */
open int getData()
{
return information;
}
}
/* Class BT */
class BT
{
private BTNode root;
/* Constructor */
open BT()
{
root = invalid;
}
/* Function to check if tree is unfilled */
open boolean isEmpty()
{
return root == invalid;
}
/* Functions to embed information */
open void insert(int information)
{
root = insert(root, information);
}
/* Function to embed information recursively */
private BTNode insert(BTNode hub, int information)
{
on the off chance that (hub == invalid)
hub = new BTNode(data);
else
{
on the off chance that (node.getRight() == invalid)
node.right = insert(node.right, information);
else
node.left = insert(node.left, information);
}
return hub;
}
/* Function to tally number of hubs */
open int countNodes()
{
return countNodes(root);
}
/* Function to tally number of hubs recursively */
private int countNodes(BTNode r)
{
on the off chance that (r == invalid)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to scan for a component */
open boolean search(int val)
{
return search(root, val);
}
/* Function to scan for a component recursively */
private boolean search(BTNode r, int val)
{
on the off chance that (r.getData() == val)
return genuine;
on the off chance that (r.getLeft() != invalid)
on the off chance that (search(r.getLeft(), val))
return genuine;
on the off chance that (r.getRight() != invalid)
on the off chance that (search(r.getRight(), val))
return genuine;
return false;
}
/* Function for inorder traversal */
open void inorder()
{
inorder(root);
}
private void inorder(BTNode r)
{
on the off chance that (r != invalid)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
open void preorder()
{
preorder(root);
}
private void preorder(BTNode r)
{
on the off chance that (r != invalid)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
open void postorder()
{
postorder(root);
}
private void postorder(BTNode r)
{
in the event that (r != invalid)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
/* Class BinaryTree */
open class BinaryTree
{
open static void main(String[] args)
{
Scanner filter = new Scanner(System.in);
/* Creating object of BT */
Bt = new BT();
/* Perform tree operations */
System.out.println("Binary Tree Test ");
scorch ch;
do
{
System.out.println(" Binary Tree Operations ");
System.out.println("1. embed ");
System.out.println("2. seek");
System.out.println("3. number hubs");
System.out.println("4. check discharge");
int decision = scan.nextInt();
switch (decision)
{
case 1 :
System.out.println("Enter whole number component to embed");
bt.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter whole number component to look");
System.out.println("Search result : "+ bt.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ bt.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ bt.isEmpty());
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display tree */
System.out.print(" Post arrange : ");
bt.postorder();
System.out.print(" Pre arrange : ");
bt.preorder();
System.out.print(" In arrange : ");
bt.inorder();
System.out.println(" Do you need to proceed with (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.