-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.c
219 lines (201 loc) · 3.68 KB
/
tree.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
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
/*
* Code for Binary Search Tree Operation using linked list
*
* Computer Engineering UiTM
* Muhammad Saufy Rohmad
*
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
}*root;
struct node* alloc(int);
void insert(struct node *,struct node*);
void read();
struct node* search(struct node*,int val);
int found=0;
void inorder(struct node *);
struct node* alloc(int n)
{
struct node *nw;
nw=(struct node*)malloc(sizeof(struct node));
nw->data=n;
nw->left=NULL;
nw->right=NULL;
}
void read()
{
struct node* temp=root;
while(temp)
{
printf("inside is %d \n",temp->data);
temp=temp->left;
}
}
void insert(struct node *root,struct node *x)
{
printf(" root,x and value x: %u : %d %u \n",root,x->data,x);
if(x->data<root->data)
{
if(root->left==NULL)
root->left=x;
else
insert(root->left,x);
}
else
{
if(root->right==NULL)
root->right=x;
else
insert(root->right,x);
}
}
struct node* search(struct node *root,int val)
{
if(root)
{
if(val<root->data)
search(root->left,val);
else if (val>root->data)
search(root->right,val);
else if(val==root->data)
return root;
}
}
struct node* searchparent(struct node* root,struct node *s)
{
if(root)
{
if(root==s)
return root;
searchparent(root->left,s);
searchparent(root->right,s);
}
}
void del(struct node *root,int val)
{
struct node *temp,*temp2;
temp=search(root,val);
if(temp->left==NULL && temp->right==NULL)
{
printf("This node dont have left and right child!\n");
temp2=searchparent(root,temp);
free(temp);
if(temp2->left==temp)temp2->left=NULL;
else temp2->right=NULL;
}
if(temp->left!=NULL && temp->right==NULL)
{
printf("This node have left child but without right child!\n");
/*
*
* TODO
*/
}
if(temp->left==NULL && temp->right!=NULL)
{
printf("This dont node have left child but have right child!\n");
/*
* TODO
*
*/
}
if(temp->left!=NULL && temp->right!= NULL)
{
printf("This node have left and right child!\n");
temp2=searchparent(root,temp);
/*
* TODO
*
*/
}
}
void inorder(struct node *root)
{
printf("ROOT DEBUG %u \n",root);
if(root)
{
inorder(root->left);
printf("READ %d at %u\n",root->data,root);
inorder(root->right);
}
}
void preorder(struct node *root)
{
printf("ROOT DEBUG %u \n",root);
if(root)
{
printf("READ %d \n",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
printf("ROOT DEBUG %u \n",root);
if(root)
{
postorder(root->left);
postorder(root->right);
printf("READ %d \n",root->data);
}
}
int main()
{
struct node *realroot;
struct node* r=NULL;
struct node *x=alloc(10);
struct node *y=alloc(5);
struct node *z=alloc(14);
struct node *a=alloc(17);
struct node *b=alloc(12);
struct node *c=alloc(2);
struct node *d=alloc(7);
struct node *e=alloc(1);
struct node *f=alloc(3);
struct node *g=alloc(6);
struct node *h=alloc(9);
struct node *i=alloc(11);
struct node *j=alloc(13);
struct node *k=alloc(15);
struct node *l=alloc(20);
if(root==NULL)
{
root=x;
realroot=x;
printf("ROOT ADD: %u \n",root);
}
else
insert(root,x);
insert(root,y);
insert(root,z);
insert(root,a);
insert(root,b);
insert(root,c);
insert(root,d);
insert(root,e);
insert(root,f);
insert(root,g);
insert(root,h);
insert(root,i);
insert(root,j);
insert(root,k);
insert(root,l);
r=search(realroot,14);
if(r)
printf("%d found! \n",r->data);
else
printf("NOT FOUND! \n");
printf("====================== \n");
inorder(realroot);
printf("====================== \n");
preorder(realroot);
printf("====================== \n");
postorder(realroot);
del(realroot,20);
inorder(realroot);
}