Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA JAVA clear and correct answer, any incorrect answers and spams will be repo

ID: 3782766 • Letter: J

Question

JAVA JAVA

clear and correct answer, any incorrect answers and spams will be reported and your account will be closed.

110 minutes] Consider a MyBinaryTree class, in which each Node has instance variables Node parent Node left; Node right; String label; and the usual getters, setters and constructors. Write the code for the method trimLeaves within MyBinaryTree, which is a method that modifies the tree structure in the following way: remove every Node that is a leaf in the original tree. For example, when the method is called on the tree below, Fred Barb Suzy Nick Dave Kate the tree is changed to be: Alex Barb Nick You are advised to write a private recursive method that performs the equivalent change of a subtree with given root.

Explanation / Answer

Come on yaar. You have all right to request for correct answer, but don't threat the teachers for their accounts being closed. Afterall, we are teachers. Anyways, here is the solution for you:

class MyBinaryTree
{
class Node
{
Node parent;
Node left;
Node right;
String label;
}
public void trimLeaves(Node root)
{
if(root == null)   //If there are no nodes in the tree.
return;           //Nothing to do.
else if(root.left== null && root.right == null)   //If there is only one node in the tree.
{  
root = null;   //As that is the leaf, remove that node.
return;       //And return.
}
else   //If the root has children.
{
trimLeaves(root.left);   //Call the trimLeaves() recursively for left subtree.
trimLeaves(root.right);   //Call the trimLeaves() recursively for right subtree.
return;                   //And return.
}
}
}

If you need any further refinements, just get back to me.