Traversing a binary tree means visiting each node of the tree exactly once. and as we know the nodes of the tree are arranged in a hierarchical order so there are different ways to traverse a binary tree.
we have three main tasks to traverse in the tree.
visit the root node.
then we traverse the left subtree of the root node in preorder
then we traverse the right subtree of the root node in preorder
Traverse the left subtree of the root node in inorder
then we visit the root node
then we traverse the right subtree of the root in inorder
Traverse the left subtree of the root node in postorder
Traverse the right subtree of the root node in postorder
visit the root node.
so we have a binary tree as you see in the given below image.

so the Preorder traversing is
Postorder traversing
In order traversing
Level order traversing
we have three main tasks to traverse in the tree.
- visiting the root node.
- visiting the left subtree.
- visiting the right subtree.
Types of traversing in a binary tree.
- Preorder
- Postorder
- Inorder
- Level order
Preorder Traversing
In preorder traversing first, wevisit the root node.
then we traverse the left subtree of the root node in preorder
then we traverse the right subtree of the root node in preorder
In order Traversing
In inorder traversing first, weTraverse the left subtree of the root node in inorder
then we visit the root node
then we traverse the right subtree of the root in inorder
Postorder traversing
In postorder traversing first, weTraverse the left subtree of the root node in postorder
Traverse the right subtree of the root node in postorder
visit the root node.
Level order Traversing
In level order traversing nodes are traversed level by level.so we have a binary tree as you see in the given below image.

so the Preorder traversing is
P A S T Q E D X M R C
Postorder traversing
T Q S D E A M C R X P
In order traversing
T S Q A E D P M X C R
P A X T Z M G Y L F C
Program to implement traversing in the binary tree using the python programming language.
from collections import deque class Node: def __init__(self, value): self.info = value self.lchild = None self.rchild = None class BinaryTree: def __init__(self): self.root = None def is_empty(self): return self.root is None def display(self): self._display(self.root, 0) print() def _display(self,p,level): if p is None: return self._display(p.rchild, level+1) print() for i in range(level): print(" ", end='') print(p.info) self._display(p.lchild, level+1) def preorder(self): self._preorder(self.root) print() def _preorder(self,p): if p is None: return print(p.info, " ", end='') self._preorder(p.lchild) self._preorder(p.rchild) def inorder(self): self._inorder(self.root) print() def _inorder(self,p): if p is None: return self._inorder(p.lchild) print(p.info," ", end='') self._inorder(p.rchild) def postorder(self): self._postorder(self.root) print() def _postorder(self,p): if p is None: return self._postorder(p.lchild) self._postorder(p.rchild) print(p.info," ",end='') def level_order(self): if self.root is None: print("Tree is empty") return qu = deque() qu.append(self.root) while len(qu) != 0: p = qu.popleft() print(p.info + " ", end='') if p.lchild is not None: qu.append(p.lchild) if p.rchild is not None: qu.append(p.rchild) def height(self): return self._height(self.root) def _height(self,p): if p is None: return 0 hL = self._height(p.lchild) hR = self._height(p.rchild) if hL > hR: return 1 + hL else: return 1 + hR def create_tree(self): self.root = Node('p') self.root.lchild = Node('Q') self.root.rchild = Node('R') self.root.lchild.lchild = Node('A') self.root.lchild.rchild = Node('B') self.root.rchild.lchild = Node('X') ########################## bt = BinaryTree() bt.create_tree() bt.display() print() print("Preorder : ") bt.preorder() print("") print("Inorder : ") bt.inorder() print() print("Postorder : ") bt.postorder() print() print("Level order : ") bt.level_order() print() print("Height of tree is ", bt.height())
Also, read these posts
- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Deletion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Circular queue
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures
- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Deletion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Circular queue
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures
0 Comments