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

//

Angela Bailey

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

When working with trees in data structures, finding the height of a tree is a common problem. The height of a tree refers to the length of the longest path from the root node to any leaf node in the tree. In this article, we will explore different methods to find the height of a tree.

## Method 1: Recursion

Recursion is a powerful technique that can be used to solve many tree-related problems. To find the height of a tree using recursion, we can use the following algorithm:

1. Base case: If the tree is empty (i.e., there are no nodes), return 0.
2. Recursive case: If the tree is not empty, calculate the heights of its left and right subtrees recursively.
3. Return: Return the maximum height between the left and right subtrees, plus 1 for the current node.

This algorithm works because at each step, we calculate the heights of both subtrees and choose the maximum height. By adding 1 for each level, we account for the current node itself.

### Pseudocode:

```function findHeight(node):
if node is null:
return 0
else:
leftHeight = findHeight(node.left)
rightHeight = findHeight(node.right)
return max(leftHeight, rightHeight) + 1
```

To implement this algorithm in code, you can use any programming language that supports recursion. Here’s an example implementation in Python:

```def find_height(node):
if node is None:
return 0
else:
left_height = find_height(node.left)
right_height = find_height(node.right)
return max(left_height, right_height) + 1
```

## Method 2: Iterative Approach

An alternative approach to finding the height of a tree is by using an iterative method. This method uses a queue to perform a level-order traversal of the tree and keeps track of the current level.

The algorithm for finding the height of a tree iteratively can be summarized as follows:

1. Initialize: Set the height to 0 and create an empty queue.
2. Enqueue: Add the root node to the queue.
3. While loop: While the queue is not empty, perform the following steps:
• Dequeue: Remove a node from the front of the queue.
• Increase height: Increment the height by 1.
• Enqueue children: Add all non-null children of the dequeued node to the queue.
4. Return: Return the final height value.

This algorithm iteratively traverses each level of the tree and increases the height by 1 for each level. By enqueueing all non-null children, we ensure that we visit all nodes in each level before moving on to the next level.

```function findHeight(root):
if root is null:
return 0
else:
height = 0
queue = new Queue()
queue.enqueue(root)

while queue is not empty:
currentLevelSize = queue.size()

for i from 0 to currentLevelSize - 1:
currentNode = queue.dequeue()

if currentNode.left is not null:
queue.enqueue(currentNode.left)

if currentNode.right is not null:
queue.right)

height += 1

return height
```

Here’s an example implementation of the iterative approach in Java:

```public int findHeight(Node root) {
if (root == null) {
return 0;
} else {
int height = 0;

while (!queue.isEmpty()) {
int currentLevelSize = queue.size();

for (int i = 0; i < currentLevelSize; i++) {
Node currentNode = queue.poll();

if (currentNode.left != null) {
}

if (currentNode.right != null) {
queue.right);
}
}

height++;
}

return height;
}
}
```

## Conclusion

In this article, we explored two methods to find the height of a tree in data structures. The recursive method uses a simple algorithm that calculates the heights of left and right subtrees recursively. The iterative method leverages a level-order traversal using a queue to visit each level of the tree and increment the height by 1 for each level.

Both methods are effective in finding the height of a tree, and the choice between them depends on factors such as programming language preference, efficiency requirements, and personal coding style. Regardless of the method chosen, understanding how to find the height of a tree is an essential skill when working with trees in data structures.