Consider the following statements. S1 : The sequence of procedure calls corresponds to a preorder traversal of the activation tree. S2 : The sequence of procedure returns corresponds to a postorder traversal of the activation tree. Which one of the following options is correct?
Consider the following statements. S1 : The sequence of procedure calls corresponds to a preorder traversal of the activation tree. S2 : The sequence of procedure returns corresponds to a postorder traversal of the activation tree. Which one of the following options is correct? Correct Answer S<sub>1</sub> is true and S<sub>2</sub> is true
Answer: Option 1
Explanation:
Statement 1:The sequence of procedure calls corresponds to a preorder traversal of the activation tree.
Consider following example
Fun( int n )
{
if( n==0 || n==1)
return n;
else {
return Fun(n/2) + Fun(n/2) ;
}
Suppose function call made as Fun(8). now recursion tree will be
[ alt="F1 Raju.S 01-04-21 Savita D18" src="//storage.googleapis.com/tb-img/production/21/04/F1_Raju.S_01-04-21_Savita_D18.png" style="width: 480px; height: 222px;">
The Function call sequence will be (in terms of n): 8, 4, 2, 1, 1, 2, 1, 1, 4, 2, 1, 1, 2, 1, 1 ( Same as pre-order traversal of the tree )
The Function Returning Sequence will be : 1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8 ( Same as post-order traversal of the tree)
Hence This Statement is correct.
Statement 2:
This Statement is also correct.