Skip to content
Snippets Groups Projects
Commit cfd52a48 authored by Pierre Kunzli's avatar Pierre Kunzli
Browse files

Merge branch 'master' into pk

parents 5aeca225 f622ba10
No related branches found
No related tags found
No related merge requests found
......@@ -220,7 +220,7 @@ bool binary_get2(int n, key_value_t table[n], key_t key, value_t *value) {
. . .
* $10^{13}$ clés, $10^8$ citoyens, $10^{-5}$ ($10^{-3}\%$ de la table est
* $10^{13}$ clés, $10^7$ citoyens, $10^{-5}$ ($10^{-3}\%$ de la table est
occupée) $\Rightarrow$ *inefficace*.
* Pire: $10^{13}$ entrées ne rentre pas dans la mémoire d'un
ordinateur.
......
......@@ -438,12 +438,12 @@ graph TD;
* Appliquer une opération à tous les noeuds de l'arbre,
* Nécessité de **parcourir** l'arbre,
* On utilise uniquement l'interface définie plus haut (visiter, gauche,
droite).
* Utiliser uniquement l'interface: visiter, gauche,
droite.
## Une idée de comment parcourir cet arbre?
* Trois parcours (R: Racine, G: sous-arbre gauche, D: sous-arbre droit):
* 3 parcours (R: Racine, G: sous-arbre gauche, D: sous-arbre droit):
::: columns
......@@ -597,6 +597,8 @@ c - a * b * d + e / f
## Live code
\footnotesize
. . .
```C
......@@ -647,6 +649,8 @@ parcours_infixe(arbre a)
# Correction
\footnotesize
* Les deux parcours sont des modifications **triviales**[^1] de l'algorithme
infixe.
......@@ -934,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.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment