Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
binary_tree
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
Show more breadcrumbs
florent.didion
binary_tree
Commits
0fecde1b
Commit
0fecde1b
authored
4 years ago
by
florent.didion
Browse files
Options
Downloads
Patches
Plain Diff
ended i guess
parent
ce25c712
No related branches found
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
Makefile
+2
-0
2 additions, 0 deletions
Makefile
binary_tree
+0
-0
0 additions, 0 deletions
binary_tree
binary_tree_lib.c
+156
-0
156 additions, 0 deletions
binary_tree_lib.c
binary_tree_lib.h
+17
-3
17 additions, 3 deletions
binary_tree_lib.h
main.c
+18
-2
18 additions, 2 deletions
main.c
test.c
+0
-5
0 additions, 5 deletions
test.c
with
193 additions
and
10 deletions
Makefile
+
2
−
0
View file @
0fecde1b
...
@@ -9,7 +9,9 @@ binary_tree: $(OBJECTS)
...
@@ -9,7 +9,9 @@ binary_tree: $(OBJECTS)
binary_tree_lib.o
:
binary_tree_lib.h
binary_tree_lib.o
:
binary_tree_lib.h
test
:
test.o
test.o
:
test.c
.PHONY
:
clean
.PHONY
:
clean
...
...
This diff is collapsed.
Click to expand it.
binary_tree
0 → 100755
+
0
−
0
View file @
0fecde1b
File added
This diff is collapsed.
Click to expand it.
binary_tree_lib.c
+
156
−
0
View file @
0fecde1b
#include
"binary_tree_lib.h"
#include
"binary_tree_lib.h"
#include
<stdlib.h>
// Fonctions de création, destruction et affichage
// création d'un arbre vide (retourne NULL)
node_t
*
bst_create
(){
node_t
*
bst
=
NULL
;
return
bst
;
}
node_t
*
bst_create_node
(
int
val
){
node_t
*
new_node
=
malloc
(
sizeof
(
node_t
));
new_node
->
val
=
val
;
new_node
->
left
=
NULL
;
new_node
->
right
=
NULL
;
return
new_node
;
}
// détruit l'arbre et vide la mémoire
void
bst_destroy
(
node_t
*
tree
){
if
(
tree
->
left
==
NULL
&&
tree
->
right
==
NULL
){
free
(
tree
);
}
else
if
(
tree
->
left
!=
NULL
){
bst_destroy
(
tree
->
left
);
}
else
if
(
tree
->
right
!=
NULL
){
bst_destroy
(
tree
->
right
);
}
}
// affiche l'arbre (voir plus bas)
void
bst_print
(
node_t
*
tree
,
int
prof
){
if
(
tree
==
NULL
)
{
return
;
}
bst_print
(
tree
->
left
,
prof
+
1
);
for
(
int
i
=
0
;
i
<
prof
;
i
++
)
{
printf
(
"-------"
);
}
printf
(
" %d
\n\n
"
,
tree
->
val
);
bst_print
(
tree
->
right
,
prof
+
1
);
}
// insertion de val dans l'arbre et retourne l'arbre (ou NULL si problème)
node_t
*
bst_insert
(
node_t
*
tree
,
int
val
){
if
(
tree
==
NULL
){
tree
=
bst_create_node
(
val
);
}
else
if
(
val
>
tree
->
val
){
if
(
tree
->
right
==
NULL
){
tree
->
right
=
bst_create_node
(
val
);
}
else
{
bst_insert
(
tree
->
right
,
val
);
}
}
else
if
(
val
<=
tree
->
val
){
if
(
tree
->
left
==
NULL
){
tree
->
left
=
bst_create_node
(
val
);
}
else
{
bst_insert
(
tree
->
left
,
val
);
}
}
return
tree
;
}
// efface le premier élément contenant la valeur val dans l'arbre
// et retourne l'arbre (ne fait rien si val est absente)
node_t
*
bst_delete
(
node_t
*
tree
,
int
val
){
if
(
val
<
tree
->
val
){
bst_delete
(
tree
->
left
,
val
);
}
else
if
(
val
>
tree
->
val
){
bst_delete
(
tree
->
right
,
val
);
}
if
(
val
==
tree
->
val
){
bst_destroy
(
tree
);
}
return
tree
;
}
// la valeur val est-elle présente dans l'arbre?
bool
bst_is_present
(
node_t
*
tree
,
int
val
){
if
(
val
<
tree
->
val
){
bst_is_present
(
tree
->
left
,
val
);
}
else
if
(
val
>
tree
->
val
){
bst_is_present
(
tree
->
right
,
val
);
}
if
(
val
==
tree
->
val
){
return
true
;
}
return
false
;
}
// retourne le noeud où la valeur val se trouve (NULL si absent)
node_t
*
bst_search
(
node_t
*
tree
,
int
val
){
if
(
val
<
tree
->
val
){
bst_search
(
tree
->
left
,
val
);
}
if
(
val
<
tree
->
val
){
bst_search
(
tree
->
right
,
val
);
}
if
(
val
==
tree
->
val
){
return
tree
;
}
return
NULL
;
}
// l'arbre est-il un arbre binaire de recherche?
bool
bst_is_bst
(
node_t
*
tree
){
if
(
!
(
tree
->
val
>
tree
->
left
->
val
)
&&
tree
->
left
!=
NULL
){
return
false
;
}
else
if
(
!
(
tree
->
val
<
tree
->
right
->
val
)
&&
tree
->
right
!=
NULL
){
return
false
;
}
else
if
(
tree
->
left
!=
NULL
){
bst_is_bst
(
tree
->
left
);
}
else
if
(
tree
->
right
!=
NULL
){
bst_is_bst
(
tree
->
right
);
}
return
true
;
}
// retourne le noeud avec la valeur minimale de l'arbre (NULL s'il y a pas)
node_t
*
bst_find_min_node
(
node_t
*
tree
){
if
(
tree
->
left
!=
NULL
){
bst_find_min_node
(
tree
->
left
);
}
return
tree
;
}
// retourne la valeur la plus petite stockée dans l'arbre (ou MIN_INT)
int
bst_find_min
(
node_t
*
tree
){
return
bst_find_min_node
(
tree
)
->
val
;
}
// retourne le noeud avec la valeur maximale de l'arbre (NULL s'il y a pas)
node_t
*
bst_find_max_node
(
node_t
*
tree
){
if
(
tree
->
right
!=
NULL
){
bst_find_min_node
(
tree
->
right
);
}
return
tree
;
}
// retourne la valeur la plus grande stockée dans l'arbre (ou MAX_INT)
int
bst_find_max
(
node_t
*
tree
){
return
bst_find_max_node
(
tree
)
->
val
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
binary_tree_lib.h
+
17
−
3
View file @
0fecde1b
...
@@ -3,10 +3,24 @@
...
@@ -3,10 +3,24 @@
#include
<stdbool.h>
#include
<stdbool.h>
typedef
struct
_node_t
{
typedef
struct
_node_t
{
int
val
;
int
val
;
struct
_node_t
*
left
;
struct
_node_t
*
left
;
struct
_node_t
*
right
;
struct
_node_t
*
right
;
}
node_t
;
}
node_t
;
// Fonctions de création, destruction et affichage
node_t
*
bst_create
();
node_t
*
bst_create_node
(
int
val
);
void
bst_destroy
(
node_t
*
tree
);
void
bst_print
(
node_t
*
tree
,
int
prof
);
node_t
*
bst_insert
(
node_t
*
tree
,
int
val
);
node_t
*
bst_delete
(
node_t
*
tree
,
int
val
);
bool
bst_is_present
(
node_t
*
tree
,
int
val
);
node_t
*
bst_search
(
node_t
*
tree
,
int
val
);
bool
bst_is_bst
(
node_t
*
tree
);
node_t
*
bst_find_min_node
(
node_t
*
tree
);
int
bst_find_min
(
node_t
*
tree
);
node_t
*
bst_find_max_node
(
node_t
*
tree
);
int
bst_find_max
(
node_t
*
tree
);
This diff is collapsed.
Click to expand it.
main.c
+
18
−
2
View file @
0fecde1b
int
main
(){
#include
"binary_tree_lib.h"
#include
<stdlib.h>
#include
<time.h>
int
main
(){
srand
(
time
(
NULL
));
int
test
[
10
];
for
(
int
i
=
0
;
i
<
10
;
i
++
){
test
[
i
]
=
rand
()
/
(
RAND_MAX
/
100
);
printf
(
"%d
\n
"
,
test
[
i
]);
}
node_t
*
tree
=
bst_create
();
for
(
int
i
=
0
;
i
<
10
;
i
++
){
tree
=
bst_insert
(
tree
,
test
[
i
]);
}
bst_print
(
tree
,
0
);
printf
(
"printed
\n\n
"
);
bst_destroy
(
tree
);
...
...
This diff is collapsed.
Click to expand it.
test.c
+
0
−
5
View file @
0fecde1b
...
@@ -5,8 +5,3 @@
...
@@ -5,8 +5,3 @@
int
main
(){
return
0
;
}
\ 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