// BinaryNode class for Assignment 5. Add the methods specified in the // assignment sheet so that this class works correctly. package MyTreePackage; /** A class that represents nodes in a binary tree. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public class BinaryNode { private T data; private BinaryNode leftChild; // Reference to left child private BinaryNode rightChild; // Reference to right child public BinaryNode() { this(null); // Call next constructor } public BinaryNode(T dataPortion) { this(dataPortion, null, null); // Call next constructor } public BinaryNode(T dataPortion, BinaryNode newLeftChild, BinaryNode newRightChild) { data = dataPortion; leftChild = newLeftChild; rightChild = newRightChild; } /** Retrieves the data portion of this node. @return The object in the data portion of the node. */ public T getData() { return data; } /** Sets the data portion of this node. @param newData The data object. */ public void setData(T newData) { data = newData; } /** Retrieves the left child of this node. @return The node�s left child. */ public BinaryNode getLeftChild() { return leftChild; } /** Sets this node�s left child to a given node. @param newLeftChild A node that will be the left child. */ public void setLeftChild(BinaryNode newLeftChild) { leftChild = newLeftChild; } /** Detects whether this node has a left child. @return True if the node has a left child. */ public boolean hasLeftChild() { return leftChild != null; } /** Retrieves the right child of this node. @return The node�s right child. */ public BinaryNode getRightChild() { return rightChild; } /** Sets this node�s right child to a given node. @param newRightChild A node that will be the right child. */ public void setRightChild(BinaryNode newRightChild) { rightChild = newRightChild; } /** Detects whether this node has a right child. @return True if the node has a right child. */ public boolean hasRightChild() { return rightChild != null; } /** Detects whether this node is a leaf. @return True if the node is a leaf. */ public boolean isLeaf() { return (leftChild == null) && (rightChild == null); } /** Counts the nodes in the subtree rooted at this node. @return The number of nodes in the subtree rooted at this node. */ public int getNumberOfNodes() { int leftNumber = 0; int rightNumber = 0; if (leftChild != null) leftNumber = leftChild.getNumberOfNodes(); if (rightChild != null) rightNumber = rightChild.getNumberOfNodes(); return 1 + leftNumber + rightNumber; } /** Computes the height of the subtree rooted at this node. @return The height of the subtree rooted at this node. */ public int getHeight() { return getHeight(this); // Call private getHeight } private int getHeight(BinaryNode node) { int height = 0; if (node != null) height = 1 + Math.max(getHeight(node.getLeftChild()), getHeight(node.getRightChild())); return height; } /** Copies the subtree rooted at this node. @return The root of a copy of the subtree rooted at this node. */ public BinaryNode copy() { BinaryNode newRoot = new BinaryNode<>(data); if (leftChild != null) newRoot.setLeftChild(leftChild.copy()); if (rightChild != null) newRoot.setRightChild(rightChild.copy()); return newRoot; } // ********************************** // Complete the additional methods below // ********************************** /* If the tree is a full tree, return true, otherwise return false. */ public boolean isFull() { } /* Return true if: * 1) the difference in height between the left and right subtrees is * at most k, and * 2) the left and right subtrees are both recursively k-balanced; * return false otherwise */ publc boolean isBalanced(int k) { } }