Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.
1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree:
public class BinTreeNode<T>
{
private T key;
private Object satelliteData;
private BinTreeNode<T> parent;
private BinTreeNode<T> leftChild;
private BinTreeNode<T> rightChild;
public BinTreeNode<T>(<T> key, Object satelliteData); // constructor
…
}
supporting the following public methods:
public void addLeftChild(BinTreeNode leftChildNode);
public void addRightChild(BinTreeNode rightChildNode);
public void setParent(BinTreeNode parentNode);
public BinTreeNode<T> getParent();
public BinTreeNode<T> getLeftChild();
public BinTreeNode<T> getRightChild();
and
public class BinTree<T> {
private BinTreeNode<T> root;
…
}
supporting the following methods
public BinTreeNode<T> getRoot();
public void setRoot(BinTreeNode<T>);
2. Add method public boolean isBalanced() 2 to the class BinTree, which returns true if the tree is balanced, and false otherwise.
public boolean isBalanced() {
BinTreeNode root = getRoot();
if (root == null) {
throw new IllegalArgumentException(
"The tree root must be non null");
}
return maxDepth(root) - minDepth(root) <= 1;
}
private static int minDepth(BinTreeNode node) {
if (node == null) {
return 0;
}
return 1 + Math.min(minDepth(node.getLeftChild()), minDepth(node.getRightChild()));
}
private static int maxDepth(BinTreeNode node) {
if (node == null) {
return 0;
}
return 1 + Math.max(maxDepth(node.getLeftChild()), maxDepth(node.getRightChild()));
}
Please write a Java algorithm solving the following problem: Implement a Java method to check if...