×

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

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 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.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

B+ Tree Program in Q language

A B+ tree is just an improvised version of a self-balancing and well-maintained tree in which all the key values that hold valuable information is present at the bottom, which...

9 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side In this problem, we have given a singly linked list, and we need to remove all...

3 minutes read.

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

Bitwise Operators and their Important Tricks

In most of the programs you write today, you deal with data types comprising bytes, such as integer, float, double, etc. Dealing with bytes? It is a quite normal task,...

5 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.

What is a Tree in Terms of a Graph?

To know the explanation of trees in terms of graphs, we need first to know what trees and graphs are. So let us first learn about trees and graphs. Trees and...

6 minutes read.

Preorder Traversal of Binary Trees

In general, Stack, Array, Queue, and other linear data structures only have one way to traverse the data. However, there are numerous ways to traverse through the data in a hierarchical...

3 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.