Javatpoint Logo
Javatpoint Logo

Count Non-Leaf Nodes in a Binary Tree

Counting non-leaf nodes in a binary tree is a big problem because it involves traversing the whole tree and visiting each one of the nodes personally. It involves that we have to figure out the number of nodes in a tree that contains at least one child. Non-leaf nodes play an important role in understanding the structure and organization of a binary tree as they continue to be the intermediate point between the root node and the leaf nodes.

In this article, we are given a binary tree and are supposed to find the total number of non-leaf nodes in a binary tree. For this, we have to repeatedly move across or traverse the whole tree, count the non-leaf nodes in the left and right subtree, and add 1 to the result obtained.

The advantages of non-leaf nodes in a binary tree are: -

  1. It provides an organized way in which the data is structured in the binary tree.
  2. Non-leaf nodes are also known for providing support for the advanced algorithms performed in the binary tree.
  3. They also help in the efficient use of the space in the binary tree and help us in performing the work.
  4. They are also the absolute path for the divide and conquer problems.

Implementation

Output

Count Non-Leaf Nodes in a Binary Tree

A step-by-step explanation of the code

  1. It begins by including the necessary header files such as 'iostream' and many others.
  2. The code then moves on to defining a structure called 'Node', which contains three members in general: a data value and a pointer to the left and right child.
  3. Next, we define a helper function called 'newNode', which is supposed to create a new node in the tree and help us allocate memory.
  4. We then define a function called 'countNonleaf()' that calculates the number of non-leaf nodes in the tree. It takes the root node as an input.
  5. In the countNonleaf() function, there are a few cases that we need to check: if the root appears to be NULL or if both the left and the correct child appear to be NULL, then it is a leaf node.
  6. If the root doesn't appear to be NULL and if one of its children is not NULL, then we have to add one to the obtained number of count leaf nodes along with the count we gathered from the left and right subtrees.
  7. The primary () function of the program serves as an entry point, and it creates a binary tree having five nodes by using the newNode() function.
  8. Finally, the countNonleaf() function is called with the root as an argument, and whatever result we have received is printed.

Example 2)

Output

Count Non-Leaf Nodes in a Binary Tree

A step-by-step explanation of the code

  1. The program begins by declaring a major class named 'TPT', which contains the main logic of the program.
  2. Inside the 'TPT' class, we create a nested class called 'Node', which contains three members in general: a data value and a pointer to the left and right child.
  3. Next, we define a helper function called 'newNode', which is supposed to create a new node in the tree and help us allocate memory.
  4. We then define a function called 'countNonleaf()' that calculates the number of non-leaf nodes in the tree. It takes the root node as an input.
  5. In the countNonleaf() function, there are a few cases that we need to check; they are: in case the root appears to be NULL or in case both the left and the correct child appear to be NULL, then it is a leaf node.
  6. If the root doesn't appear to be NULL, and if one of its children is not NULL, then we have to add one to the obtained number of count leaf nodes along with the count we gathered from the left and right subtrees.
  7. The primary () function of the program serves as an entry point, and it creates a binary tree having five nodes by using the newNode() function.
  8. Finally, the countNonleaf() function is called with the root as an argument, and whatever result we have received is printed.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA