Find Value of y in Algorithm using Tree Root

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Value
In summary: Therefore, the value of y at termination would be different depending on the tree structure and values of the nodes.In summary, the algorithm updates the value of y each time it is called and the final value of y before termination depends on the values of all the nodes in the tree.
  • #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:

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? :confused:
 

Attachments

  • tree11.png
    tree11.png
    3.8 KB · Views: 82
Technology news on Phys.org
  • #2
No, the function does not work that way. The value of y is updated each time the Algorithm function is called. The value of y is reset to 0 each time Algorithm is called and then it is incremented or decremented depending on the value of P->key. So the value of y before termination would depend on the values of all the nodes in the tree.
 

FAQ: Find Value of y in Algorithm using Tree Root

How do you find the value of y in an algorithm using tree root?

In order to find the value of y in an algorithm using tree root, you will need to traverse the tree and perform calculations at each node. This may involve using mathematical operations or comparing values to determine the appropriate value for y.

What is the purpose of finding the value of y in an algorithm using tree root?

The value of y is often used to represent an unknown variable in an equation or mathematical problem. In the context of an algorithm using a tree root, the value of y may be used to determine the outcome of the algorithm or to solve a specific problem.

Can you explain the process of finding the value of y in an algorithm using tree root?

The process of finding the value of y in an algorithm using tree root typically involves breaking down the problem into smaller steps and using the information provided by the tree structure to determine the appropriate value. This may require using logic and problem-solving skills to arrive at the correct answer.

Are there any tips for finding the value of y in an algorithm using tree root?

Some tips for finding the value of y in an algorithm using tree root include carefully analyzing the structure of the tree, breaking down the problem into smaller parts, and using mathematical operations or logical reasoning to determine the value. It may also be helpful to draw out the tree and label each node to better visualize the problem.

How can I check if the value of y I have found in an algorithm using tree root is correct?

To check if the value of y is correct in an algorithm using tree root, you can retrace your steps and perform the necessary calculations or comparisons at each node of the tree. Alternatively, you can also use test cases or input different values to see if the algorithm produces the expected output for the given value of y.

Similar threads

Replies
30
Views
5K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
20
Views
4K
Replies
2
Views
2K
Back
Top