Count Non-Leaf Nodes in a Binary TreeCounting 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: - - It provides an organized way in which the data is structured in the binary tree.
- Non-leaf nodes are also known for providing support for the advanced algorithms performed in the binary tree.
- They also help in the efficient use of the space in the binary tree and help us in performing the work.
- They are also the absolute path for the divide and conquer problems.
ImplementationOutput A step-by-step explanation of the code - It begins by including the necessary header files such as 'iostream' and many others.
- 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.
- Next, we define a helper function called 'newNode', which is supposed to create a new node in the tree and help us allocate memory.
- 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.
- 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.
- 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.
- 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.
- Finally, the countNonleaf() function is called with the root as an argument, and whatever result we have received is printed.
Example 2)Output A step-by-step explanation of the code - The program begins by declaring a major class named 'TPT', which contains the main logic of the program.
- 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.
- Next, we define a helper function called 'newNode', which is supposed to create a new node in the tree and help us allocate memory.
- 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.
- 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.
- 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.
- 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.
- Finally, the countNonleaf() function is called with the root as an argument, and whatever result we have received is printed.
|