# 5 Best Ways to Find the Number of Nodes in a Subtree with the Same Label Using Python

Rate this post

π‘ Problem Formulation: In tree data structures, subtrees often represent hierarchical subsets of data. A common query might involve counting the nodes within a subtree that share a common label. For example, given a tree where each node is labelled with a letter, one might be tasked with finding the number of ‘A’-labelled nodes within a particular subtree. The ideal output would be an integer that represents this count.

## Method 1: Depth-First Search (DFS)

Depth-First Search (DFS) is a fundamental algorithm used for traversing or searching tree or graph data structures. The algorithm explores as far as possible along each branch before backtracking. To find the number of nodes with the same label in a subtree, we can recursively apply DFS to each node and count the instances of the target label from the given subtree node.

Here’s an example:

```def count_labels(root, target_label):
if root is None:
return 0
return (root.label == target_label) + count_labels(root.left, target_label) + count_labels(root.right, target_label)

# Assuming a Node class with 'label', 'left', and 'right' attributes.
```

Output:

If the target label is ‘A’, and there are 4 ‘A’ labelled nodes in the subtree with a given root, the function will return `4`.

This snippet represents a simple recursive function that traverses the nodes in a subtree and tallies those with the desired label. The sum of boolean expressions adds 1 when a node’s label matches the target label and 0 otherwise. When the recursion hits a null pointer (leaf node’s child), it contributes nothing to the count, effectively ending that path’s contribution to the sum.

## Method 2: Breadth-First Search (BFS)

Breadth-First Search (BFS) explores the neighbor nodes at the present depth prior to moving on to nodes at the next depth level. This level-by-level approach can also be employed to count the labels by iterating over each level of the subtree and counting the instances of the target label. Unlike DFS, BFS uses a queue to track the nodes at the current depth level.

Here’s an example:

```from collections import deque

def count_labels_bfs(root, target_label):
if not root:
return 0

count = 0
queue = deque([root])

while queue:
node = queue.popleft()
if node.label == target_label:
count += 1
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)

return count

# Again, assuming a Node class definition.
```

If we have a subtree where 3 nodes have the label ‘B’, the function will return `3`.

This BFS-based method utilizes a queue to process nodes level by level. Nodes with the matching label increment the count. The function is straightforward – it checks every node’s label against the target label, using a queue to ensure we explore the breadth of the tree before its depth.

## Method 3: Iterative Post-order Traversal

Post-order traversal is a type of DFS where nodes are processed after their children. The iterative version uses a stack to traverse the tree without recursion, which can be more space-efficient for deep trees. This method again checks each node’s label against the target label as we process nodes in post-order fashion.

Here’s an example:

```def count_labels_postorder(root, target_label):
if not root:
return 0

count = 0
stack = [root]
while stack:
node = stack.pop()
if node.label == target_label:
count += 1
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)

return count

# We use the same Node class definition as before.
```

The output will match the count of nodes with the given target label in the subtree, e.g., `5` for label ‘C’.

This snippet demonstrates how to count labels without using recursion by maintaining our own stack for traversal. Although this method traverses all nodes as well, the iterative approach might be preferable in scenarios with large trees to avoid stack overflow from deep recursion.

## Method 4: Optimized DFS with Memoization

Memoization enhances DFS by saving results from subproblems to avoid redundant computations in overlapping subtrees. For counting labels, we can memoize the count for each subtree with a given root and target label, which is particularly helpful when the same subtrees are repeatedly queried.

Here’s an example:

```def count_labels_memo(root, target_label, memo):
if root is None:
return 0
if (root, target_label) in memo:
return memo[(root, target_label)]
memo[(root, target_label)] = (root.label == target_label) + \
count_labels_memo(root.left, target_label, memo) + \
count_labels_memo(root.right, target_label, memo)
return memo[(root, target_label)]

memo = {}
```

If the target label is ‘D’ and we have memoized previous calculations, the function utilizes the stored values to quickly return the count, e.g., `2`.

In this code, memoization stores the counts of matching labels for subtrees, avoiding recalculation when the same subtree is encountered again. This is especially efficient when the function is called frequently on the same tree with varying subtrees as input.

## Bonus One-Liner Method 5: Comprehension with Generators

The Pythonic way to use comprehensions with generators can create succinct and readable one-liners. By using a generator expression combined with the built-in sum function, we can traverse the tree in a compact form to count matching labels.

Here’s an example:

```def count_labels_gen(root, target_label):
return sum(1 for node in gen_nodes(root) if node.label == target_label)

def gen_nodes(node):
if node:
yield node
yield from gen_nodes(node.left)
yield from gen_nodes(node.right)

# Node class is defined as per previous examples.
```

This will return a count of nodes with the desired label, e.g., `7` for label ‘E’.

The snippet defines `gen_nodes` as a generator that traverses the tree yielding one node at a time. The `count_labels_gen` function then counts the number of yielded nodes matching the target label, demonstrating Python’s powerful generator expressions and comprehensions for concise, functional-style code.

## Summary/Discussion

• Method 1: Depth-First Search (DFS). Strengths: Conceptually simple; uses recursion for natural tree traversal. Weaknesses: Potentially high call stack usage for deep trees leading to stack overflow.
• Method 2: Breadth-First Search (BFS). Strengths: Can handle very deep trees without stack overflow; may perform better if the matching labels are distributed towards the top of the tree. Weaknesses: Extra space for the queue; typically slower for complete label count due to level-by-level traversal.
• Method 3: Iterative Post-order Traversal. Strengths: Avoids recursive calls, therefore preventing stack overflow; post-order processing may simplify certain label counting conditions. Weaknesses: More complex implementation; less intuitive than recursion.
• Method 4: Optimized DFS with Memoization. Strengths: Highly efficient when many overlapping queries are made on the same subtrees. Weaknesses: Additional memory overhead for memoization; unnecessary for single-use cases.
• Bonus Method 5: Comprehension with Generators. Strengths: Extremely concise and Pythonic; generatorβs lazy evaluation is memory efficient. Weaknesses: May be slower due to the overhead of generator creation; less readable for those unfamiliar with Python generators.