Preorder Traversal of Binary Tree in Python
Last Updated :
22 Feb, 2025
Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:
- The root node of the subtree is visited first.
- Then the left subtree is traversed.
- At last, the right subtree is traversed.
Consider the following tree:
If we perform a preorder traversal in this binary tree, then the traversal will be as follows:
Step-by-step approach:
- Step 1: At first the root will be visited, i.e. node 1.
- Step 2: After this, traverse in the left subtree. Now the root of the left subtree is visited i.e., node 2 is visited.
- Step 3: Again the left subtree of node 2 is traversed and the root of that subtree i.e., node 4 is visited.
- Step 4: There is no subtree of 4 and the left subtree of node 2 is visited. So now the right subtree of node 2 will be traversed and the root of that subtree i.e., node 5 will be visited.
- Step 5: The left subtree of node 1 is visited. So now the right subtree of node 1 will be traversed and the root node i.e., node 3 is visited.
- Step 6: Node 3 has no left subtree. So the right subtree will be traversed and the root of the subtree i.e., node 6 will be visited. After that there is no node that is not yet traversed. So the traversal ends.
So the order of traversal of nodes is 1 -> 2 -> 4 -> 5 -> 3 -> 6.
Examples of Preorder Traversal
Input:
Output: ABC
Explanation: The Preorder traversal visits the nodes in the following order: Root, Left, Right. Therefore, we visit the root node A, then visit the left node B and lastly the right node C
Input:
Output: ABDEC
Input: NULL
Output:
Output is empty in this case.
Algorithm for Preorder Traversal of Binary Tree
The algorithm for preorder traversal is shown as follows:
Preorder(root):
- If root is NULL then return
- Process root (For example, print root’s data)
- Preorder (root -> left)
- Preorder (root -> right)
Python Program to Implement Preorder Traversal of Binary Tree
Below is the code implementation of the preorder traversal:
Python
# Python program for preorder traversals
# Structure of a Binary Tree Node
class Node:
def __init__(self, v):
self.data = v
self.left = None
self.right = None
# Function to print preorder traversal
def printPreorder(node):
if node is None:
return
# Deal with the node
print(node.data, end=' ')
# Recur on left subtree
printPreorder(node.left)
# Recur on right subtree
printPreorder(node.right)
# Driver code
if __name__ == '__main__':
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.right = Node(6)
# Function call
print("Preorder traversal of binary tree is:")
printPreorder(root)
OutputPreorder traversal of binary tree is:
1 2 4 5 3 6
Complexity Analysis:
Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.
Auxiliary Space:
- O(1) if no recursion stack space is considered.
- Otherwise, O(h) where h is the height of the tree
- In the worst case, h can be the same as N (when the tree is a skewed tree)
- In the best case, h can be the same as logN (when the tree is a complete tree)
Related Posts: