Consider the following definitions, and answer the parts (b-g) below int mystery
ID: 3905099 • Letter: C
Question
Consider the following definitions, and answer the parts (b-g) below int mystery(Node t) f if (tnull) return 1; else int x- mystery(t.right); int y- mystery (t.left); if (x > y) return x * t.key; else return y * t.key TA tB Figure 2 b. (4 marks) What does mystery (tA) return, where tA is depicted in Figure 2 c. (4 marks) What does mystery(tB) return, where tB is depicted in Figure 2 d. (2 marks) What does mystery (t) return when t is a tree with a single node? e. (5 marks) What does mystery (t) return when t is any binary search tree with positive values in all Nodes? f. (5 marks) What does mystery) eturn when t is any binary tree with positive values g. (2 marks) If all Nodes contain positive values except for the root, which contains value h. (2 marks) If all Nodes contain positive values except for the root, which contains value in all Nodes? -1, what does mystery(t) return? 0, what does mystery (t) return?Explanation / Answer
In the given code, if t is null we return 1.
Otherwise, we run the function mystery() for the left and right child and see which is maximum.
The maximum value is multiplied with the key of the current node and returned.
We can observe here that the given function returns the maximum product of the keys in a particular path from root to leaf in the tree.
b. The maximum product is in the path : 6->8->9. Hence mystery(tA) returns: 6*8*9 = 432
c. The maximum product is in the path : 5->7-8. Hence mystery(tA) returns: 5*7*8 = 280
d. When t is a tree with a single node, t.right and t.left both are null. Thus mystery(t.right) and mystery(t.left) bith returns 1. Hence, mystery(t) returns 1*t.key = t.key
e. when t is a binary search tree with positive values in all node, mystery(t) returns the maximum product of the keys in a particular path from root to leaf in the tree. It is same for any binary tree
f. when t is a binary tree with positive values in all node, mystery(t) returns the maximum product of the keys in a particular path from root to leaf in the tree.
g.If all node contains positive values except teh root which contains -1, mystery(t) returns (the maximum product of the keys in a particular path from root to leaf in the tree)*-1 i.e. the negative value of the maximum product of the keys in a particular path from root to leaf in the tree.
h.if the root contains 0 value then at the end of all the recursions, 0 will be multiplied to the product and we will always get a 0 as output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.