![]() |
VOOZH | about |
Tree traversal refers to the process of visiting or accessing each node of a tree exactly once in a specific order. Unlike linear data structures such as arrays, linked lists, or queues (which have only one logical way of traversal), trees offer multiple ways to traverse their nodes.
Tree traversals are broadly classified into two categories:
π tree_traversal_techniquesDepth-First Traversal (DFT)
Breadth-First Traversal (BFT)
Inorder traversal visits the node in the order: Left -> Root -> Right
Algorithm for Inorder Traversal
Inorder(tree )
β Traverse the left subtree, i.e., call Inorder(left->subtree)
β Visit the root.
β Traverse the right subtree, i.e., call Inorder(right->subtree)
Uses of Inorder Traversal
Also Check: Refer Inorder Traversal of Binary Tree for more
Preorder traversal visits the node in the order: Root -> Left -> Right
Algorithm for Preorder Traversal
Preorder(tree)
β Visit the root.
β Traverse the left subtree, i.e., call Preorder(left->subtree)
β Traverse the right subtree, i.e., call Preorder(right->subtree)
Uses of Preorder Traversal
Also Check: Refer Preorder Traversal of Binary Tree for more
Postorder traversal visits the node in the order: Left -> Right -> Root
Algorithm for Postorder Traversal:
Postorder(tree)
βTraverse the left subtree, i.e., call Postorder(left->subtree)
β Traverse the right subtree, i.e., call Postorder(right->subtree)
β Visit the root
Uses of Postorder Traversal
Also Check: Refer Postorder Traversal of Binary Tree for more
Level Order Traversal visits all nodes present in the same level completely before visiting the next level.
Algorithm for Level Order Traversal
LevelOrder(tree)
β Create an empty queue Q
β Enqueue the root node of the tree to Q
β Loop while Q is not empty
βDequeue a node from Q and visit it
β Enqueue the left child of the dequeued node if it exists
β Enqueue the right child of the dequeued node if it exists .
Uses of Level Traversal
Also Check: Refer Level Order Traversal (Breadth First Search or BFS) of Binary Tree for more
Boundary Traversal of a Tree includes
Also Check: Refer Boundary Traversal of binary tree for more
In the Diagonal Traversal of a Tree, all the nodes in a single diagonal will be printed one by one.
π Diagonal-Traversal-of-binary-treeAlso Check: Refer Diagonal Traversal of Binary Tree for more