-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbre.c
249 lines (198 loc) · 5.31 KB
/
arbre.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
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
/**********************************PARTIE IMPLEMENTATION ARBRE BINAIRE******************************/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "liste.c"
struct noeud* creer_arbre()
{
return NULL;
}
unsigned arbre_vide(struct noeud* a)
{
return a == NULL;
}
struct noeud* construire(int x ,struct noeud* sag,struct noeud* sad)
{
struct noeud* a = (struct noeud*)malloc(sizeof(struct noeud*));
a->info = x;
a->sag = sag;
a->sad = sad;
return a;
}
struct noeud* gauche(struct noeud* a)
{
assert(!arbre_vide(a));
return a->sag;
}
struct noeud* droite(struct noeud* a)
{
assert(!arbre_vide(a));
return a->sad;
}
int lire_racine(struct noeud *a)
{
assert(!arbre_vide(a));
return a->info;
}
unsigned arbre_confondues(struct noeud *ra,struct noeud *rb)
{
/*Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both
trees simultaneously, and while traversing we need to compare
data and children of the trees*/
/*idea is.. you will kind of mimic the display algorithm
and instead of displaying the nodes content you're gonna compare.
Obviously,you're gonna need two queues for the job
cases:
-same position nodes are/n't equal
*/
assert(!arbre_vide(ra));
assert(!arbre_vide(rb));
struct file fa,fb;
creer_file(&fa);
creer_file(&fb);
struct noeud* ta = ra,*tb = rb;
enfiler(ta,&fa);
enfiler(tb,&fb);
printf("\n");
while(!file_vide(fa) && !file_vide(fb))
{
ta = premier(fa);
tb = premier(fb);
if(ta->info != tb->info)
return 0;
defiler(&fa);
defiler(&fb);
if(ta->sag)
enfiler(ta->sag,&fa);
if(ta->sad)
enfiler(ta->sad,&fa);
if(tb->sag)
enfiler(tb->sag,&fb);
if(ta->sad)
enfiler(tb->sad,&fb);
}
return 1;
}
unsigned est_degeneree(struct noeud *abr)
{
//Degenerated/pathological
/*Un
* arbre binaire est dit dégénéré si
chacun de ses noeuds a au plus un fils.*/
struct noeud *q = abr;
while(q)
{
if(q->sag && q->sad)
return 0;
if(q->sag)
q = q->sag;
else
q = q->sad;
}
return 1;
}
unsigned profondeur(struct noeud* abr)
{
/*Approach is quite similar to "Level Order Traversal" (parcous par niveau)
* initialise height = 0
* "NULL" is gonna be used as a"marker"at "every level",so whenever we encounter
* "NULL" we will increment the height+1
* -First add root to the QUEUE and add NULL as well as its marker
* -Extract a node from QUEUE
* -Check if it is "NULL" ,it means either we reached to the end of level or
* entire tree is traversed
* -So before adding NULL as marker to the next level,check if queue is
* empty,which means we have traveled all the levels and if not empty
* then add NULL as marker and increase height +1
*
* */
/*
*WHEN SHOULD WE PUT THE NULL ELEMENT??
NOTE : THE BELOW FUNCTION HAS FEW ERRANEOUS PARTS ,ITS UNDER MAINTENANCE
* */
struct noeud *t;
struct file f; creer_file(&f);
enfiler(abr,&f); enfiler(NULL,&f); //its marker
t = premier(f);
unsigned height =-1;
printf("\n");
while(t || !file_vide(f))
{
t = premier(f);
defiler(&f);
if(t)
printf("%d\t",t->info);
if(!t)
{
height++;
}
if(t->sag || t->sad)
{
if(t->sag)
enfiler(t->sag,&f);
if(t->sad)
enfiler(t->sad,&f);
}
}
return height;
}
void parcours_niveau_trie(struct noeud * abr)
{
/*Avec une arbre binaire contenant des entiers.
on va creer un algorithme qui va rendre la liste des
elements entiers triés par ordre croissant sans utiliser
un algorithme de tri*/
struct file f; creer_file(&f);
creer_liste(); //OA liste
struct noeud* t ;
enfiler(abr,&f);
while(!file_vide(f))
{
t = premier(f);
ajout_trie(t);
defiler(&f);
if(t->sad)
enfiler(t->sad,&f);
if(t->sag)
enfiler(t->sag,&f);
}
}
/************************************IMPLEMTNATION FILE*************************************/
void creer_file(struct file* f)
{
f->tete = NULL;
f->queue = NULL;
}
unsigned file_vide(struct file f)
{
return(f.tete == NULL && f.queue == NULL);
}
struct noeud* premier(struct file f)
{
assert(!file_vide(f));
return f.tete->info;
}
void enfiler(struct noeud* x,struct file *f)
{
struct elem* q;
q = (struct elem*)malloc(sizeof(struct elem));
q->suivant = NULL;
q->info = x;
//there are two cases
//empty queue and filled one
if(!file_vide(*f))
f->queue->suivant = q;
else
f->tete = q;
//We will add from the queue in both cases
f->queue = q;
}
void defiler(struct file *f)
{
assert(!file_vide(*f));
struct elem *q = f->tete;
f->tete = f->tete->suivant;
free(q);
if(f->tete == NULL) //if ever the head is NULL,forcely the queue = NULL AUSSI
f->queue = NULL;
}