Tree Traversal in Data structure | Inorder traversal | Preorder Traversal |Post order Traversal

 

Tree Traversal

  • A Tree is a non-linear data structure in which elements are arranged in a hierarchical structure.
  • Tree Traversal is the process of visiting each node of a tree exactly once in a particular order.
  • Tree traversal is used to process, search, display and perform operations on the nodes of a tree.
Types of Tree Traversal
  1. Preorder Traversal (Root → Left → Right)
  2. Inorder Traversal (Left → Root → Right)
  3. Postorder Traversal (Left → Right → Root)
Preorder Traversal :
In Preorder Traversal, the root node is visited first, followed by the left subtree and then the right subtree.

Order
Root → Left → Right

Example

        A
       / \
      B   C
     / \
    D   E

Step 1: Visit A
Step 2: Move to the left subtree and visit B
Step 3: Visit B's left child D
Step 4: Visit B's right child E
Step 5: Move to the right subtree and visit C

Therefore, Preorder = A → B → D → E → C

Preorder Algorithm

Preorder(node)
  If node is not NULL
       Visit node
       Preorder(node → left)
       Preorder(node → right)


Inorder Traversal:
In Inorder Traversal, the left subtree is visited first, followed by the root node and then the right subtree.

Order
Left → Root → Right

Example

        A
       / \
      B   C
     / \
    D   E

Step 1: Move to the leftmost node D and visit D
Step 2: Return to B and visit B
Step 3: Visit B's right child E
Step 4: Return to A and visit A
Step 5: Move to the right subtree and visit C

Therefore, Inorder = D → B → E → A → C

Inorder Algorithm

Inorder(node)
   If node is not NULL
        Inorder(node → left)
        Visit node
         Inorder(node → right)


Postorder Traversal:
In Postorder Traversal, the left subtree is visited first, followed by the right subtree, and the root node is visited last.

Order
Left → Right → Root

Example

        A
       / \
      B   C
     / \
    D   E
Step 1: Visit D
Step 2: Visit E
Step 3: Visit B
Step 4: Visit C
Step 5: Finally visit A

Therefore, Postorder = D → E → B → C → A

Postorder Algorithm
Postorder(node)
     If node is not NULL
         Postorder(node → left)
         Postorder(node → right)
         Visit node


Applications of Tree Traversal
  • Preorder: Used to create a copy of a tree and represent the structure of a tree.
  • Inorder: Used in Binary Search Trees to obtain elements in sorted order.
  • Postorder: Useful when deleting a tree because child nodes can be processed before the parent node.
  • Tree traversal is also used in expression trees, searching, file systems and other hierarchical data processing.

Read Next Topics:
Tree Traversal → BST → B Tree → B+ Tree

Handwritten Notes- Download PDF

Video Explanation- Watch on YouTube




Comments

Popular posts from this blog

B+ Tree in DBMS | B+ Tree in Data Structure

Normalization in DBMS,(1NF,2NF,3NF,BCNF,4NF,5NF)

Hashing in Data Structure | Collision Resolution Techniques | Hashing in DBMS