In this program, we are going to share a C program to count number of non leaf nodes of a given tree. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to count number of non leaf nodes of a given tree with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <stdio.h> #include <stdlib.h> struct btnode { int value; struct btnode *r,*l; } *root = NULL, *temp = NULL; void create(); void insert(); void add(struct btnode *t); void inorder(struct btnode *t); int count = 0; void main() { int ch; printf("\nOPERATIONS ---"); printf("\n1] Insert "); printf("\n2] Display"); printf("\n3] Exit "); while (1) { printf("\nEnter your choice : "); scanf("%d", &ch); switch (ch) { case 1: insert(); break; case 2: inorder(root); printf("\nNumber of non leaf nodes: %d", count); break; case 3: exit(0); default : printf("Wrong choice, Please enter correct choice "); break; } } } /* To create a new node with the data from the user */ void create() { int data; printf("Enter the data of node : "); scanf("%d", &data); temp = (struct btnode* ) malloc(1*(sizeof(struct btnode))); temp->value = data; temp->l = temp->r = NULL; } /* To check for root node and then create it */ void insert() { create(); if (root == NULL) root = temp; else add(root); } /* Search for the appropriate position to insert the new node */ void add(struct btnode *t) { if ((temp->value > t->value) && (t->r != NULL)) add(t->r); else if ((temp->value > t->value) && (t->r == NULL)) t->r = temp; else if ((temp->value < t->value) && (t->l != NULL)) add(t->l); else if ((temp->value < t->value) && (t->l == NULL)) t->l = temp; } /* To display and count the sum of non leaf nodes */ void inorder(struct btnode *t) { if (t->l != NULL) inorder(t->l); if ((t->l != NULL) || (t->r != NULL)) { count++; printf("%d ->",t->value); } if (t->r != NULL) inorder(t->r); } |
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
20->40->60->80->
Number of non leaf nodes: 4
Enter your choice : 3
40
/\
/ \
20 60
/ \ \
10 30 80
\
90
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.