-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.c
More file actions
298 lines (234 loc) · 5.82 KB
/
binary_tree.c
File metadata and controls
298 lines (234 loc) · 5.82 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// ODEV 1 - BINARY AGAC VERI YAPISI :
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
typedef struct node Node;
Node * queue[1000];
int front=0;
int rear=-1;
Node* newNode(int data){
Node* temp = (Node*)malloc(sizeof(Node));
temp->key = data;
temp->left = temp->right = NULL;
return temp;
}
Node *insertRight(Node *new, int data){
if (new==NULL){
new = newNode(data);
}
else{
Node * temp = new;
if (temp->right != NULL)
{
temp->right = insertRight(temp->right, data);
}
else{
temp->right = newNode(data);
}
}
return new;
}
Node *insertLeft(Node *new, int data){
if (new==NULL){
new = newNode(data);
}
else{
Node * temp = (Node*)malloc(sizeof(Node));
temp = new;
if (temp->left != NULL){
temp->left = insertLeft(temp->left, data);
}
else{
temp->left = newNode(data);
}
}
return new;
}
void printPreorder( Node* node){
if (node == NULL)
return;
printf("%d ", node->key);
printPreorder(node->left);
printPreorder(node->right);
}
void printInorder(Node* node){
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->key);
printInorder(node->right);
}
void printPostorder(Node* node)
{
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->key);
}
void enqueue(Node *root){
if(root != NULL){
rear++;
queue[rear] = root;
}
}
void dequeue(){
if(rear>=front){
Node *root = queue[front];
if(root != NULL) {
printf("%d ", root->key);
}
front++;
enqueue(root->left);
enqueue(root->right);
}
}
void printBFS(Node *node){
if(node != NULL){
enqueue(node);
do{
dequeue(node);
}while(front <= rear);
}
}
int main()
{
Node* root = ( Node*)malloc(sizeof( Node));
root = NULL;
root = insertRight(root,1);
insertRight(root,2);
insertLeft(root,3);
insertLeft(root,5);
insertRight(root->left,7);
insertRight(root,4);
insertLeft(root->right,6);
printf("Preorder: ");
printPreorder(root);
printf("\n");
printf("Inorder : ");
printInorder(root);
printf("\n");
printf("Postorder: ");
printPostorder(root);
printf("\n");
printf("BFS : ");
printBFS(root);
return 0;
}
// ODEV 2 - INDEX TABLOSU :
#include <stdio.h>
#include <stdlib.h>
struct binarySearchTree {
int index;
struct binarySearchTree *left, *right;
};
typedef struct binarySearchTree bst;
int ogrenci[15] = {18060311,20060045,19061091,20060134,20060678,18061086,20060032,20060067,19060456,18060245,20060110,20060234,20060141,20060011,20060012};
int puan[15] = {40,50,75,90,40,75,50,60,60,75,40,90,60,50,60};
bst* kayitYarat(int index)
{
bst *p;
p = (bst*)malloc(sizeof(bst));
p->index = index;
p->left = NULL;
p->right = NULL;
return p;
}
bst* ekle(bst *root, int index)
{
if(root==NULL)
return kayitYarat(index);
else if(puan[root->index] > puan[index])
root->left = ekle(root->left, index);
else if(puan[root->index] < puan[index])
root->right = ekle(root->right, index);
else
if(ogrenci[root->index] < ogrenci[index])
root->left = ekle(root->left, index);
else
root->right = ekle(root->right, index);
return root;
}
void siraliYazdir(bst *root, int grade)
{
if(root != NULL) {
siraliYazdir(root->right, grade);
if (grade != puan[root->index])
printf("%d \t %d \t %d\n", root->index, ogrenci[root->index], puan[root->index]);
siraliYazdir(root->left, grade);
}
}
bst* min(bst *root){
bst* current = root;
while (current && current->left != NULL)
current = current->left;
return current;
}
bst* sil(bst* node , int i){
if(node == NULL)
return NULL;
else if (puan[i] < puan[node->index])
node->left = sil(node->left, i);
else if (puan[i] > puan[node->index])
node->right = sil(node->right, i);
else {
if (i == node->index) {
if (node->left == NULL) {
bst* temp = node->right;
free(node);
return temp;
}
else if (node->right == NULL) {
bst* temp = node->left;
free(node);
return temp;
}
bst* temp = min(node->right);
node->index = temp->index;
node->right = sil(node->right, temp->index);
}
else {
if (ogrenci[i] <= ogrenci[node->index])
node->right = sil(node->right, i);
else
node->left = sil(node->left, i);
}
}
return node;
}
void degistir(bst* root, int i, int yeniPuan){
sil(root,i);
puan[i] = yeniPuan ;
ekle(root,i);
}
void yazdir(bst* node , int grade){
if (node == NULL) {
return;
}
else if (grade < puan[node->index])
yazdir(node->left, grade);
else if (grade > puan[node->index])
yazdir(node->right, grade);
else
siraliYazdir(node->right, grade);
}
int main()
{
bst* root;
root = kayitYarat(0);
for (int i = 1; i < 15; i++) {
ekle(root, i);
}
//sil(root, 0);
//printf("0 indeksi silindi\n");
//ekle(root,0);
//printf("0 indeksi eklendi\n");
//degistir(root,2,100);
//printf("2 indeksinin puanı 100 ile değişti\n");
printf("Index \t Öğrenci No \t Puan\n");
yazdir(root, 50);
return 0;
}