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

Write a recursive body for the following static method that computes and returns

ID: 3858860 • Letter: W

Question

Write a recursive body for the following static method that computes and returns the largest integer in a given non-empty Tree. Note that the Tree must be restored, i.e., its outgoing value must be the same as its incoming value. [JAVA]

/**

* Returns the largest integer in the given {@code Tree}.

*

* @param t

*            the {@code Tree} whose largest integer to return

* @return the largest integer in the given {@code Tree}

* @requires |t| > 0

* @ensures

* max is in labels(t) and

* for all i: integer where (i is in labels(t)) (i <= max)

*

*/

public static int max(Tree t) {...}

Explanation / Answer

Assuming your Tree node has element, left and right public fields below is the implementation of the method which will return the largest integer in the given binary tree.

Logic :

We first assume that the root is the max element. Then we compare left node of the root with the max with Math.max() static function using recursion technique. Then we do the same for the right side of the node. Finally we return the max Integer which we have found.

Comment if you have any further queries. Give a thumbs up if this answer helped you.

//Code ends here

Hope it helps!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote