From 84067056ce437321f67aa88317598ca5bbfa6962 Mon Sep 17 00:00:00 2001
From: Orestis <orestis.malaspinas@pm.me>
Date: Mon, 21 Feb 2022 17:42:21 +0100
Subject: [PATCH] updated with insertion

---
 slides/cours_15.md | 106 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 90 insertions(+), 16 deletions(-)

diff --git a/slides/cours_15.md b/slides/cours_15.md
index 8d8183b..b1743bb 100644
--- a/slides/cours_15.md
+++ b/slides/cours_15.md
@@ -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 != courant && !success) {
+    while (NULL != current && !success) {
         if (current->key > X) {
-            current = courant->gauche;
+            current = current->gauche;
         } else if (current->key < X){
-            current = courant->droite;
+            current = current->droite;
         } else {
             return current;
         }
@@ -869,10 +869,10 @@ int arbre_size(tree_t tree) {
 # Pseudocode d'insertion (1/2)
 
 * Deux parties:
-    * Recherche de la position.
-    * Ajout dans l'arbre.
+    * Recherche le parent où se passe l'insertion.
+    * Ajout du fils dans l'arbre.
 
-## Recherche de la position
+## Recherche du 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(suivant)
             arbre = suivant
     returne arbre
 ```
@@ -892,23 +892,97 @@ arbre position(arbre, clé)
     * Recherche de la position.
     * Ajout dans l'arbre.
 
-## Ajout dans l'arbre
+## Ajout du 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)
+
+\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);
+```
 
 
 [^1]: Copyright cours de mathématiques pendant trop d'années.
-- 
GitLab