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
8967896b
Verified
Commit
8967896b
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
plit at 15-16
parent
8e8cbebc
No related branches found
No related tags found
No related merge requests found
Pipeline
#15768
passed
3 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
slides/cours_15.md
+0
-108
0 additions, 108 deletions
slides/cours_15.md
slides/cours_16.md
+220
-0
220 additions, 0 deletions
slides/cours_16.md
with
220 additions
and
108 deletions
slides/cours_15.md
+
0
−
108
View file @
8967896b
...
...
@@ -938,112 +938,4 @@ tree_t position(tree_t tree, key_t key) {
}
```
# 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);
```
# La suppression de clé
*
Cas simples: le noeud à supprimer et est feuill ou a un seul fils.
*
Comment faites-vous?
. . .
::: columns
:::: column
Une feuille (le 19 p.ex.).
```
mermaid
flowchart TB;
10-->20;
10-->5
20-->21
20-->19
```
::::
:::: column
Un seul fils (le 20 p.ex.).
```
mermaid
flowchart TB;
10-->20;
10-->5
20-->25
25-->24
25-->30
5-->4;
5-->8;
```
::::
:::
## Cas compliqué
*
Le noeud à supprimer à deux descendants.
[
^1
]:
Copyright
cours de mathématiques pendant trop d'années.
This diff is collapsed.
Click to expand it.
slides/cours_16.md
0 → 100644
+
220
−
0
View file @
8967896b
---
title
:
"
Arbres"
date
:
"
2022-03-02"
patat
:
eval
:
tai
:
command
:
fish
fragment
:
false
replace
:
true
ccc
:
command
:
fish
fragment
:
false
replace
:
true
images
:
backend
:
auto
---
# Pseudocode d'insertion (1/2)
*
Deux parties:
*
Recherche le parent où se passe l'insertion.
*
Ajout du fils dans l'arbre.
## Recherche du parent
```
arbre position(arbre, clé)
si est_non_vide(arbre)
si clé < clé(arbre)
suivant = gauche(arbre)
sinon
suivant = droite(arbre)
tant que clé(arbre) != clé && est_non_vide(suivant)
arbre = suivant
si clé < clé(arbre)
suivant = gauche(arbre)
sinon
suivant = droite(arbre)
retourne arbre
```
# Pseudocode d'insertion (2/2)
*
Deux parties:
*
Recherche de la position.
*
Ajout dans l'arbre.
## Ajout du fils
```
ajout(arbre, clé)
si est_vide(arbre)
arbre = noeud(clé)
sinon
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 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;
subtree = key > current->key ? current->right :
current->left;
}
}
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);
```
# La suppression de clé
*
Cas simples: le noeud à supprimer et est feuill ou a un seul fils.
*
Comment faites-vous?
. . .
::: columns
:::: column
Une feuille (le 19 p.ex.).
```
{.mermaid format=pdf width=400 loc=figs/}
flowchart TB;
10-->20;
10-->5
20-->21
20-->19
```
::::
:::: column
Un seul fils (le 20 p.ex.).
```
{.mermaid format=pdf width=400 loc=figs/}
flowchart TB;
10-->20;
10-->5
20-->25
20-->18
25-->24
25-->30
5-->4;
5-->8;
style 18 fill:#fff,stroke:#fff,color:#fff
```
::::
:::
# La suppression de clé
## Cas compliqué
::: columns
:::: column
*
Le noeud à supprimer à (au moins) deux descendants (10).
```
{.mermaid format=pdf width=400 loc=figs/}
flowchart TB;
10-->20;
10-->5
20-->25
20-->18
25-->24
25-->30
5-->4;
5-->8;
```
::::
:::: column
*
Si on enlève 10 il se passe quoi?
*
On peut pas juste enlever
`10`
et recoller...
*
Proposez une solution bon sang!
::::
:::
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