C Program to Find the Summation of Node Values at Row or Level

This C Program finds the summation of node values at row level and print it.

Here is source code of the C Program to finds the summation of node values at row level and print it. 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 Summation of Node values at level/row and print it
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct btnode
  8. {
  9.     int value;
  10.     struct btnode *r,*l;
  11. }*root = NULL, *temp = NULL;
  12.  
  13. void create();
  14. void insert();
  15. void add(struct btnode *t);
  16. void computesum(struct btnode *t);
  17. void display();
  18.  
  19. int count = 0, sum[100] = {0}, max = 0;
  20.  
  21. void main()
  22. {
  23.     int ch;
  24.  
  25.     printf("\n OPERATIONS ---");
  26.     printf("\n 1] Insert an element into tree");
  27.     printf("\n 2] Display the sum of the elements at the same level");
  28.     printf("\n 3] Exit ");    
  29.     while (1)
  30.     {                        
  31.         printf("\nEnter your choice : ");
  32.         scanf("%d", &ch);
  33.         switch (ch)
  34.         {
  35.         case 1:    
  36.             insert();
  37.             break;
  38.         case 2: 
  39.             count = 0;
  40.             max = 0;
  41.             computesum(root);
  42.             display();
  43.             break;
  44.         case 3: 
  45.             exit(0);
  46.         default :     
  47.             printf("Wrong choice, Please enter correct choice  ");
  48.             break;    
  49.         }
  50.     }
  51. }
  52.  
  53. /* To create a new node with the data from the user */
  54. void create()
  55. {
  56.     int data;
  57.  
  58.     printf("Enter the data of node : ");
  59.     scanf("%d", &data);
  60.     temp = (struct btnode* ) malloc(1*(sizeof(struct btnode)));
  61.     temp->value = data;
  62.     temp->l = temp->r = NULL;
  63. }
  64.  
  65. /* To check for root node and then create it */
  66. void insert()
  67. {
  68.     create();
  69.  
  70.     if (root == NULL)
  71.         root = temp;
  72.     else
  73.         add(root);
  74. }
  75.  
  76. /* Search for the appropriate position to insert the new node */
  77. void add(struct btnode *t)
  78. {
  79.     if ((temp->value > t->value) && (t->r != NULL))        /* value more than root node value insert at right */
  80.         add(t->r);
  81.     else if ((temp->value > t->value) && (t->r == NULL))        
  82.         t->r = temp;
  83.     else if ((temp->value < t->value) && (t->l != NULL))        /* value less than root node value insert at left */
  84.         add(t->l);
  85.     else if ((temp->value < t->value) && (t->l==NULL))
  86.         t->l = temp;
  87. }
  88.  
  89. /* Function to find the sum of nodes at same distance */
  90. void computesum(struct btnode *t)
  91. {
  92.     if (root == NULL)
  93.     {    
  94.         printf("Tree is empty ");
  95.         return;
  96.     }
  97.     if (t->l != NULL)
  98.     {
  99.         count++;    
  100.         computesum(t->l);
  101.     }
  102.     sum[count] = sum[count] + t->value;  /* addition of elelment by row wise */
  103.     if (max < count)
  104.         max = count;
  105.     if (t->r != NULL)
  106.     {
  107.         count++;        
  108.         computesum(t->r);
  109.     }
  110.     count--;
  111. }
  112.  
  113. /* To display the sum of the nodes at the same distance */
  114. void display()
  115. {
  116.     int i;
  117.  
  118.     printf("Sum of nodes : \n Level \t Sum ");
  119.     for (i = 0; i <= max; i++)
  120.        printf("\n %d \t: %d ", i, sum[i]);
  121. }

$ cc tree43.c
$ a.out
 
 OPERATIONS ---
 1] Insert an element into tree
 2] Display the sum of the elements at the same level
 3] Exit 
 Enter your choice : 1
 Enter the data of node : 40
 
 Enter your choice : 1
 Enter the data of node : 20
 
 Enter your choice : 1
 Enter the data of node : 60
 
 Enter your choice : 1
 Enter the data of node : 10
 
 Enter your choice : 1
 Enter the data of node : 30
 
 Enter your choice : 1
 Enter the data of node : 80
 
 Enter your choice : 1
 Enter the data of node : 90
 
 Enter your choice : 2
 Sum of nodes : 
  Level      Sum 
   0     : 40 
   1     : 80 
   2     : 120 
   3     : 90 
 Enter your choice : 3
 
                40
                /\
               /  \
             20    60
             / \    \
           10  30   80
                     \
                     90 
 
 $ ./a.out
 
 OPERATIONS ---
 1] Insert an element into tree
 2] Display the sum of the elements at the same level
 3] Exit 
 Enter your choice : 1
 Enter the data of node : 50
 
 Enter your choice : 1
 Enter the data of node : 30
 
 Enter your choice : 1
 Enter the data of node : 20
 
 Enter your choice : 1
 Enter the data of node : 40
 
 Enter your choice : 1
 Enter the data of node : 35
 
 Enter your choice : 1
 Enter the data of node : 100
 
 Enter your choice : 1
 Enter the data of node : 70
 
 Enter your choice : 1
 Enter the data of node : 120
 
 Enter your choice : 1
 Enter the data of node : 140
 
 Enter your choice : 2
 Sum of nodes : 
 Level      Sum 
   0      : 50 
   1      : 130 
   2      : 250 
   3      : 175 
Enter your choice : 3
 
 
 
                50
                /\
               /  \
             30    100
             / \   / \
           20  40 70 120
               /       \
              35       140

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.