# How Do You Find the Width of a Tree in Data Structure?

//

Scott Campbell

Finding the Width of a Tree in Data Structure

In data structure, a tree is a hierarchical structure that is widely used to represent various relationships between elements. When working with trees, one common task is to determine the width of the tree.

The width of a tree refers to the maximum number of nodes at any level in the tree. This article will guide you on how to find the width of a tree using different techniques.

The Breadth-First Search (BFS) algorithm can be used to find the width of a tree. BFS traverses the tree level by level, keeping track of the maximum number of nodes found at any level.

Let’s take a look at an example:

```    1
/ \
2   3
/ \   \
4   5   6
```

To find the width using BFS, we start by enqueueing the root node and initializing variables for the current level count and maximum width:

```queue.enqueue(root)
currentLevelCount = 1
maxWidth = currentLevelCount
```

Then, we enter into a loop that continues until all nodes have been processed:

```while queue is not empty:
currentNode = queue.dequeue()
currentLevelCount -= 1

// Process currentNode

if currentNode has left child:
queue.enqueue(left child)

if currentNode has right child:
queue.enqueue(right child)

if currentLevelCount equals zero:
currentLevelCount = size of queue

// Update maxWidth if necessary
maxWidth = max(maxWidth, currentLevelCount)
```

The above algorithm performs a level-by-level traversal, updating both the current level count and maximum width whenever necessary. At the end, the maximum width of the tree is stored in the maxWidth variable.

### Example:

Let’s apply the BFS approach to find the width of the example tree:

Starting with the root node (1), we enqueue it and initialize currentLevelCount and maxWidth to 1.

In the first iteration:

• We dequeue node 1 and process it.
• We enqueue nodes 2 and 3 as they are its children.
• We decrement currentLevelCount by 1 (currentLevelCount = 0).
• We update currentLevelCount to the size of the queue (currentLevelCount = 2).
• Since currentLevelCount is not zero, we do not update maxWidth.

In the second iteration:

• We dequeue node 2 and process it.
• We enqueue nodes 4 and 5 as they are its children.
• We decrement currentLevelCount by 1 (currentLevelCount = 1).
• The queue still has one more node, so we do not update currentLevelCount or maxWidth.

In the third iteration:

• We dequeue node 3 and process it.
• We enqueue node 6 as it is its child.
• We update currentLevelCount to the size of the queue (currentLevelCount = 1).

In the fourth iteration:

• We dequeue node 4 and process it.
• There are no children to enqueue for node 4.

In the fifth iteration:

• We dequeue node 5 and process it.
• There are no children to enqueue for node 5.

In the sixth iteration:

• We dequeue node 6 and process it.
• There are no children to enqueue for node 6.
• We decrement currentLevelCount by 1 (currentLevelCount = 0).
• .

The while loop ends as there are no more nodes in the queue. The maximum width of the tree is stored in maxWidth, which is equal to 2. Therefore, the width of this tree is 2.

## Conclusion

Finding the width of a tree using Breadth-First Search (BFS) is a simple and efficient approach. By traversing the tree level by level, we can keep track of the maximum number of nodes at any level, which gives us the width of the tree. Remember to use a queue data structure to implement the BFS algorithm effectively.

Now that you understand how to find the width of a tree, you can apply this knowledge to various applications in data structures and algorithms. Experiment with different types of trees and explore other traversal techniques to deepen your understanding.