C Program to Find the Sum of all Nodes in a Tree

This C Program Finds the Sum of all Nodes in a Tree such that any node is sum of values at left and right sub tree.

Here is source code of the C Program to Find the Sum of all Nodes in a Tree such that any node is sum of values at left and right sub tree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find the Sum of all Nodes in a Tree
  3.  *
  4.  *        50
  5.  *        / \
  6.  *     20  30
  7.  *    / \
  8.  *   70  80
  9.  *   / \   \
  10.  *  10 40   60
  11.  *
  12.  */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. struct btnode
  17. {
  18.     int value;
  19.     struct btnode *l;
  20.     struct btnode *r;
  21. }*root = NULL, *ptr, *temp;
  22.  
  23. // Function Prototypes
  24. int find_depth(struct btnode *);
  25. int modify_tree(struct btnode *);
  26. void printout(struct btnode *);
  27. struct btnode* newnode(int);
  28.  
  29. void main()
  30. {
  31.     int d;
  32.  
  33.     root  =  newnode(50);
  34.     root->l  =  newnode(20);
  35.     root->r  =  newnode(30);
  36.     root->l->l  =  newnode(70);
  37.     root->l->r  =  newnode(80);
  38.     root->l->r->r  =  newnode(60);
  39.     root->l->l->l  =  newnode(10);
  40.     root->l->l->r  =  newnode(40);
  41.     printout(root);
  42.     ptr = root;
  43.     d = find_depth(ptr);
  44.     printf("Depth of tree is %d\n",d);
  45.     printf("tree elements after modification are ----\n");
  46.     modify_tree(ptr);
  47.     printout(ptr);
  48. }
  49.  
  50. // Create a node
  51. struct btnode* newnode(int value)
  52. {
  53.     struct btnode* node  =  (struct btnode*)malloc(sizeof(struct btnode));
  54.     node->value  =  value;
  55.     node->l  =  NULL;
  56.     node->r  =  NULL;
  57.     return(node);
  58. }
  59.  
  60. // Function to find depth of a tree
  61. int find_depth(struct btnode* tree)
  62. {
  63.     int ldepth, rdepth;
  64.  
  65.     if (tree == NULL)
  66.         return 0;
  67.     else
  68.     {
  69.         ldepth = find_depth(tree->l);
  70.         rdepth = find_depth(tree->r);
  71.         if (ldepth > rdepth)
  72.             return ldepth + 1;
  73.         else
  74.             return rdepth + 1;
  75.     }
  76. }
  77.  
  78. // Function to modify the tree
  79. int modify_tree(struct btnode *tree)
  80. {
  81.     int i, original;
  82.  
  83.     if (tree == NULL)
  84.         return 0;
  85.     original = tree->value;
  86.     tree->value = modify_tree(tree->l) + modify_tree(tree->r);
  87.     return tree->value + original;
  88. }
  89.  
  90. // Function to print the elements of tree
  91. void printout(struct btnode *tree)
  92. {
  93.     if (tree->l)
  94.         printout(tree->l);
  95.     printf("%d\n", tree->value);
  96.     if (tree->r)
  97.         printout(tree->r);
  98. }

$ gcc tree37.c
$ a.out
10
70
40
20
80
60
50
30
Depth of tree is 4
tree elements after modification are ----
0
50
0
260
60
0
310
0

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.