For Function1 below, describe what the integer (firstInt) represents (Example: t
ID: 3866542 • Letter: F
Question
For Function1 below, describe what the integer (firstInt) represents (Example: the height of the tree, the depth of the tree, etc)? Please also give justification for your answer and show working. What led to your answer? It would be helpful to also give a brief description of how the function works.
Note1: Function2returns a boolean which is true if and only if the node v has no children
Note2: Function 3 returns the depth of the tree at a certain position
public static <E> int Function1 (Tree<P> A) {
int A = 0;
for (Position <P> v: T.positions()) {
if(T.function2(v))
firstInt = Math.max(firstInt, Fuction3(T,v))
}
return firstInt;
}
Explanation / Answer
In the code provided, firstInt represents maximum depth of the tree.
This is because, the function runs as follows: -
Function1 takes a tree as input parameter. The for loop traverses the tree to get all the positions, or nodes in the tree.
For each position, we first check if it is leaf node. This is done using if(T.function2(v)), This function will be true, if node has no children, i.e. leaf node.
If we get leaf node, then we compare the previous value of firstInt and the depth of tree at leaf node v. This is done using Function3() method which returns the depth of tree at position v.
It saves the maximum of the two value in firstInt
So, For all the leaves, we are getting the depth and comparing, what is the maximum depth, and that depth is returned by the function.
So maximum depth is stored in firstInt.
Let me know in comments if you have any trouble understanding it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.