Algorithm to check two binary trees are identical
Algorithm:
1. Check if both trees are empty , return 1
2. If both trees are not empty:
a) Compare data of root node
b) Check left subtree recursively and compare
c) Check right subtree recursively and compare
d) If all above conditions return true, then return 1.
3. If any one of the tree is empty, return 0.
Implementation 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);
}
int identical(struct node *a, struct node *b)
{
if(a == NULL && b == NULL)
return 1;
else
{
if( a!= NULL && b!= NULL)
{
return ( a->data == b->data && identical(a->left,b->left) && identical(a->right, b->right) );
}
return 0;
}
}
int main()
{
struct node *root1 = newnode(1);
struct node *root2 = newnode(1);
root1->left = newnode(2);
root1->right = newnode(3);
root1->left->left = newnode(4);
root1->left->right = newnode(5);
root2->left = newnode(2);
root2->right = newnode(3);
root2->left->left = newnode(4);
root2->left->right = newnode(5);
if(identical(root1,root2))
printf("Both binary tree are same");
else
printf("Both binary tree are different");
}
Time complexity: It depends on the tree. Tree with less number of nodes determine the complexity of this algorithm. Lets say Tree1 has m nodes and Tree2 has n nodes and n<m. So the complexity will be O(n).
1. Check if both trees are empty , return 1
2. If both trees are not empty:
a) Compare data of root node
b) Check left subtree recursively and compare
c) Check right subtree recursively and compare
d) If all above conditions return true, then return 1.
3. If any one of the tree is empty, return 0.
Implementation 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);
}
int identical(struct node *a, struct node *b)
{
if(a == NULL && b == NULL)
return 1;
else
{
if( a!= NULL && b!= NULL)
{
return ( a->data == b->data && identical(a->left,b->left) && identical(a->right, b->right) );
}
return 0;
}
}
int main()
{
struct node *root1 = newnode(1);
struct node *root2 = newnode(1);
root1->left = newnode(2);
root1->right = newnode(3);
root1->left->left = newnode(4);
root1->left->right = newnode(5);
root2->left = newnode(2);
root2->right = newnode(3);
root2->left->left = newnode(4);
root2->left->right = newnode(5);
if(identical(root1,root2))
printf("Both binary tree are same");
else
printf("Both binary tree are different");
}
Time complexity: It depends on the tree. Tree with less number of nodes determine the complexity of this algorithm. Lets say Tree1 has m nodes and Tree2 has n nodes and n<m. So the complexity will be O(n).
Comments
Post a Comment