

BINARY SEARCH TREE COMPARE
#include<stdio.h>
#include<stdlib.h>
typedef struct bstree
{
int data;
struct bstree *left,*right;
}bstree;
int main()
{
bstree *get_node(int );
bstree *create_bstree_wt_rc();
void compare_2_bst(bstree *,bstree *);
void insert_bstree_wt_rc(bstree *, bstree *);
bstree *root1=NULL,*root2=NULL;
printf("\nEnter the 1st BST:\n\n");
root1=create_bstree_wt_rc();
printf("\nEnter the 2nd BST:\n\n");
root2=create_bstree_wt_rc();
compare_2_bst(root1,root2);
return 1;
}
bstree *get_node(int x)
{
bstree *p;
p=(bstree*)malloc(sizeof (bstree));
p->data=x;
p->left=NULL;
p->right=NULL;
return p;
}
bstree *create_bstree_wt_rc()
{
void insert_bstree_wt_rc(bstree *, bstree *);
bstree *root,*new_node;
int x;
printf("Enter the root element: ");
scanf("%d",&x);
root=get_node(x);
printf("(Enter[-999]for stop insertion process) Enter another data: ");
scanf("%d",&x);
while(x!=-999)
{
new_node=get_node(x);
insert_bstree_wt_rc(root,new_node);
printf("(Enter[-999]for stop insertion process) Enter another data: ");
scanf("%d",&x);
}
return root;
}
void insert_bstree_wt_rc(bstree *root, bstree *new_node)
{
bstree *p,*q;
p=root;
q=root;
while(p!=NULL)
{
q=p;
if(new_node->data<p->data)
p=p->left;
else
p=p->right;
}
if(new_node->data<q->data)
q->left=new_node;
else
q->right=new_node;
}
void compare_2_bst(bstree *root1,bstree *root2)
{
bstree *p,*q,*stack1[50],*stack2[50];
int top1=-1,top2=-1;
if(root1==NULL||root2==NULL)
{
printf("\nThe two bst are not equeal.\n\n");
return;
}
p=root1;
q=root2;
while(1)
{
while(p!=NULL&&q!=NULL)
{
if(p->data!=q->data)
{
printf("\nThe two bst are not equeal.\n\n");
return;
}
if(p->left!=NULL&&q->left!=NULL)
{
if(p->left->data!=q->left->data)
{
printf("\nThe two bst are not equeal.\n\n");
return;
}
}
if(p->right!=NULL&&q->right!=NULL)
{
if(p->right->data!=q->right->data)
{
printf("\nThe two bst are not equeal.\n\n");
return;
}
}
if(p->right!=NULL)
{
top1++;
stack1[top1]=p->right;
}
p=p->left;
if(q->right!=NULL)
{
top2++;
stack2[top2]=q->right;
}
q=q->left;
}
if(top1==-1||top2==-1)
break;
p=stack1[top1];
top1--;
q=stack2[top2];
top2--;
}
if(top1==-1&&top2==-1)
printf("\n[ THE TWO BINARY SEARCH TREE ARE EQUAL ].\n\n");
else
printf("\nThe two bst are not equeal.\n\n");
}
