-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added a C program to check if two binary trees are identical #225
Open
azmuth13
wants to merge
1
commit into
sat5297:master
Choose a base branch
from
azmuth13:suraj159
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
#define MAX(x, y) (((x) > (y)) ? (x) : (y)) | ||
#define MIN(x, y) (((x) < (y)) ? (x) : (y)) | ||
|
||
|
||
|
||
/* | ||
two binary trees are said to be identical | ||
if left subtree of first tree is exactly | ||
similar to left subtree of the 2nd tree | ||
and right subtree of first tree | ||
is exactly similar to right subtree | ||
of 2nd tree for each node of first tree | ||
*/ | ||
|
||
|
||
struct treenode{ | ||
int value; | ||
struct treenode *left; | ||
struct treenode *right; | ||
}; | ||
|
||
// allocates a node | ||
struct treenode *getnode() | ||
{ | ||
struct treenode *temp; | ||
temp = (struct treenode *)malloc(sizeof(struct treenode)); | ||
temp->left = NULL; | ||
temp->right=NULL; | ||
return temp; | ||
} | ||
|
||
// This insert is for BST (binary search tree) | ||
struct treenode *insert(struct treenode *root,int x) | ||
{ | ||
struct treenode *p, *q; | ||
q = getnode(); | ||
q->value = x; | ||
q->left = NULL; | ||
q->right = NULL; | ||
|
||
if(root == NULL) | ||
return (q); | ||
|
||
p = root; | ||
|
||
while(1) | ||
{ | ||
if(x<p->value) | ||
{ | ||
if(p->left == NULL) | ||
break; | ||
p = p->left; | ||
} | ||
else | ||
{ | ||
if(p->right == NULL) | ||
break; | ||
p=p->right; | ||
} | ||
} | ||
|
||
if(x<p->value) | ||
p->left = q; | ||
else | ||
p->right = q; | ||
|
||
return (root); | ||
} | ||
|
||
void printInorder(struct treenode *root) | ||
{ | ||
if(root==NULL) | ||
return; | ||
|
||
printInorder(root->left); | ||
|
||
printf("%d ", root->value); | ||
|
||
printInorder(root->right); | ||
|
||
} | ||
|
||
|
||
int isIdentical(struct treenode *t1, struct treenode *t2) | ||
{ | ||
if(t1==NULL && t2 == NULL) | ||
return (1); | ||
|
||
if(t1==NULL) return 0; | ||
if(t2==NULL) return 0; | ||
|
||
if(t1->value == t2->value) | ||
if(isIdentical(t1->left,t2->left)) | ||
if(isIdentical(t1->right,t2->right)) | ||
return 1; | ||
|
||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
|
||
|
||
struct treenode *root2; | ||
root2 = getnode(); | ||
root2->value = 2; | ||
root2->left = getnode(); | ||
root2->left->value = 1; | ||
root2->right = getnode(); | ||
root2->right->value = 4; | ||
printInorder(root2); | ||
printf("\n"); | ||
|
||
struct treenode *root3; | ||
root3 = getnode(); | ||
root3->value = 2; | ||
root3->left = getnode(); | ||
root3->left->value = 1; | ||
root3->right = getnode(); | ||
root3->right->value = 4; | ||
printInorder(root3); | ||
printf("\n"); | ||
|
||
if(isIdentical(root2,root3)) | ||
printf("Yes equal\n"); | ||
else | ||
printf("Not equal\n"); | ||
|
||
return 0; | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check these conditions once plz. All of these should have been satisfied. see once,