Home » Binary Tree Preorder Traversal

Binary Tree Preorder Traversal

by Online Tutorials Library

Pre-order traversal

Steps

  • Visit the root node
  • traverse the left sub-tree in pre-order
  • traverse the right sub-tree in pre-order

Algorithm

  • Step 1: Repeat Steps 2 to 4 while TREE != NULL
  • Step 2: Write TREE -> DATA
  • Step 3: PREORDER(TREE -> LEFT)
  • Step 4: PREORDER(TREE -> RIGHT)
    [END OF LOOP]
  • Step 5: END

C Function

Example

Traverse the following binary tree by using pre-order traversal

binary tree Pre-order traversal

  • Since, the traversal scheme, we are using is pre-order traversal, therefore, the first element to be printed is 18.
  • traverse the left sub-tree recursively. The root node of the left sub-tree is 211, print it and move to left.
  • Left is empty therefore print the right children and move to the right sub-tree of the root.
  • 20 is the root of sub-tree therefore, print it and move to its left. Since left sub-tree is empty therefore move to the right and print the only element present there i.e. 190.
  • Therefore, the printing sequence will be 18, 211, 90, 20, 190.

Next TopicDoubly Linked List

You may also like