Count Number of Nodes at each level
Write a C Program to Count Number of Nodes at each level in Binary Tree. Here’s simple Program to Find Number of Nodes at a level in Binary Tree in C Programming Language.
What is Tree ?
In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.
A tree data structure can be defined as follows…
- Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.
A tree data structure can also be defined as follows…
- Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.
Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.
Below is the source code for C Program to Count Number of Nodes at each level in Binary Tree which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
/* C Program to Count Number of Nodes at each level in Binary Tree */ #include<stdio.h> #include<stdlib.h> struct node { struct node *lchild; int info; struct node *rchild; }; struct node *insert(struct node *ptr, int ikey); void display(struct node *ptr,int level); int NodesAtLevel(struct node *ptr, int level) ; int main() { struct node *root=NULL,*root1=NULL,*ptr; int choice,k,item,level; while(1) { printf("\n"); printf("1.Insert Tree \n"); printf("2.Display Tree \n"); printf("3.Number of Nodes \n"); printf("4.Quit\n"); printf("\nEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the key to be inserted : "); scanf("%d",&k); root = insert(root, k); break; case 2: printf("\n"); display(root,0); printf("\n"); break; case 3: printf("\n"); printf("Enter any level :: "); scanf("%d",&level); printf("\nNumber of nodes at [ %d ] Level :: %d\n",level,NodesAtLevel(root,level)); break; case 4: exit(1); default: printf("\nWrong choice\n"); }/*End of switch */ }/*End of while */ return 0; }/*End of main( )*/ struct node *insert(struct node *ptr, int ikey ) { if(ptr==NULL) { ptr = (struct node *) malloc(sizeof(struct node)); ptr->info = ikey; ptr->lchild = NULL; ptr->rchild = NULL; } else if(ikey < ptr->info) /*Insertion in left subtree*/ ptr->lchild = insert(ptr->lchild, ikey); else if(ikey > ptr->info) /*Insertion in right subtree */ ptr->rchild = insert(ptr->rchild, ikey); else printf("\nDuplicate key\n"); return(ptr); }/*End of insert( )*/ void display(struct node *ptr,int level) { int i; if(ptr == NULL )/*Base Case*/ return; else { display(ptr->rchild, level+1); printf("\n"); for (i=0; i<level; i++) printf(" "); printf("%d", ptr->info); display(ptr->lchild, level+1); } }/*End of display()*/ int NodesAtLevel(struct node *ptr, int level) { if(ptr==NULL) return 0; if(level==0) return 1; return NodesAtLevel(ptr->lchild,level-1) + NodesAtLevel(ptr->rchild,level-1); }/*End of NodesAtLevel()*/ |
OUTPUT : :
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
/* C Program to Count Number of Nodes at each level in Binary Tree */ 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 7 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 5 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 6 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 4 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 2 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 13 Wrong choice 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 11 Wrong choice 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 3 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 1 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 8 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 9 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 1 Enter the key to be inserted : 10 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 2 10 9 8 7 6 5 4 3 2 1 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 3 Enter any level :: 0 Number of nodes at [ 0 ] Level :: 1 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 3 Enter any level :: 1 Number of nodes at [ 1 ] Level :: 2 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 3 Enter any level :: 2 Number of nodes at [ 2 ] Level :: 3 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 3 Enter any level :: 3 Number of nodes at [ 3 ] Level :: 2 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 3 Enter any level :: 4 Number of nodes at [ 4 ] Level :: 2 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 3 Enter any level :: 5 Number of nodes at [ 5 ] Level :: 0 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 2 10 9 8 7 6 5 4 3 2 1 1.Insert Tree 2.Display Tree 3.Number of Nodes 4.Quit Enter your choice : 4 Process returned 1 |
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post…