This is a C++ Program for counting the total number of internal nodes present in a given Binary Search Tree.
We will be given a Binary Search Tree and we have to create a C++ program which counts the total number of non-leaf nodes i.e. Internal Nodes present in it using recursion. An internal node is one which has at least one children.
Case 1. Balanced Tree: When the weight on both the sides of the root node is same.
25 / \ 19 29 / \ / \ 17 20 27 55
Output: 3
Case 2. Right Skewed Tree: When the nodes at every level have just a right child.
1 \ 2 \ 3 \ 4 \ 5
Output: 4
Case 3. Tree having just one node
15
Output: 0
We can easily find the number of internal nodes present in any tree using recursion. An internal node is a node whose left or the right child is not NULL. We just need to check this single condition to determine whether the node is a leaf node or a non leaf (internal) node.
Here is source code of the C++ Program to count the total number of internal nodes present in a given Binary Search Tree. The program is successfully compiled and tested using Codeblocks gnu/GCC compiler on windows 10. The program output is also shown below.
/* C++ Program to find the number of internal nodes in a Tree */
#include <iostream>
using namespace std;
struct node
{
int info;
struct node *left, *right;
};
int count = 0;
class BST
{
public:
/*
* Function to create new nodes
*/
struct node *createnode(int key)
{
struct node *newnode = new node;
newnode->info = key;
newnode->left = NULL;
newnode->right = NULL;
return(newnode);
}
int internalnodes(struct node *newnode)
{
if(newnode != NULL)
{
internalnodes(newnode->left);
if((newnode->left != NULL) || (newnode->right != NULL))
{
count++;
}
internalnodes(newnode->right);
}
return count;
}
};
int main()
{
/* Creating first Tree. */
BST t1,t2,t3;
struct node *newnode = t1.createnode(25);
newnode->left = t1.createnode(19);
newnode->right = t1.createnode(29);
newnode->left->left = t1.createnode(17);
newnode->left->right = t1.createnode(20);
newnode->right->left = t1.createnode(27);
newnode->right->right = t1.createnode(55);
/* Sample Tree 1. Balanced Tree
* 25
* / \
* 19 29
* / \ / \
* 17 20 27 55
*/
cout<<"Number of internal nodes in first Tree are "<<t1.internalnodes(newnode);
cout<<endl;
count = 0;
/* Creating second tree */
struct node *node = t2.createnode(1);
node->right = t2.createnode(2);
node->right->right = t2.createnode(3);
node->right->right->right = t2.createnode(4);
node->right->right->right->right = t2.createnode(5);
/* Sample Tree 2. Right Skewed Tree (Unbalanced).
* 1
* \
* 2
* \
* 3
* \
* 4
* \
* 5
*/
cout<<"\nNumber of internal nodes in second tree are "<<t2.internalnodes(node);
cout<<endl;
count = 0;
/* Creating third Tree. */
struct node *root = t3.createnode(15);
/* Sample Tree 3. Tree having just one root node.
* 15
*/
cout<<"\nNumber of internal nodes in third tree are "<<t3.internalnodes(root);
return 0;
}
In this program we have used recursion to find the total number of internal nodes present in a tree.
2. A internal Node is one whose left or the right child is not NULL. We have created a function called internalnodes() which takes in root of the tree as a parameter and returns the total number of internal nodes it has.
3. The basic idea is to traverse the tree using any traversal so as to visit each and every node and check the condition for internal node for each node, that is what we have done in internalnodes() function.
4. In the internalnodes() function we have used the inorder traversal, by first traversing the left subtree, then instead of printing the root->data as a second step of inorder traversal, we have checked the internal node condition and then at last we have traversed the right subtree by passing root->right as a parameter.
Number of internal nodes in first Tree are 3 Number of internal nodes in second tree are 4 Number of internal nodes in third tree are 0
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.
- Practice Computer Science MCQs
- Practice Design & Analysis of Algorithms MCQ
- Check Data Structure Books
- Check Computer Science Books
- Check Programming Books