Traversal In A Binary Tree - Tree -3

Traversal In Binary Tree

  1. Traversing a binary tree means visiting each node of the tree exactly once.
  2. At each node :
    • N : Visit the node
    • L : Traverse its left subtree
    • R : Traverse its right subtree
    • These actions can be performed in any order... (3! = 6 different ways of ordering the three tasks.)
    • Different orders:
      1. NRL
      2. NLR
      3. LNR
      4. LRN
      5. RNL
      6. RLN
    • But we follow the convention which states left subtree to be traversed before right sub tree. Thus we are left with three orders: NLR , LNR and LRN
    • The orders are named according the position of the node i.e., position of N in the order.
    • pre : NLR
    • in  : LNR
    • post: LRN
  3. PRE ORDER TRAVERSAL: ( NLR )
    • N : Visit the node
    • L : Traverse the left subtree
    • R : Traverse the right subtree

  4. IN ORDER TRAVERSAL: ( LNR )
    • L : Traverse the left subtree
    • N : Visit the node
    • R : Traverse the right subtree

  5. POST ORDER TRAVERSAL: ( LRN )
    • L : Traverse the left subtree
    • R : Traverse the right subtree
    • N : Visit the node

Comments

Popular

Tree data structure - 2

Pre Order Traversal In Binary Tree