# Windows 7Preorder Traversal of Binary Tree

#### Rishab7

##### New Member
Hi everyone,

I'm currently learning about binary tree traversals, and I'm having trouble understanding preorder traversal in binary tree. Can someone please explain it to me in a simple way?

I understand that preorder traversal visits the root node first, followed by the left subtree, and then the right subtree. But I'm not sure how to implement it recursively or iteratively.

Can someone please provide an example of both recursive and iterative implementations of preorder traversal?

Hello!

Preorder traversal in a binary tree is a method of visiting each node in the tree by following a specific order. In preorder traversal, you visit the root node first, followed by its left subtree, and then its right subtree.

Here's an example of how you can implement preorder traversal recursively:

```python
# Define the binary tree node structure
class Node:
def __init__(self, value):
self.data = value
self.left = None
self.right = None

# Preorder traversal function
def preorder_recursive(root):
if root is not None:
# Visit the root node
print(root.data, end=" ")
# Traverse the left subtree
preorder_recursive(root.left)
# Traverse the right subtree
preorder_recursive(root.right)
```

In the above code, we first check if the root is not None. If it's not None, we visit the root node and print its data. Then, we make a recursive call to traverse the left subtree and another recursive call to traverse the right subtree.

Now, let's look at an iterative implementation of preorder traversal using a stack data structure:

```python
# Preorder traversal function
def preorder_iterative(root):
if root is None:
return

stack = []
stack.append(root)

while stack:
current_node = stack.pop()
print(current_node.data, end=" ")

# Push the right child onto the stack first
if current_node.right:
stack.append(current_node.right)
# Push the left child onto the stack next
if current_node.left:
stack.append(current_node.left)
```

In the iterative implementation, we start by initializing an empty stack and pushing the root node onto the stack. We then enter a loop that continues until the stack is empty. In each iteration, we pop a node from the stack, print its data, and push its right and left children onto the stack (if any).

I hope this clarifies the concept and provides you with the examples you were looking for! Let me know if you have any further questions.

Hello!

Preorder traversal in a binary tree is a method of visiting each node in the tree by following a specific order. In preorder traversal, you visit the root node first, followed by its left subtree, and then its right subtree.

Here's an example of how you can implement preorder traversal recursively:

```python
# Define the binary tree node structure
class Node:
def __init__(self, value):
self.data = value
self.left = None
self.right = None

# Preorder traversal function
def preorder_recursive(root):
if root is not None:
# Visit the root node
print(root.data, end=" ")
# Traverse the left subtree
preorder_recursive(root.left)
# Traverse the right subtree
preorder_recursive(root.right)
```

In the above code, we first check if the root is not None. If it's not None, we visit the root node and print its data. Then, we make a recursive call to traverse the left subtree and another recursive call to traverse the right subtree.

Now, let's look at Bathroom Remodel Contractor Bellevue an iterative implementation of preorder traversal using a stack data structure:

```python
# Preorder traversal function
def preorder_iterative(root):
if root is None:
return

stack = []
stack.append(root)

while stack:
current_node = stack.pop()
print(current_node.data, end=" ")

# Push the right child onto the stack first
if current_node.right:
stack.append(current_node.right)
# Push the left child onto the stack next
if current_node.left:
stack.append(current_node.left)
```

In the iterative implementation, we start by initializing an empty stack and pushing the root node onto the stack. We then enter a loop that continues until the stack is empty. In each iteration, we pop a node from the stack, print its data, and push its right and left children onto the stack (if any).

I hope this clarifies the concept and provides you with the examples you were looking for! Let me know if you have any further questions.
Recursive implementation:
python
class TreeNode:
def __init__(self, value):
self.val = value
self.left = None
self.right = None

def preorder_recursive(root):
if root is None:
return
print(root.val, end=" ") # Visit the root node
preorder_recursive(root.left) # Traverse left subtree
preorder_recursive(root.right) # Traverse right subtree

# Example usage:
# Constructing a sample binary tree
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)

# Calling the preorder traversal function
print("Recursive Preorder Traversal:")
preorder_recursive(root)
Iterative implementation:
python
def preorder_iterative(root):
if root is None:
return
stack = [root] # Initialize stack with root node
while stack:
node = stack.pop() # Pop the top node from the stack
print(node.val, end=" ") # Visit the popped node
# Push right child first so that left child is processed first (LIFO)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)

# Example usage (using the same tree as above):
print("\n\nIterative Preorder Traversal:")
preorder_iterative(root)
Both implementations will traverse the binary tree in preorder, visiting the root node first, followed by its left subtree, and then its right subtree.