Skip to content
Snippets Groups Projects
Verified Commit 8967896b authored by orestis.malaspin's avatar orestis.malaspin
Browse files

plit at 15-16

parent 8e8cbebc
No related branches found
No related tags found
No related merge requests found
Pipeline #15768 passed
......@@ -938,112 +938,4 @@ tree_t position(tree_t tree, key_t key) {
}
```
# Code d'insertion en C (2/2)
## Ajout du fils (ensemble)
\scriptsize
* 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!
\footnotesize
```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.
---
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)
\scriptsize
* 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!
\footnotesize
```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!
::::
:::
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment