Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
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
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
algorithmique
cours
Commits
84067056
Verified
Commit
84067056
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
updated with insertion
parent
15a7c1e8
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/cours_15.md
+90
-16
90 additions, 16 deletions
slides/cours_15.md
with
90 additions
and
16 deletions
slides/cours_15.md
+
90
−
16
View file @
84067056
...
...
@@ -361,7 +361,7 @@ A
```
C
typedef struct _node {
contenu info;
struct _node *left
_subtree, *right_subtree
;
struct _node *left
, *right
;
} node;
typedef node *tree;
```
...
...
@@ -373,7 +373,7 @@ typedef node *tree;
```
C
typedef struct _node {
int info;
struct _node left
_subtree, right_subtree
;
struct _node left
, right
;
} node;
```
...
...
@@ -790,11 +790,11 @@ typedef struct _node {
typedef node* tree_t;
tree_t search(key_t key, tree_t tree) {
tree_t current = tree;
while (NULL != c
o
ur
a
nt && !success) {
while (NULL != cur
re
nt && !success) {
if (current->key > X) {
current = c
o
ur
a
nt->gauche;
current = cur
re
nt->gauche;
} else if (current->key < X){
current = c
o
ur
a
nt->droite;
current = cur
re
nt->droite;
} else {
return current;
}
...
...
@@ -869,10 +869,10 @@ int arbre_size(tree_t tree) {
# Pseudocode d'insertion (1/2)
*
Deux parties:
*
Recherche
d
e
la posi
tion.
*
Ajout dans l'arbre.
*
Recherche
l
e
parent où se passe l'inser
tion.
*
Ajout
du fils
dans l'arbre.
## Recherche d
e la position
## Recherche d
u parent
```
arbre position(arbre, clé)
...
...
@@ -881,7 +881,7 @@ arbre position(arbre, clé)
suivant = gauche(arbre)
sinon
suivant = droite(arbre)
tant que clé(arbre) != clé && est_non_vide(sivant)
tant que clé(arbre) != clé && est_non_vide(s
u
ivant)
arbre = suivant
returne arbre
```
...
...
@@ -892,23 +892,97 @@ arbre position(arbre, clé)
*
Recherche de la position.
*
Ajout dans l'arbre.
## Ajout d
ans l'arbre
## Ajout d
u fils
```
ajout(arbre, clé)
si clé < clé(arbre)
gauche(arbre) = noeud(clé)
sinon si clé > clé(arbre)
droite(arbre) = noeud(clé)
si est_vide(arbre)
arbre = noeud(clé)
sinon
retourne
si clé < clé(arbre)
gauche(arbre) = noeud(clé)
sinon si clé > clé(arbre)
droite(arbre) = noeud(clé)
sinon
retourne
```
# Code d'insertion en C (1/2)
## Recherche de la position
## Recherche du parent (ensemble)
. . .
```
C
tree_t position(tree_t tree, key_t key) {
tree_t current = tree;
if (NULL != current) {
tree_t subtree = key > current->key ? current->right :
current->left;
while (key != current->key && NULL != subtree) {
current = subtree;
}
}
return current;
}
```
# Code d'insertion en C (2/2)
## Ajout du fils (ensemble)
\s
criptsize
*
2 cas: arbre vide ou pas.
*
on retourne un pointeur vers le noeud ajouté (ou
`NULL`
)
. . .
```
C
tree_t add_key(tree_t *tree, key_t key) {
node_t *new_node = calloc(1, sizeof(*new_node)); // nouveauté!
new_node->key = key;
if (NULL == *tree) {
*tree = new_node;
} else {
tree_t subtree = position(*tree, key);
if (key == subtree->key) {
return NULL;
} else {
if (key > subtree->key) {
subtree->right = new_node;
} else {
subtree->left = new_node;
}
}
}
return new_node;
}
```
# Une nouvelle corde à votre arc!
\f
ootnotesize
```
C
void *calloc(size_t nmemb, size_t size); // man 3 calloc
```
```
$ man 3 calloc
The calloc() function allocates memory for an array of nmemb elements
of size bytes each and returns a pointer to the allocated memory.
The memory is set to zero. If nmemb or size is 0, then calloc() re‐
turns either NULL, or a unique pointer value that can later be suc‐
cessfully passed to free(). If the multiplication of nmemb and size
would result in integer overflow, then calloc() returns an error. By
contrast, an integer overflow would not be detected in the following
call to malloc(), with the result that an incorrectly sized block of
memory would be allocated:
malloc(nmemb * size);
```
[
^1
]:
Copyright
cours de mathématiques pendant trop d'années.
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