# Pre Order Traversal Of Binary Tree Nodes

Pre-order binary tree traversal is a technique used to visit all the nodes of a binary tree in the following order: First, the root node of the current tree is visited, then all nodes in the left subtree are visited in pre-order fashion, and finally all the nodes in the right subtree are visited in pre-order fashion. The animated examples discussed in the next section will make the definition more clear.

## Animation of Pre-Order Binary Tree Traversal

Let’s consider the below binary tree and apply the pre-order tree traversal technique:

Node `A` is the root node of the binary tree. We need to visit this node before visiting all the nodes under it’s left and right subtrees in pre-order fashion. Visited nodes will be coloured in green.

Now we need to visit all the nodes under the left subtree of node `A` i.e., subtree with root as node `B` in pre-order fashion. We apply the same logic again i.e, first visit the subtree’s root node (`B`), then visit all nodes in left subtree under node `B`, and then visit all nodes in right subtree under node `B`. So we visit node `B`, left subtree of node `B` contains a singe node `D` so we just visit that node and finally visit node `E`.

Now that all the nodes under the left subtree of node `A` are visited, we need to visit all nodes under the right subtree of node `A` in pre-order fashion.

This is the order in which we visited all the nodes: `A, B, D, E, C, F, G`.

## Recursive Implementation

Below is the implementation of a function which takes in the root node of a binary tree and traverses all the nodes in Pre-Order fashion. It uses recursion technique which is much simpler and straightforward than the iterative version.

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

def pre_order_traversal(node):
# Base case: if the node is None, return
if node is None:
return

# Step 1: Visit the current node
# print its value or perform any other operation
print(node.value, end=" ")

# Step 2: Traverse the left subtree recursively
pre_order_traversal(node.left)

# Step 3: Traverse the right subtree recursively
pre_order_traversal(node.right)

# Create a sample binary tree
def create_binary_tree():
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
return root

# Let's now test the pre_order_traversal function
if __name__ == "__main__":
tree_root = create_binary_tree()
print("Pre-Order Traversal:")
pre_order_traversal(tree_root)
``````

## Iterative Implementation

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

def iterative_pre_order_traversal(root):
if root is None:
return

# Initialize a stack to simulate recursion
stack = [root]

# Traverse the tree until the stack is empty
while stack:
# Pop the top node from the stack
node = stack.pop()

# Visit the node (print or perform any operation)
print(node.value, end=" ")

# Push the right child to the stack first
if node.right:
stack.append(node.right)

# Push the left child to the stack
# Since the stack is LIFO, the left child will be visited before the right child
if node.left:
stack.append(node.left)

# Create a sample binary tree
def create_binary_tree():
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
return root

# Iterative pre-order traversal usage
if __name__ == "__main__":
tree_root = create_binary_tree()
print("Iterative Pre-Order Traversal:")
iterative_pre_order_traversal(tree_root)
``````