-
Notifications
You must be signed in to change notification settings - Fork 0
/
connecting-trees.c
118 lines (114 loc) · 2.63 KB
/
connecting-trees.c
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
#include <stdio.h>
#include <stdlib.h>
struct nodetype {
int info;
struct nodetype *father, *left, *right;
};
typedef struct nodetype *NODEPTR;
NODEPTR getnode(){
NODEPTR p;
p = (NODEPTR)malloc(sizeof(struct nodetype));
return p;
}
NODEPTR maketree(int x){
NODEPTR p;
p = getnode();
p->info = x;
p->father = NULL;
p->left = NULL;
p->right = NULL;
return p;
}
int treenumbers[100];
int i=0;
void pretrav(NODEPTR tree){
if(tree!=NULL){
treenumbers[i++] = tree->info;
pretrav(tree->left);
pretrav(tree->right);
}
}
void prettrav(NODEPTR tree){
if(tree!=NULL){
printf("%d-",tree->info);
prettrav(tree->left);
prettrav(tree->right);
}
}
void connect(NODEPTR t1, NODEPTR t2){//DLR
pretrav(t2);
while(i != -1){
int temp = treenumbers[i--];
NODEPTR helper1,helper2;
helper1 = t1;
while(helper1!=NULL){
helper2 = helper1;
if(temp>helper1->info)
helper1 = helper1->right;
else
helper1 = helper1->left;
}
if(temp>helper2->info)
helper2->right = maketree(temp);
else if(temp==helper2->info)
printf("duplicate\n");
else
helper2->left = maketree(temp);
}
prettrav(t1);
}
int main(){
//1.ağaç
NODEPTR p,q,temp;
int number;
scanf("%d",&number);
p = maketree(number);
while(number != -1){
scanf("%d",&number);
if(number==-1){
printf("birinci agac olusturuldu\n");
break;
}
temp = p;
while(temp!=NULL){
q = temp;
if(number > temp->info)
temp = temp->right;
else
temp = temp->left;
}
if(number > q->info)
q->right = maketree(number);
else if(number == q->info)
printf("duplicate\n");
else
q->left = maketree(number);
}
//2.ağaç
scanf("%d",&number);
NODEPTR p2;
p2 = maketree(number);
NODEPTR m1,m2;
while(number!=-1){
scanf("%d",&number);
if(number==-1)
break;
m1 = p2;
while(m1!=NULL){
m2 = m1;
if(number > m1->info)
m1 = m1->right;
else
m1 = m1->left;
}
if(number>m2->info)
m2->right = maketree(number);
else if(number==m2->info)
printf("duplicate\n");
else
m2->left = maketree(number);
}
//calling connect func
connect(p,p2);
return 0;
}