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 { /** Tree's element which is stored within this Node. */ private E element;

/** Left child of the current Node. */ private Entry left; /** Right child of the current Node. */ private Entry right; /** Parent in the binary tree for the current Node. */ private Entry parent;

/** * 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 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 getLeft() { return left; }

/** Specify a node to be the left child of the current node. */ public void setLeft(Entry left) { this.left = left; }

/** Get the node's right child. */ public Entry getRight() { return right; }

/** Specify a node to be the right child of the current node. */ public void setRight(Entry right) { this.right = right; }

/** Get the node's parent in the tree. This is null if the node is a root. */ public Entry getParent() { return parent; }

/** Specify a node to be the parent of the current node. */

public void setParent(Entry parent) { this.parent = parent; }

}

BinaryTree(need to complete)

package edu.buffalo.cse116;

import java.util.AbstractSet; import java.util.Iterator;

public class BinaryTree extends AbstractSet {

protected Entry root;

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 iterator() { throw new UnsupportedOperationException("Not implemented yet!"); }

} // 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 bt;

@SuppressWarnings("unchecked") @Before public void setUp() { bytesPrinted = new ByteArrayOutputStream(); PrintStream sysOut = new PrintStream(bytesPrinted); System.setOut(sysOut);

Entry[] nodes = new Entry[11]; for (int i = 0; i < nodes.length; i++ ) { nodes[i] = new Entry<>(); nodes[i].setElement((char) ('K' + i)); }

// 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

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!