In a binary tree, traversal refers to the process of visiting each node exactly once in a systematic way. There are several common traversal strategies, each of which can be implemented using recursion or iteration (often with the help of a stack or queue). Here are the most well-known binary tree traversal methods:
1. Preorder Traversal (Root, Left, Right)
In preorder traversal, the nodes are visited in the following order:
- Visit the root node.
- Traverse the left subtree in preorder.
- Traverse the right subtree in preorder.
Algorithm:
preorder(node)
if node == null return
visit(node)
preorder(node.left)
preorder(node.right)
2. Inorder Traversal (Left, Root, Right)
In inorder traversal, the nodes are visited as follows:
- Traverse the left subtree in inorder.
- Visit the root node.
- Traverse the right subtree in inorder.
This traversal is commonly used on binary search trees because it returns values in non-decreasing order.
Algorithm:
inorder(node)
if node == null return
inorder(node.left)
visit(node)
inorder(node.right)
3. Postorder Traversal (Left, Right, Root)
In postorder traversal, the nodes are visited in the following sequence:
- Traverse the left subtree in postorder.
- Traverse the right subtree in postorder.
- Visit the root node.
Algorithm:
postorder(node)
if node == null return
postorder(node.left)
postorder(node.right)
visit(node)
4. Level-order Traversal (Breadth-First)
Level-order traversal visits nodes level by level from top to bottom and from left to right. This traversal is also known as breadth-first traversal.
Algorithm (using a queue):
levelOrder(root)
if root == null return
queue <- empty queue
queue.enqueue(root)
while queue is not empty
node <- queue.dequeue()
visit(node)
if node.left != null
queue.enqueue(node.left)
if node.right != null
queue.enqueue(node.right)
How to Implement Traversal Functions
The traversal algorithms above use a visit(node) function, which represents the action taken when a node is visited, such as printing the node's value.
In a real implementation, you would likely define a TreeNode class (or similar) for nodes in the binary tree, and you would pass the root node of the tree to the traversal function to start the process.
Here's an example of a simple binary tree node class in Python:
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def visit(self):
print(self.value)
You can then use this TreeNode class to create your binary tree and call the traversal algorithms accordingly. For instance:
def preorder(node):
if node is None:
return
node.visit()
preorder(node.left)
preorder(node.right)
# Create binary tree
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
# Perform preorder traversal
preorder(root)
Remember that while the recursive implementations are more straightforward and elegant, they can lead to a stack overflow with very deep trees. Iterative implementations using stacks or queues avoid this issue and are generally preferred for very large trees.