×

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree.

  1. In-order traversal
  2. Pre-order traversal
  3. Post-order traversal

In-order traversal: 

In the in-order traversal method, the left child and left subtree are traversed first, afterward the root tree and then the right children or the right subtree are traversed.

Algorithm of In-order traversal

In-order-traversal (tree)
Step 1: Start with left sub-tree      // call In-order (left subtree) 
Step 2: Then, root tree 
Step 3: And then, right sub-tree    // call In-order (right subtree)

Example: Find the in-order traversal for this tree.

Traversal of binary tree

Solution.

Traversal of binary tree

Step 1: Left sub-tree is 1 ? 4 ? 9

Step 2: Root node is 5

Step 3: Right sub-tree is 5 ? 7 ? 2 ? 6 ? 3    

In-order Traversal = 1 ? 4 ? 9 ? 5 ? 5 ? 7 ? 2 ? 6 ? 3

Pre-order Traversal:

In the pre-order traversal method, the root node is traversed first, then the left subtree, and then the right subtree is traversed.

Algorithm of pre-order traversal

Pre-order-traversal (tree) 
Step 1: Start with the root node    
Step 2: Then, the left sub-tree       // call Pre-order (left subtree) 
Step 3: And then, right sub-tree    // call Pre-order (right subtree)

Example: Find the pre-order traversal for this tree.

Traversal of binary tree

Solution.

Traversal of binary tree

 Step 1: Root node is 5

Step 2: Left sub-tree is 4 ? 1 ? 9

Step 3: Right sub-tree is 6 ? 7 ? 5 ? 2 ? 3    

Pre-order Traversal = 5 ? 4 ? 1 ? 9 ? 6 ? 7 ? 5 ? 2 ? 3

Post-order traversal:

In the post-order traversal method, the left child and left subtree are traversed first, then the right subtree is traversed, and then the root node.

Algorithm of Post-order traversal

Post-order-traversal (tree)
Step 1: Start with left sub-tree        // call Post-order (left subtree) 
Step 2: Then, right sub-tree           // call Post-order (right subtree) 
Step 3: And then, root tree

Example: Find the Post-order traversal for this tree.

Traversal of binary tree

Solution.

Traversal of binary tree
Step 1: Left sub-tree is 1 ? 9 ? 4
Step 2: Right sub-tree is 5 ? 2 ? 7 ? 3 ? 6
Step 3: Root node is 5    
Post-order Traversal = 1 ? 9 ? 4 ? 5 ? 2 ? 7 ? 3 ? 6 ? 5

Applications of binary tree

  1. The binary search tree is used in many search applications.
  2. Nowadays, a binary Space Partition is used for every 3D game.3.
  3. The binary tree is used in every high bandwidth router that stores the router table.

Binary tree program in C language

// Binary Tree in C   
#include <stdio.h> 
#include <stdlib.h>   
struct node 
{   
int data;   
struct node *left;   
struct node *right; 
}; 
struct node *newNode(int data) 
{   
struct node *node = (struct node *)malloc(sizeof(struct node));   
node->data = data;   node->left = NULL;   
node->right = NULL;   return (node); }   
void traversePreOrder(struct node *t)
 {  
 if (t != NULL) 
  {   
  printf(" %d", t->data);  
   traversePreOrder(t->left); 
    traversePreOrder(t->right); 
  } 
}  
 void traverseInOrder(struct node *t)
 {  
 if (t != NULL) 
  {    
 traverseInOrder(t->left); 
    printf(" %d", t->data);  
   traverseInOrder(t->right);
  }
 } 
  void traversePostOrder(struct node *t)
 {  
 if (t != NULL) 
  {    
 traversePostOrder(t->left); 
    traversePostOrder(t->right); 
    printf(" %d", t->data); 
  } 
}   
int main() 
{  
 struct node *root = newNode(1); 
  root->left = newNode(2);  
 root->right = newNode(3); 
  root->left->left = newNode(4); 
  printf("The preorder traversal of the tree is: "); 
  traversePreOrder(root);   
printf("\nThe inorder traversal of the tree is: "); 
  traverseInOrder(root);   
printf("\nThe postorder traversal of the tree is: ");  
 traversePostOrder(root); }  

Related Topics

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Red Black Tree

Red Black Tree A red-black tree is referred as self-balancing binary search tree. The tree was invented by Rudolf Bayer in 1972. In red-black, each node stores an extra bit that...

8 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Binary Search Tree vs AVL Tree: Data Structure

Difference Between Binary Search Tree and AVL Tree Binary Search Tree: The binary search tree is a kind of binary tree data structure and it follows the conditions of binary...

3 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.