Skip to main content

In this tutorial we will explore one of the fundamental data structure in computer science: Binary Tree

What is a Binary Tree?

A binary tree is a hierarchical data structure composed of nodes, where each node has at most two children: a left child and a right child. The topmost node of the tree is called the root node. Each child node can have its own left and right children, forming sub-trees.

Representation of a Node Binary Tree​

Binary Tree is made up of nodes, where each node except leaf nodes have children. Each node in the tree consist of three parts i.e., data, left pointer, right pointer. To create a node we can follow the below structure of code:

//Creating a node in C++ using structure

typedef struct treetype
{
struct treetype *left;
int data;
struct treetype *right;
}node;

//Creating a node in C++ using class
class Node{
public:
Node *left;
int data;
Node *right;
};

Operation in Binary Tree​

1. Insert Operation:​

We can add a node in the Binary Tree as a left child or right child of any node.


Algorithm or steps to insert in a binary tree

  • First check if the root is null or not if root is null means this is the first node of the tree, make it as a root and return it else move to next step
  • Now as we have already studient queue data strucuture, we will use that to add nodes to the binary tree
  • Insert a node as the left or right child of the parent node if the parent node points to null
  • Return root to main function.

2. Traversal of Binary Tree:​

Traversal means visiting all nodes of the binary tree. Three famous binary tree traversal methods are:

  • Preorder Traversal (NLR) : In this traversal method we have to visit the current node before visiting its left or right subtree. The traversal is Node-left-right means first node (root node) is traveresed then left child and then right child
  • Inorder Traversal (LNR) : In this traversal method we have to visit all the nodes inside the left subtree first then visit the current node and then the nodes in the right subtree. The traversal is Left-Node-right.
  • Postorder Traversal (LRN) : In this traversal method we visit the current node after visiting all the nodes in left and right subtree. The traversal is in the order left-right-node.

3. Search in Binary Tree:​

We can search any element in the Binary Tree using the same algorithm as insertion just with some difference
Algorithm or steps to insert in a binary tree
*First check if the root is null or not if root is null return false to main else move to next step.
*Store the root in a queue, start a loop which ends when queue is empty.
*Take the front node (as temp) from queue check the temp value with the search value given if same return true to main else move to next step.
*Check if the temp left or temp right child are NULL or not , if null continue the loop, else push the left then right child in the queue.
*If the queue comes empty and element is not found return false to main.

Programming implementation of operation in Binary Tree​

#include<iostream>
#include<queue>
using namespace std;
class Node
{
public:
Node *left,*right;
int data;
Node(int val)
{
left=NULL;
data=val;
right=NULL;
}
};
//insert function for binary search
Node* insert(Node* root, int data)
{
if (root==NULL){
root=new Node(data);
return root;
}
//using queue data structure to find the position to insert the node
queue<Node*>store;
store.push(root);
while(!store.empty())
{
Node *temp=store.front();
store.pop();
//check for left and right child
if (temp->left==NULL)
{
temp->left=new Node(data);
break;
}
else
store.push(temp->left);
if (temp->right==NULL)
{
temp->right=new Node(data);
break;
}
else
store.push(temp->right);
}
return root;
}
//now traversals methods of binary tree
//PRE-ORDER traversal
void pre_traversal(Node* root)
{
if (root==NULL)
return;
cout<<root->data<<" ";
pre_traversal(root->left);
pre_traversal(root->right);
}
//IN-ORDER traversal
void in_traversal(Node *root)
{
if(root==NULL)
return ;
in_traversal(root->left);
cout<<root->data<<" ";
in_traversal(root->right);
}
//POST-ORDER traversal
void post_traversal(Node *root)
{
if (root==NULL)
return ;
post_traversal(root->left);
post_traversal(root->right);
cout<<root->data<<" ";
}
//search function for binary tree
bool search(Node *root, int value)
{
if (root==NULL)
return false;
queue<Node*>store;
store.push(root);
while(!store.empty())
{
Node *temp=store.front();
store.pop();
if (temp->data==value)
return true;
if (temp->left!=NULL)
{
store.push(temp->left);
}
if (temp->right!=NULL)
{
store.push(temp->right);
}
}
return false;
}
int main()
{
Node *root=NULL;
//insertion operation in binary tree
root=insert(root,1);
root=insert(root,2);
root=insert(root,3);
root=insert(root,4);
root=insert(root,5);
//traversal
//preorder traversal
cout<<"Preorder Traversal: ";
pre_traversal(root);
//inorder traversal
cout<<endl<<"Inorder Traversal: ";
in_traversal(root);
//postorder traversal
cout<<endl<<"Post-order Traversal:";
post_traversal(root);
//seraching a node in the binary tree
if (search(root,4))
cout<<endl<<"Node found in the binary tree";
else
cout<<endl<<"Node not found in the binary tree";
return 0;
}

Output:

Preorder Traversal: 1 2 4 5 3 
Inorder Traversal: 4 2 5 1 3
Post-order Traversal: 4 5 2 3 1
Node found in the binary tree