Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Prog_C
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
thibault.chatillo
Prog_C
Commits
5f9d50a0
Commit
5f9d50a0
authored
4 years ago
by
thib
Browse files
Options
Downloads
Patches
Plain Diff
done
parent
7b2a7645
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
tree/main.c
+29
-16
29 additions, 16 deletions
tree/main.c
tree/test.c
+1
-1
1 addition, 1 deletion
tree/test.c
tree/tree.c
+53
-13
53 additions, 13 deletions
tree/tree.c
tree/tree.h
+2
-1
2 additions, 1 deletion
tree/tree.h
with
85 additions
and
31 deletions
tree/main.c
+
29
−
16
View file @
5f9d50a0
...
@@ -3,18 +3,31 @@ int main(){
...
@@ -3,18 +3,31 @@ int main(){
node_t
*
tree
=
bst_create
();
node_t
*
tree
=
bst_create
();
tree
=
bst_insert
(
tree
,
10
);
tree
=
bst_insert
(
tree
,
10
);
// count_child(tree);
tree
=
bst_insert
(
tree
,
5
);
tree
=
bst_insert
(
tree
,
5
);
// count_child(tree);
tree
=
bst_insert
(
tree
,
15
);
tree
=
bst_insert
(
tree
,
15
);
tree
=
bst_insert
(
tree
,
2
);
tree
=
bst_insert
(
tree
,
2
);
tree
=
bst_insert
(
tree
,
6
);
tree
=
bst_insert
(
tree
,
6
);
bst_print
(
tree
,
0
);
if
(
bst_is_present
(
tree
,
3
)){
if
(
bst_is_bst
(
tree
)){
printf
(
"found
\n
"
);
printf
(
"i'm a bst !
\n
"
);
}
if
(
NULL
!=
bst_search
(
tree
,
2
)){
printf
(
"found it
\n
"
);
}
}
printf
(
"%d
\n
"
,
bst_find_min
(
tree
));
bst_print
(
tree
,
0
);
printf
(
"%d
\n
"
,
bst_find_max
(
tree
));
tree
=
bst_delete
(
tree
,
5
);
printf
(
"
\n\n
"
);
bst_print
(
tree
,
0
);
// bst_print(tree, 0);
// if(bst_is_present(tree, 3)){
// printf("found\n");
// }
// if(NULL!=bst_search(tree, 2)){
// printf("found it\n");
// }
// printf("%d\n",bst_find_min(tree));
// printf("%d\n",bst_find_max(tree));
// count_child(tree);
bst_destroy
(
tree
);
bst_destroy
(
tree
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tree/test.c
+
1
−
1
View file @
5f9d50a0
...
@@ -16,7 +16,7 @@ MunitResult test_insert() //f de test
...
@@ -16,7 +16,7 @@ MunitResult test_insert() //f de test
bst_insert
(
tree
,
15
);
bst_insert
(
tree
,
15
);
bst_insert
(
tree
,
2
);
bst_insert
(
tree
,
2
);
bst_insert
(
tree
,
6
);
bst_insert
(
tree
,
6
);
bst_print
(
tree
);
bst_print
(
tree
,
0
);
return
MUNIT_OK
;
return
MUNIT_OK
;
...
...
This diff is collapsed.
Click to expand it.
tree/tree.c
+
53
−
13
View file @
5f9d50a0
...
@@ -72,11 +72,11 @@ node_t *bst_insert(node_t *tree, int val) {
...
@@ -72,11 +72,11 @@ node_t *bst_insert(node_t *tree, int val) {
return
tree
;
return
tree
;
}
}
// la valeur val est-elle présente dans l'arbre?
// la valeur val est-elle présente dans l'arbre?
bool
bst_is_present
(
node_t
*
tree
,
int
val
)
{
bool
bst_is_present
(
node_t
*
tree
,
int
val
)
{
return
bst_search
(
tree
,
val
)
!=
NULL
;
return
bst_search
(
tree
,
val
)
!=
NULL
;
}
}
// retourne le noeud où la valeur val se trouve (NULL si absent)
// retourne le noeud où la valeur val se trouve (NULL si absent)
node_t
*
bst_search
(
node_t
*
tree
,
int
val
)
{
node_t
*
bst_search
(
node_t
*
tree
,
int
val
)
{
...
@@ -95,7 +95,20 @@ node_t *bst_search(node_t *tree, int val) {
...
@@ -95,7 +95,20 @@ node_t *bst_search(node_t *tree, int val) {
}
}
// l'arbre est-il un arbre binaire de recherche?
// l'arbre est-il un arbre binaire de recherche?
bool
bst_is_bst
(
node_t
*
tree
);
bool
bst_is_bst
(
node_t
*
tree
)
{
if
(
tree
==
NULL
){
return
true
;
}
if
(
tree
->
left
!=
NULL
&&
tree
->
left
->
val
>
tree
->
val
)
{
return
false
;
}
if
(
tree
->
right
!=
NULL
&&
tree
->
right
->
val
<
tree
->
val
)
{
return
false
;
}
return
bst_is_bst
(
tree
->
left
)
&&
bst_is_bst
(
tree
->
right
);
}
node_t
*
bst_find_min_node
(
node_t
*
tree
)
{
node_t
*
bst_find_min_node
(
node_t
*
tree
)
{
if
(
tree
->
left
!=
NULL
)
{
if
(
tree
->
left
!=
NULL
)
{
...
@@ -115,14 +128,41 @@ node_t *bst_find_max_node(node_t *tree) {
...
@@ -115,14 +128,41 @@ node_t *bst_find_max_node(node_t *tree) {
}
}
}
}
//
int bst_find_max(node_t *tree) { return bst_find_max_node(tree)->val; }
int
bst_find_max
(
node_t
*
tree
)
{
return
bst_find_max_node
(
tree
)
->
val
;
}
// // efface le premier élément contenant la valeur val dans l'arbre
int
count_child
(
node_t
*
node
)
{
// // et retourne l'arbre (ne fait rien si val est absente)
int
nb
=
(
node
->
left
!=
NULL
)
+
(
node
->
right
!=
NULL
);
// node_t *bst_delete(node_t *tree, int val){
// printf("%d child(s)\n", nb);
// node_t *tmp=bst_search(tree, val);
return
nb
;
// if(tmp->left==NULL&&tmp->right==NULL){
}
// tmp=NULL;
// free(tmp);
node_t
*
bst_delete
(
node_t
*
tree
,
int
val
)
{
// }else if()
if
(
!
bst_is_empty
(
tree
))
{
// }
\ No newline at end of file
if
(
val
>
tree
->
val
)
{
tree
->
right
=
bst_delete
(
tree
->
right
,
val
);
// go right
}
else
if
(
val
<
tree
->
val
)
{
tree
->
left
=
bst_delete
(
tree
->
left
,
val
);
// go left
// found node
}
else
if
(
count_child
(
tree
)
==
2
)
{
node_t
*
next
=
bst_find_min_node
(
tree
->
right
);
tree
->
val
=
next
->
val
;
tree
->
right
=
bst_delete
(
next
,
next
->
val
);
}
else
{
// 1 or 0 childs
node_t
*
tmp
=
tree
;
if
(
tmp
->
left
!=
NULL
)
{
// left
tree
=
tree
->
left
;
}
else
if
(
tmp
->
right
!=
NULL
)
{
// right
tree
=
tree
->
right
;
}
else
{
tree
=
NULL
;
}
free
(
tmp
);
}
}
return
tree
;
}
This diff is collapsed.
Click to expand it.
tree/tree.h
+
2
−
1
View file @
5f9d50a0
...
@@ -38,4 +38,5 @@ node_t *bst_find_max_node(node_t *tree);
...
@@ -38,4 +38,5 @@ node_t *bst_find_max_node(node_t *tree);
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int
bst_find_max
(
node_t
*
tree
);
int
bst_find_max
(
node_t
*
tree
);
int
count_child
(
node_t
*
node
);
#endif
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment