# Counting Non-Leaf Nodes in Python: Top 5 Methods Explored

Rate this post

π‘ Problem Formulation: In binary trees, non-leaf nodes are nodes that have at least one child node. The challenge lies in accurately traversing the tree to count these nodes. Given a binary tree structure, we wish to devise a Python program that can count the number of non-leaf (internal) nodes. For instance, given a tree with structure `A-B-C` where `A` is the root, `B` is a left child, and `C` a right child, the desired output would be `1` since only `A` is a non-leaf node.

## Method 1: Recursive Traversal

The recursive method relies on the depth-first search (DFS) algorithm to traverse the tree structure. It counts a node if it has one or more children. The function is called recursively for each child node until it reaches the leaves, at which point it does not increment the count, thereby providing the number of non-leaf nodes.

Here’s an example:

```class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def countNonLeafNodes(node):
if node is None or (node.left is None and node.right is None):
return 0
return 1 + countNonLeafNodes(node.left) + countNonLeafNodes(node.right)

# Constructing the tree
root = Node('A')
root.left = Node('B')
root.right = Node('C')

# Counting non-leaf nodes
print(countNonLeafNodes(root))```

Output of this code:

`1`

This code snippet defines a Node class for individual tree nodes, and a `countNonLeafNodes()` function that counts the non-leaf nodes using recursion. It returns 0 for leaf nodes and counts 1 for every other node that has at least one child, adding up counts as it traverses up the tree.

## Method 2: Iterative Using Stack

Iterative traversal using a stack is an alternative approach to recursion. The stack helps keep track of nodes visited while avoiding recursion, which can be advantageous for trees with large depths where recursion might lead to a stack overflow.

Here’s an example:

```class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def countNonLeafNodes(root):
if root is None:
return 0
stack = [root]
non_leaf_count = 0
while stack:
node = stack.pop()
if node.left or node.right:
non_leaf_count += 1
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return non_leaf_count

# Constructing the tree
root = Node('A')
root.left = Node('B')
root.right = Node('C')

# Counting non-leaf nodes
print(countNonLeafNodes(root))```

Output of this code:

`1`

The code utilizes an iterative process with a stack to count non-leaf nodes. Nodes are pushed to and popped from the stack as the tree is traversed. A counter increments each time a node with one or more children is encountered. This method avoids the potential pitfalls of deep recursion.

## Method 3: Level Order Traversal Using Queue

Level order traversal, or breadth-first search (BFS), makes use of a queue to visit nodes level by level. Counting non-leaf nodes is done during this traversal by checking if a dequeued node has children and incrementing the count accordingly.

Here’s an example:

```from collections import deque

class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def countNonLeafNodes(root):
if root is None:
return 0
queue = deque([root])
non_leaf_count = 0
while queue:
node = queue.popleft()
if node.left or node.right:
non_leaf_count += 1
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return non_leaf_count

# Constructing the tree
root = Node('A')
root.left = Node('B')
root.right = Node('C')

# Counting non-leaf nodes
print(countNonLeafNodes(root))```

Output of this code:

`1`

This snippet introduces a breadth-first level order traversal using a queue, provided by `collections.deque`. As nodes are traversed level by level, the method tallies non-leaf nodes encountered, ensuring no node is missed and all levels are accounted for.

## Method 4: Using Morris Traversal for Space Efficiency

Morris Traversal is a binary tree traversal method that does not use recursion or a stack but makes temporary changes to the tree by creating links between nodes. It is especially useful for its space efficiency since it uses constant extra space.

Here’s an example:

```class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def countNonLeafNodes(root):
count = 0
current = root
while current:
if current.left:
predecessor = current.left
while predecessor.right and predecessor.right != current:
predecessor = predecessor.right

if predecessor.right is None:
predecessor.right = current
count += 1  # Count the node as it has a left child
current = current.left
else:
predecessor.right = None
current = current.right
else:
current = current.right
return count

# Constructing the tree
root = Node('A')
root.left = Node('B')
root.right = Node('C')

# Counting non-leaf nodes
print(countNonLeafNodes(root))```

Output of this code:

`1`

The Morris Traversal is a complex but ingeniously space-efficient method that manipulates the tree itself to create temporary links that help emulate the traversal process. The count increment occurs when a left child is first encountered, which indicates the presence of a non-leaf node.

## Bonus One-Liner Method 5: Functionally Recursive with Ternary Operator

This one-liner method is a compact version of the recursive traversal, using Python’s ternary conditional operator to streamline the logic into a single line within a lambda function.

Here’s an example:

```class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

# Counting non-leaf nodes with a one-liner recursive function
countNonLeafNodes = lambda node: 0 if node is None or (node.left is None and node.right is None) else 1 + countNonLeafNodes(node.left) + countNonLeafNodes(node.right)

# Constructing the tree
root = Node('A')
root.left = Node('B')
root.right = Node('C')

# Counting non-leaf nodes
print(countNonLeafNodes(root))```

Output of this code:

`1`

The code snippet presents a succinct recursive solution utilizing a lambda function to count non-leaf nodes, highlighting Python’s capability for writing concise yet powerful code. The one-liner captures the essence of the recursive approach with an elegant use of the ternary operator.

## Summary/Discussion

• Method 1: Recursive Traversal. This method is intuitive and simple to implement. Its strength lies in its direct approach to the problem. However, it could reach the recursion limit for deeply nested trees.
• Method 2: Iterative Using Stack. This method avoids the potential stack overflow issue of recursion and is as effective. The disadvantage might be that the code is less straightforward than the recursive method.
• Method 3: Level Order Traversal Using Queue. By using level order traversal, this method ensures that all nodes are systematically checked. It is non-recursive, which is beneficial for large trees, though counting each level adds a bit of complexity.
• Method 4: Using Morris Traversal for Space Efficiency. Morris Traversal is space-efficient, requiring O(1) extra space. The downside is that its implementation is quite complex and modifies the tree structure temporarily, which could be an issue for concurrency.
• Bonus One-Liner Method 5: Functionally Recursive with Ternary Operator. The one-liner is elegant and compact, which showcases Python’s expressive power. Like Method 1, it might also suffer from the recursion limit in deep trees.