Computing Binary Tree Sums without Globals/Statics

In summary, the conversation discusses an algorithm for counting the sum of keys in a binary tree without using globals or statics. The algorithm uses a recursive function S to traverse the tree and add the data of each node to the sum of its left and right subtrees. The person asking for feedback confirms that the algorithm is correct, and apologizes for the confusion about the variable name. The expert concludes the conversation by congratulating the person on their successful algorithm.
  • #1
evinda
Gold Member
MHB
3,836
0
Hi! (Wave)

I want to write an algorithm, that counts the sum of the keys of the nodes of a binary tree, without the use of globals and statics.

That's what I have tried:

Code:
S(NODE *P){
 if (P==NULL) return 0;
 int m=P->data+S(P->left);
 int n=m+S(P->right);
 return n;

Could you tell me if it is right? (Thinking)
 
Last edited:
Technology news on Phys.org
  • #2
evinda said:
Hi! (Wave)

I want to write an algorithm, that counts the sum of the keys of the nodes of a binary tree, without the use of globals and statics.

That's what I have tried:

Code:
S(NODE *P){
 if (P==NULL) return 0;
 int s1=P->data+S(P->left);
 int s2=s1+S(P->right);
 return s2;

Could you tell me if it is right? (Thinking)

It is right. (Smile)
 
  • #3
evinda said:
Code:
S(NODE *P){
 if (P==NULL) return 0;
 int m=P->data+S(P->left);
 int n=s1+S(P->right);
 return n;
What is [m]s1[/m]?
 
  • #4
Evgeny.Makarov said:
What is [m]s1[/m]?

With [m] s1 [/m] I meant [m]m[/m], I am sorry... (Smile)
 
  • #5
I like Serena said:
It is right. (Smile)

Great! Thanks a lot! (Party)
 

Related to Computing Binary Tree Sums without Globals/Statics

1. How do you calculate the sum of a binary tree without using global or static variables?

To calculate the sum of a binary tree without using global or static variables, you can use a recursive function. The function should take in the root of the tree as its parameter and recursively traverse the tree, adding the value of each node to a local variable. This local variable will be updated with the sum of all nodes in the tree and can be returned as the final sum.

2. Why is it important to avoid using global or static variables when computing binary tree sums?

Using global or static variables can lead to unexpected side effects and make the code more difficult to understand and maintain. It can also result in incorrect sums if multiple functions are trying to access and update the same global or static variable simultaneously.

3. Can you explain the concept of recursion and how it can be used to compute binary tree sums?

Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. In the case of computing binary tree sums, the recursive function will traverse the tree and call itself on each child node until it reaches a leaf node. Then, it will return the value of the leaf node back to the parent node, which will add it to its own value and return it to its parent node, and so on until the entire tree is traversed.

4. Is there a more efficient way to compute binary tree sums without using global or static variables?

Yes, there are other data structures such as stacks and queues that can be used to compute binary tree sums without using global or static variables. These data structures can store the values of the nodes as they are traversed, eliminating the need for a global or static variable to keep track of the sum.

5. Can you provide an example of a recursive function for computing binary tree sums without using global or static variables?

Yes, here is an example of a recursive function in JavaScript for computing the sum of a binary tree without using global or static variables:

function calculateSum(root) {
    if (root === null) return 0;
    let leftSum = calculateSum(root.left);
    let rightSum = calculateSum(root.right);
    return root.value + leftSum + rightSum;
}

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
Back
Top