Question: Complete the inOrderPrint(),preOrderPrint(), and postOrderPrint() methods so that each provides the correct argument to printTree() so that the print statement used generates an in-order, pre-order,
Complete the inOrderPrint(),preOrderPrint(), and postOrderPrint() methods so that each provides the correct argument to printTree() so that the print statement used generates an in-order, pre-order, and post-order traversal, respectively.
I do rate the answer so please do it seriously don't just copy and paste some code which is totally NOT RELEATED to this question. Thank You.
Entry
package edu.buffalo.cse116;
public class Entry
/** Left child of the current Node. */ private Entry
/** * Initializes this Entry object. This default constructor is defined for future expansion purposes. */ public Entry() {}
/** * Recursively prints out the elements in the binary tree. The parameter is used to control whether this printout will * use an pre-order, in-order, or post-order ordering. * * @param printLocation Integer 1, 2, or 3 to specify which print location should be used. */ public void printTree(int printLocation) { if (printLocation == 1) { System.out.print(element); } if (left != null) { left.printTree(printLocation); } if (printLocation == 2) { System.out.print(element); } if (right != null) { right.printTree(printLocation); } if (printLocation == 3) { System.out.print(element); } }
/** * Initializes this Entry object from element and parent. */ public Entry(E element, Entry
/** 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
/** Specify a node to be the left child of the current node. */ public void setLeft(Entry
/** Get the node's right child. */ public Entry
/** Specify a node to be the right child of the current node. */ public void setRight(Entry
/** Get the node's parent in the tree. This is null if the node is a root. */ public Entry
/** Specify a node to be the parent of the current node. */
public void setParent(Entry
}
BinaryTree(need to complete)
package edu.buffalo.cse116;
import java.util.AbstractSet; import java.util.Iterator;
public class BinaryTree
protected Entry
protected 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; }
/** * Returns the size of this BinarySearchTree object. * * @return the size of this BinarySearchTree object. */ @Override public int size() { return size; }
// Specify where we would need to do the processing in each method to generate the ordering specified. // THESE METHODS ARE LISTED IN ALPHABETICAL ORDER AND MAY OR MAY NOT REFLECT THE REQUIRED LOCATION VALUE public void inOrderPrint() { }
public void postOrderPrint() { root.printTree(); }
public void preOrderPrint() { root.printTree(); }
/** * Iterator method will be implemented for a future * * @return an iterator positioned at the smallest element in this * BinarySearchTree object. */ @Override public Iterator
} // class BinarySearchTree
Test
package edu.buffalo.cse116;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.PrintStream;
import org.junit.Before; import org.junit.Test;
public class TraversalOrderingTest {
private OutputStream bytesPrinted; private BinaryTree
@SuppressWarnings("unchecked") @Before public void setUp() { bytesPrinted = new ByteArrayOutputStream(); PrintStream sysOut = new PrintStream(bytesPrinted); System.setOut(sysOut);
Entry
// Setup the left and right children in the tree nodes[5].setLeft(nodes[3]); nodes[5].setRight(nodes[4]);
nodes[4].setLeft(nodes[10]);
nodes[2].setLeft(nodes[0]); nodes[2].setRight(nodes[1]);
nodes[6].setLeft(nodes[2]); nodes[6].setRight(nodes[5]);
nodes[8].setRight(nodes[9]);
nodes[7].setLeft(nodes[6]); nodes[7].setRight(nodes[8]);
// Setup the parents in the tree nodes[6].setParent(nodes[7]); nodes[8].setParent(nodes[7]); nodes[9].setParent(nodes[8]); nodes[5].setParent(nodes[6]); nodes[2].setParent(nodes[6]); nodes[0].setParent(nodes[2]); nodes[1].setParent(nodes[2]); nodes[10].setParent(nodes[4]); nodes[3].setParent(nodes[5]); nodes[4].setParent(nodes[5]);
bt = new BinaryTree<>(); bt.root = nodes[7]; bt.size = 11; }
@Test public void testPreOrder() { bt.preOrderPrint(); String str = bytesPrinted.toString(); String actual = "RQMKLPNOUST"; assertEquals("Pre-ordering incorrect", actual, str); }
@Test public void testPostOrder() { bt.postOrderPrint(); String str = bytesPrinted.toString(); String actual = "KLMNUOPQTSR"; assertTrue("Post-ordering incorrect", str.equals(actual)); }
@Test public void testInOrder() { bt.inOrderPrint(); String str = bytesPrinted.toString(); String actual = "KMLQNPUORST"; assertEquals("In-ordering incorrect", actual, str); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
