forked from masterishaan19/Codeforces_CompCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary tree comparison.cpp
More file actions
132 lines (131 loc) · 2.58 KB
/
binary tree comparison.cpp
File metadata and controls
132 lines (131 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
node *left, *right;
node *parent;
}node;
node *create()
{
node *root = NULL;
node *temp = NULL;
printf("Keep Entering Data till -1 is input.....\n");
int val;
while(1)
{
scanf("%d",&val);
if(val == -1)
break;
else
{
temp = root;
while(1)
{
if(temp != NULL)
{
if(val < temp->data)
{
if(temp->left == NULL)
{
node *newn;
newn = (node *)malloc(sizeof(node));
newn->data = val;
newn->left = NULL;
newn->right = NULL;
temp->left = newn;
newn->parent = temp;
break;
}
else
temp = temp->left;
}
else if(val > temp->data)
{
if(temp->right == NULL)
{
node *newn;
newn = (node *)malloc(sizeof(node));
newn->data = val;
newn->left = NULL;
newn->right = NULL;
temp->right = newn;
newn->parent = temp;
break;
}
else
temp = temp->right;
}
else
{
printf("\t\tUnique Number Please...... \n");
break;
}
}
else
{
root = (node *)malloc(sizeof(node));
root->data = val;
root->left = NULL;
root->right = NULL;
root->parent = NULL;
break;
}
}
}
}
return root;
}
void display(node *root)
{
if(root != NULL)
{
display(root->left);
printf("%d\n",root->data);
display(root->right);
}
}
int compare(node *root, node *toor)
{
if(root != NULL && toor != NULL)
{
if(root->data == toor->data)
{
int x;
x = compare(root->left, toor->left);
if(x == 1)
{
x = compare(root->right, toor->right);
if(x == 1)
return 1;
else
return 0;
}
return 0;
}
return 0;
}
else
{
if (root != NULL || toor != NULL)
{
return 0;
}
return 1;
}
}
int main()
{
node *root, *toor;
root = create();
toor = create();
printf("First binary tree is : \n");
display(root);
printf("Second bianry tree is : \n");
display(toor);
int result;
result = compare(root, toor);
if(result == 1)
printf("Yes, binary trees are equal...\n");
else
printf("No ,binary trees are not equal....\n");
}