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

added rgd et ex

parent d095f187
No related branches found
No related tags found
No related merge requests found
Pipeline #15629 passed
......@@ -336,10 +336,10 @@ A
. . .
```C
<r, A1, A2>
r: racine
A1: sous-arbre gauche
A2: sous-arbre droite
<R, G, D>
R: racine
G: sous-arbre gauche
D: sous-arbre droite
```
## Comment cela s'écrirait en C?
......@@ -376,7 +376,7 @@ typedef struct _node {
```C
NULL -> arbre (vide)
<n, arbre, arbre> -> arbre
racine(arbre) -> noeud
visiter(arbre) -> noeud
gauche(arbre) -> arbre (sous-arbre de gauche)
droite(arbre) -> arbre (sous-arbre de droite)
```
......@@ -422,3 +422,212 @@ graph TD;
:::
# Parcours d'arbres binaires
* Appliquer une opération à tous les noeuds de l'arbre.
* Nécessité de **parcourir** l'arbre.
## Une idée de comment parcourir cet arbre?
* Trois parcours (R: Racine, G: sous-arbre gauche, D: sous-arbre droit):
::: columns
:::: column
```{.mermaid format=pdf width=200 loc=figs/}
graph TD;
A[*]-->B[-];
B-->C[c];
B-->D[*];
D-->E[a];
D-->F[b];
A-->G[+];
G-->H[d];
G-->I["/"];
I-->J[e];
I-->K[f];
```
::::
:::: column
1. Pacrours **préfixe** (R, G D),
2. Parcours **infixe** (G, R, D),
3. Parcours **postfixe** (G, D, R).
::::
:::
# Le parcours infixe (G, R, D)
* Gauche, Racine, Droite:
1. On descend dans l'arbre de gauche tant qu'il est pas vide,
2. On visite la racine du sous arbre,
3. On descend dans le sous-arbre de droite (s'il est pas vide),
4. On recommence.
. . .
## Incompréhensible?
* La récursivité c'est la vie.
```
parcours_infixe(arbre a) :
si est_pas_vide(gauche(a))
parcours_infixe(gauche(a))
visiter(A)
si est_pas_vide(droite(A))
parcours_infixe(droite(A))
```
# Graphiquement (dessinons)
::: columns
:::: column
```{.mermaid format=pdf width=200 loc=figs/}
graph TD;
A[*]-->B[-];
B-->C[c];
B-->D[*];
D-->E[a];
D-->F[b];
A-->G[+];
G-->H[d];
G-->I["/"];
I-->J[e];
I-->K[f];
```
::::
:::: column
```
parcours_infixe(arbre a) :
si est_pas_vide(gauche(a))
parcours_infixe(gauche(a))
visiter(A)
si est_pas_vide(droite(A))
parcours_infixe(droite(A))
```
::::
:::
# Graphiquement (`mermaid` c'est super)
::: columns
:::: column
```{.mermaid format=pdf width=200 loc=figs/}
graph TD;
A[*]-->B[-];
A[*]-.->|1|B[-];
B-->C[c];
B-.->|2|C[c];
C-.->|3|B;
B-->D[*];
B-.->|4|D;
D-->E[a];
D-.->|5|E;
E-.->|6|D;
D-->F[b];
D-.->|7|F;
F-.->|8|A;
A-->G[+];
A-.->|9|G;
G-->H[d];
G-.->|10|H;
H-.->|11|G;
G-->I["/"];
G-.->|12|I;
I-->J[e];
I-.->|13|J;
J-.->|14|I;
I-->K[f];
I-.->|15|K;
```
::::
:::: column
```
parcours_infixe(arbre a) :
si est_pas_vide(gauche(a))
parcours_infixe(gauche(a))
visiter(A)
si est_pas_vide(droite(A))
parcours_infixe(droite(A))
```
## Remarque
Le noeud est visité à la **remontée**.
## Résultat
```
a * b - c * d + e / f
```
::::
:::
# Et en C?
## Live code
. . .
```C
// Déclarations pour le type arbre
typedef int data;
typedef struct _node {
data info;
struct _node* left;
struct _node* right;
} node;
typedef node* tree_t;
void tree_print(tree_t tree, int n) {
if (NULL != tree) {
tree_print(tree->left, n+1);
for (int i = 0; i < n; i++) {
printf(" ");
}
printf("%d\n", tree->info);
tree_print(tree->gauche, n+1);
}
}
```
# Question
## Avez-vous compris le fonctionnement?
. . .
## Vous en êtes sûr·e·s?
. . .
## OK, alors deux exercices:
1. Écrire le pseudo-code pour le parcours R, G, D (matrix).
2. Écrire le pseudo-code pour la parcours G, D, R (matrix),
## Rappel
```
parcours_infixe(arbre a)
si est_pas_vide(gauche(a))
parcours_infixe(gauche(a))
visiter(A)
si est_pas_vide(droite(A))
parcours_infixe(droite(A))
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment