C Program to Count Leaf Nodes and Total Nodes in a Binary Search Tree (BST)
A Binary Search Tree (BST) is a binary tree where the left subtree contains values less than the node and the right subtree contains values greater than the node. In this program, we implement two functions:
- Count leaf nodes (nodes with no children).
- Count total number of nodes in the BST.
๐ Problem Statement
B) Write a C Program to implement the following functions on Binary Search Tree:
- To count leaf nodes.
- To count total number of nodes.
๐ก Logic (Simple Steps)
๐น Create BST
- Define a node structure with
data,left, andrightpointers. - Insert nodes recursively based on BST property:
- If value < current node โ go to left subtree.
- If value > current node โ go to right subtree.
๐น Count Leaf Nodes
- If node is
NULLโ return 0. - If node has no left and right children โ return 1.
- Otherwise โ recursively count leaf nodes in left and right subtrees.
๐น Count Total Nodes
- If node is
NULLโ return 0. - Otherwise โ 1 + count nodes in left subtree + count nodes in right subtree.
๐ป Source Code (Student-Friendly)
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct node
{
int data;
struct node* left;
struct node* right;
};
// Function to create a new node
struct node* createNode(int value)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
// Function to insert node in BST
struct node* insertBST(struct node* root, int value)
{
if(root == NULL)
return createNode(value);
if(value < root->data)
root->left = insertBST(root->left, value);
else if(value > root->data)
root->right = insertBST(root->right, value);
return root;
}
// Function to count leaf nodes
int countLeafNodes(struct node* root)
{
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
return countLeafNodes(root->left) + countLeafNodes(root->right);
}
// Function to count total nodes
int countTotalNodes(struct node* root)
{
if(root == NULL)
return 0;
return 1 + countTotalNodes(root->left) + countTotalNodes(root->right);
}
int main()
{
struct node* root = NULL;
int n, value, i;
printf("Enter number of nodes: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter value for node %d: ", i+1);
scanf("%d", &value);
root = insertBST(root, value);
}
printf("Total number of nodes: %d\n", countTotalNodes(root));
printf("Number of leaf nodes: %d\n", countLeafNodes(root));
return 0;
}
๐ฅ๏ธ Sample Output
Enter number of nodes: 5
Enter value for node 1: 50
Enter value for node 2: 30
Enter value for node 3: 70
Enter value for node 4: 20
Enter value for node 5: 40
Total number of nodes: 5
Number of leaf nodes: 3
Explanation:
Leaf nodes are 20, 40, and 70. Total nodes = 5.
๐ Viva Questions with Answers
1. What is a Binary Search Tree (BST)?
A binary tree where left subtree < node < right subtree for every node.
2. What is a leaf node?
A node with no left or right children.
3. How do you count total nodes?
1 + count of nodes in left subtree + count of nodes in right subtree.
4. Why recursion is used here?
Because tree traversal naturally follows a recursive structure.
5. What is the time complexity of inserting in BST?
O(h), where h = height of the BST. In worst case (skewed tree) O(n).
โ Conclusion
This C program is student-friendly and demonstrates how to:
- Create a Binary Search Tree
- Count leaf nodes
- Count total nodes
It is important for practical exams and helps students understand recursion, tree traversal, and BST properties clearly.
