Given a node class Bt, containing child reference lt and rt, and a value field “
ID: 3696422 • Letter: G
Question
Given a node class Bt, containing child reference lt and rt, and a value field “double value;”, as well as a method “check”, implemented as follows:
public static Boolean check (Bt t1, Bt t2)
{
boolean result;
If ((t1 == null) && (t2 == null)){
result = true;
}
else if ((t1 != null) && (t2 != null) && (t1.value == t2.value) {
boolean Left = check (t1.lt, t2.lt);
boolean Right = check (t1.rt, t2.rt);
Result = Left && Right;
} else {
result = false;
}
return result;
}
What does check return for these trees?
T1 -> 7 T2 -> 7
50 3 50 3
Answer: ___________
What does check return for these trees?
T1 -> 7 T2 -> 7
50 3 3
Answer: ___________
What does “check” do in general? That is – what does it return for any two binary trees t1 and t2?
Explanation / Answer
1) check returns True
2) check returns False
In general check returns "true" if the two trees t1 and t2 are equal and "flase" if they are not equal.
The code is simple to understand.
1) If the two trees are null, this means both are equal , So check returns true.
2) If both are not null and value at root node are equal. Then we check whether left subtree of t1 is equal to left subtree of t2 and right subtree of t1 is equal to right subtree of t2. If these two return true. Then check returns true.
3) Any other possibility reults in false.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.