Binary Tree Traversals šŸŽÆ

beginner
24 min

Binary Tree Traversals šŸŽÆ

Welcome to our deep dive into Binary Tree Traversals! In this lesson, we'll explore how to traverse a binary tree, a fundamental data structure used in computer science. Let's get started!

What is a Binary Tree? šŸ“

A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. This structure is used to represent a hierarchical set of items, making it perfect for organizing data in a way that's easy to search, insert, and delete.

Binary Tree Traversals šŸ’”

There are four main ways to traverse a binary tree:

  1. Inorder Traversal
  2. Preorder Traversal
  3. Postorder Traversal
  4. Level Order Traversal

Let's understand each one with a simple example.

Inorder Traversal šŸ“

In an inorder traversal, the left subtree is visited first, followed by the root, and finally the right subtree. This results in a traversal that visits nodes in ascending order.

Example: Inorder Traversal of a Binary Tree

python
class Node: def __init__(self, key): self.left = None self.right = None self.val = key def inorder(root): if root: inorder(root.left) print(root.val, end=" ") inorder(root.right) # Example usage root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) inorder(root) # Output: 4 2 5 1 3

Preorder Traversal šŸ“

In a preorder traversal, the root node is visited first, followed by the left subtree, and finally the right subtree. This results in a traversal that visits nodes in the order of root, left subtree, right subtree.

Example: Preorder Traversal of a Binary Tree

python
def preorder(root): if root: print(root.val, end=" ") preorder(root.left) preorder(root.right) # Example usage (same as Inorder Traversal example) preorder(root) # Output: 1 2 4 5 3

Postorder Traversal šŸ“

In a postorder traversal, the left subtree is visited first, followed by the right subtree, and finally the root node. This results in a traversal that visits nodes in the order of left subtree, right subtree, root.

Example: Postorder Traversal of a Binary Tree

python
def postorder(root): if root: postorder(root.left) postorder(root.right) print(root.val, end=" ") # Example usage (same as Inorder Traversal example) postorder(root) # Output: 4 5 2 3 1

Level Order Traversal šŸ“

In a level order traversal, nodes are visited by level, starting from the root level and moving down to the leaf nodes. This results in a traversal that visits nodes in the order of level by level.

Example: Level Order Traversal of a Binary Tree

python
def levelOrder(root): if not root: return queue = [root] while queue: current = queue.pop(0) print(current.val, end=" ") if current.left: queue.append(current.left) if current.right: queue.append(current.right) # Example usage (same as Inorder Traversal example) levelOrder(root) # Output: 1 2 3 4 5

Quiz Time! šŸŽÆ

Quick Quiz
Question 1 of 1

What is the order of traversal in an Inorder Traversal?

By now, you should have a good understanding of binary tree traversals. Mastering these techniques will not only help you navigate through complex data structures but also make your coding more efficient. Happy coding! šŸš€

Stay tuned for our next lesson, where we'll dive deeper into binary trees and explore topics like tree depth, height, and balancing! 🌲