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
eca255c7
Verified
Commit
eca255c7
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added rgd et ex
parent
d095f187
No related branches found
No related tags found
No related merge requests found
Pipeline
#15629
passed
3 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/cours_15.md
+214
-5
214 additions, 5 deletions
slides/cours_15.md
with
214 additions
and
5 deletions
slides/cours_15.md
+
214
−
5
View file @
eca255c7
...
...
@@ -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))
```
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