- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)
I want to find the value of the variable [m] y [/m] of the algorithm of the graph, exactly before the termination of the algorithm, when it is applied with argument a pointer to the root of the following tree.That is the given algorithm:
View attachment 3869I thought that we execute the following commands:[m]
Algorithm(P)
y=0
y=20
Algorithm(A)
y=0
y=10
Algorithm(NULL)
Algorithm(NULL)
y=1
Algorithm(B)
y=0
y=70
Algorithm(C)
y=0
y=40
...
...
Algorithm(D)
y=0
y=70
y=71
[/m]I thought that the value of [m] y [/m] that we find at a call of the function when we consider a node doesn't depend on the call of the function when we consider an other node.
The result would be [m] 71 [/m] but isn't right... (Shake)
So doesn't the function work in this way?
I want to find the value of the variable [m] y [/m] of the algorithm of the graph, exactly before the termination of the algorithm, when it is applied with argument a pointer to the root of the following tree.That is the given algorithm:
Code:
void Algorithm(node *P){
static int y=0;
if (P==NULL) return 0;
y=y+2*P->key;
Algorithm(P->lc);
Algorithm(P->rc);
y++;
}
View attachment 3869I thought that we execute the following commands:[m]
Algorithm(P)
y=0
y=20
Algorithm(A)
y=0
y=10
Algorithm(NULL)
Algorithm(NULL)
y=1
Algorithm(B)
y=0
y=70
Algorithm(C)
y=0
y=40
...
...
Algorithm(D)
y=0
y=70
y=71
[/m]I thought that the value of [m] y [/m] that we find at a call of the function when we consider a node doesn't depend on the call of the function when we consider an other node.
The result would be [m] 71 [/m] but isn't right... (Shake)
So doesn't the function work in this way?