For this assignment you must take the given BinaryTree class
For this assignment you must take the given BinaryTree<E> class and complete the
code for the following methods:
int countLeaves ( );
boolean equals ( BinaryTree<E> otherTree );
void makeMirror( );
This is The code:
public class BinaryTree < E > {
/** Class to encapsulate a tree node. */
public static class Node < E > {
// Data Fields
/** The information stored in this node. */
public E data;
/** Reference to the left child. */
public Node < E > left;
/** Reference to the right child. */
public Node < E > right;
/** Construct a node with given data and no children.
@param data The data to store in this node
*/
public Node(E data) {
this.data = data;
left = null;
right = null;
}
/** Return a string representation of the node.
* @return A string representation of the data fields
*/
public String toString() {
return data.toString();
}
}
/** The root of the binary tree */
public Node < E > root;
/** Constructs an empty tree, with no nodes at all. Size = 0
*/
public BinaryTree() {
root = null;
}
/** Constructs a binary tree that is a \"shallow copy\" of an existing tree
* @param root pointer to the root of the existing tree
*/
protected BinaryTree(Node< E > root) {
this.root = root;
}
/** Constructs a new binary tree with data in its root, leftTree
* as its left subtree and rightTree as its right subtree.
*/
public BinaryTree(E data, BinaryTree< E > leftTree, BinaryTree< E > rightTree) {
root = new Node< E > (data);
if (leftTree != null)
root.left = leftTree.root;
else
root.left = null;
if (rightTree != null)
root.right = rightTree.root;
else
root.right = null;
}
/** Return the data field of the root
* @return the data field of the root, or null if the root is null
*/
public E getRootData() {
if (root != null)
return root.data;
else
return null;
}
/** Determine whether this tree is a leaf.
* @return true if the root has no children
*/
public boolean is_Tree_a_Leaf() {
return (root.left == null && root.right == null);
}
public String toString() {
StringBuilder sb = new StringBuilder();
inOrderTraverse(root, sb);
return sb.toString();
}
private void preOrderTraverse(Node < E > currPos, StringBuilder sb) {
if (currPos == null) { // BASE CASE
;
}
else { // GENERAL CASE
sb.append( currPos.toString() + \" \" );
preOrderTraverse(currPos.left, sb);
preOrderTraverse(currPos.right, sb);
}
int foo = 3; // dummy code to prevent tail recursion
}
private void postOrderTraverse(Node < E > currPos, StringBuilder sb) {
if (currPos == null) {
;
}
else {
postOrderTraverse(currPos.left, sb);
postOrderTraverse(currPos.right, sb);
sb.append(currPos.toString() + \" \");
}
}
private void inOrderTraverse(Node < E > currPos, StringBuilder sb) {
if (currPos == null) {
;
}
else {
inOrderTraverse(currPos.left, sb);
sb.append(currPos.toString() + \" \");
inOrderTraverse(currPos.right, sb);
}
}
/** Perform a traversal.
* @return the number of nodes in the entire tree
*/
public int size ( ) {
return( size( root ) );
}
/** Perform a preorder traversal.
* @param currPos The local root
* @return the number of nodes below the given local root
*/
private int size ( Node < E > currPos ) {
if (currPos == null) {
return( 0 );
}
else {
int leftCount = size ( currPos.left );
int rightCount = size ( currPos.right );
return( leftCount + rightCount + 1 );
}
}
public boolean contains( E target ) {
return( contains ( root, target ) );
}
private boolean contains( Node< E > currPos, E target ) {
if (currPos == null) {
return( false );
}
if ( currPos.data.equals( target ) ){
return( true );
}
boolean result = contains ( currPos.left, target );
if ( result )
return true;
else
return ( contains ( currPos.right, target ) );
}
/** Perform a traversal.
* @return the number of leaves in the tree
*/
public int countLeaves ( ) {
return -99;
}
/**Determine whether this tree is identical to the otherTree
* @param otherTree the tree to compare against
* @return true if the two trees are identical. The shape of
* the two trees must be the same, and the data at each node
* must be the same.
*/
public boolean equals ( BinaryTree<E> otherTree ) {
return false;
}
/** Transform the tree into its mirror image
*/
public void makeMirror ( ) {
}
}
Solution
public void mirror(Node root){
print(root);
Node z = mirrorTree(root);
System.out.print(\"\ This is the Mirror Image \");
print(z);
int countLeaves(Node node){
if( node == null )
return 0;
if( node.left == null && node.right == null ) {
return 1;
} else {
return countLeaves(node.left) + countLeaves(node.right);
}
}
BinaryTree<E> tone = (BinaryTree<E>) other;
if (hasLeft() != tone.hasLeft() || hasRight() != tone.hasRight()) return false;
if (!data.equals(tone.data)) return false;
if (hasLeft() && !left.equals(tone.left)) return false;
if (hasRight() && !right.equals(tone.right)) return false;
return true;
}
| public void mirror(Node root){ |





