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!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.