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

more quad trees

parent e2face64
No related branches found
No related tags found
No related merge requests found
Pipeline #16388 passed
...@@ -32,7 +32,7 @@ patat: ...@@ -32,7 +32,7 @@ patat:
# Encore un petit exercice # Encore un petit exercice
* Insérer les noeuds suivants dans un arbre AVL * Insérer les nœuds suivants dans un arbre AVL
``` ```
25 | 60 | 35 | 10 | 5 | 20 | 65 | 45 | 70 | 40 | 50 | 55 | 30 | 15 25 | 60 | 35 | 10 | 5 | 20 | 65 | 45 | 70 | 40 | 50 | 55 | 30 | 15
...@@ -323,15 +323,15 @@ graph TD; ...@@ -323,15 +323,15 @@ graph TD;
# Résumé de la suppression # Résumé de la suppression
1. On supprime comme pour un arbre binaire de recherche. 1. On supprime comme pour un arbre binaire de recherche.
2. Si un noeud est déséquilibré, on le rééquilibre. 2. Si un nœud est déséquilibré, on le rééquilibre.
* Cette opération pour déséquilibrer un autre noeud. * Cette opération pour déséquilibrer un autre nœud.
3. On continue à rééquilibrer tant qu'il y a des noeuds à équilibrer. 3. On continue à rééquilibrer tant qu'il y a des nœuds à équilibrer.
# Les arbres quaternaires # Les arbres quaternaires
## Définition ## Définition
Arbre dont chaque noeud a 4 enfants ou aucun. Arbre dont chaque nœud a 4 enfants ou aucun.
![Un exemple de quadtree.](figs/quad_ex.svg) ![Un exemple de quadtree.](figs/quad_ex.svg)
...@@ -341,7 +341,7 @@ Arbre dont chaque noeud a 4 enfants ou aucun. ...@@ -341,7 +341,7 @@ Arbre dont chaque noeud a 4 enfants ou aucun.
Typiquement utilisés pour représenter des données bidimensionnelles. Typiquement utilisés pour représenter des données bidimensionnelles.
Son équivalent tri-dimensionnel est l'octree (chaque noeud a 8 enfants ou aucun). Son équivalent tri-dimensionnel est l'octree (chaque nœud a 8 enfants ou aucun).
## Cas d'utilisation: images ## Cas d'utilisation: images
...@@ -402,7 +402,7 @@ struct node ...@@ -402,7 +402,7 @@ struct node
inf_gauche, inf_droit inf_gauche, inf_droit
``` ```
![Un noeud d'arbre quaternaire.](figs/quad_struct.svg) ![Un nœud d'arbre quaternaire.](figs/quad_struct.svg)
:::: ::::
...@@ -451,6 +451,7 @@ bool est_feuille(noeud) ...@@ -451,6 +451,7 @@ bool est_feuille(noeud)
. . . . . .
* Inutile d'avoir 4 conditions (soit 4 enfants soit aucun!)
* Facile d'en oublier un! * Facile d'en oublier un!
* Comment changer la structure pour que ça soit moins terrible? * Comment changer la structure pour que ça soit moins terrible?
...@@ -459,10 +460,10 @@ bool est_feuille(noeud) ...@@ -459,10 +460,10 @@ bool est_feuille(noeud)
```python ```python
struct node struct node
info info
node sup_gauche[4] node enfant[4]
``` ```
# Une fonctionnalité simple # Structure de données
## En C? ## En C?
...@@ -471,10 +472,206 @@ struct node ...@@ -471,10 +472,206 @@ struct node
```C ```C
typedef struct _node { typedef struct _node {
int info; int info;
struct _node *children; struct _node *child[4];
} node; } node;
typedef node *tree;
``` ```
## Fonction `is_leaf(node *tree)`?
. . .
```C
bool is_leaf(node *tree) {
return (NULL == tree->child[0]); // only first matters
}
```
# Problème à résoudre
* Construire un arbre quaternaire à partir d'une image:
* Créer l'arbre (allouer la mémoire pour tous les nœuds),
* Le remplir avec les valeurs des pixels.
* Compression de l'image:
* Si les pixels sont les mêmes dans le quadrant on supprime le sous-arbre (sans perte)
* Si les pixels dévident pas trop on supprime le quadrant (avec perte)
# Fonctions utiles (1/N)
## Comment créer un arbre de profondeur `prof` (3min)?
. . .
```python
arbre creer_arbre(prof)
n = nouveau_noeud() # alloue la mémoire
si prof > 0
pour i = 0 à 3
n.enfant[i] = creer_arbre(prof-1)
retourne n
```
## En `C` (3 min, matrix)?
. . .
```C
node *qt_create(int depth) {
node *n = calloc(1, sizeof(*n));
if (depth > 0) {
for (int i = 0; i < 4; ++i) {
n->child[i] = qt_create(depth-1);
}
}
return n;
}
```
# Fonctions utiles (2/N)
## Comment remplir un arbre depuis une matrice?
```
SG=0 | SD=1
21 | 12 | 4 | 4
9 | 7 | 4 | 4
-----------------
1 | 1 | 0 | 31
1 | 1 | 3 | 27
IG=2 | ID=3
```
## Quel arbre cela représente?
. . .
![L'arbre correspondant](figs/quad_img_simple.svg)
# Fonctions utiles (3/N)
* On veut transformer une ligne/colonne en feuille.
* Comment?
::: columns
:::: {.column width=40%}
## Soit `ligne=2`, `colonne=3`
```
SG=0 | SD=1
21 | 12 | 4 | 4
9 | 7 | 4 | 4
-----------------
1 | 1 | 0 | 31
1 | 1 | 3 | 27
IG=2 | ID=3
```
::::
:::: {.column width=70%}
## Trouver un algorithme
![Déterminer un algorithme.](figs/quad_img_simple.svg)
* Quelle feuille?
* Plus important: quel chemin?
. . .
* `co -> G/D`, `li -> S/I`,
* `2 * (li / 2) + co / 2 -> 2 * 1 + 1 = 3`
* `2 * ((li % 2) / 1) + (co % 2) / 1 -> 2 * 0 + 1 = 1`
* Comment généraliser?
::::
:::
# Fonctions utiles (4/N)
::: columns
:::: {.column width=40%}
## Soit `ligne=2`, `colonne=3`
```
SG=0 | SD=1
21 | 12 | 4 | 4
9 | 7 | 4 | 4
-----------------
1 | 1 | 0 | 31
1 | 1 | 3 | 27
IG=2 | ID=3
```
::::
:::: {.column width=70%}
## Trouver un algorithme
![Déterminer un algorithme.](figs/quad_img_simple.svg)
* Comment généraliser?
. . .
```C
noeud position(li, co, arbre)
d = profondeur(arbre);
tant_que (d > 1)
index = 2 * ((li % 2^d) / 2^(d-1)) +
(col % 2^d) / 2^(d-1)
arbre = arbre.enfant[index]
d -= 1
retourn arbre
```
::::
:::
# Remplir l'arbre
## A partir d'une matrice (pseudo-code, 5min, matrix)?
. . .
```C
arbre matrice_à_arbre(matrice, arbre)
arbre = creer_arbre(profondeur)
pour li de 0 à nb_lignes(matrice)
pour co de 0 à nb_colonnes(matrice)
noeud = position(li, co, arbre)
noeud.info = matrice[co][li]
retourne arbre
```
. . .
## A partir d'une matrice (C, 5min, matrix)?
```C
node *matrice_to_qt(int nb_li, int nb_co, int matrix[nb_li][nb_co], int depth)
node *qt = qt_create(depth);
for (int li = 0; li < nd_li; ++li) {
for (int co = 0; co < nd_co; ++co) {
node *current = position(li, co, qt);
current->info = matrix[li][co];
}
}
return qt;
```
# Interface
## Quelles sont les fonctions à implémenter?
# Implémentation # Implémentation
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.40.1 (20161225.0304)
-->
<!-- Title: G Pages: 1 -->
<svg width="1142pt" height="188pt" viewBox="0.00 0.00 1142.00 188.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 184)">
<title>G</title>
<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-184 1138,-184 1138,4 -4,4"/>
<!-- 0 -->
<g id="node1" class="node">
<title>0</title>
<ellipse fill="none" stroke="#000000" cx="567" cy="-162" rx="27" ry="18"/>
<text text-anchor="middle" x="567" y="-157.8" font-family="Times,serif" font-size="14.00" fill="#000000"> </text>
</g>
<!-- sg1 -->
<g id="node2" class="node">
<title>sg1</title>
<ellipse fill="none" stroke="#000000" cx="171" cy="-90" rx="27" ry="18"/>
<text text-anchor="middle" x="171" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">SG</text>
</g>
<!-- 0&#45;&gt;sg1 -->
<g id="edge1" class="edge">
<title>0-&gt;sg1</title>
<path fill="none" stroke="#000000" d="M540.7107,-157.2201C471.8543,-144.7008 286.1298,-110.9327 207.2397,-96.589"/>
<polygon fill="#000000" stroke="#000000" points="207.7449,-93.1236 197.28,-94.7782 206.4926,-100.0107 207.7449,-93.1236"/>
</g>
<!-- sd1 -->
<g id="node3" class="node">
<title>sd1</title>
<ellipse fill="none" stroke="#000000" cx="459" cy="-90" rx="27" ry="18"/>
<text text-anchor="middle" x="459" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">SD</text>
</g>
<!-- 0&#45;&gt;sd1 -->
<g id="edge2" class="edge">
<title>0-&gt;sd1</title>
<path fill="none" stroke="#000000" d="M547.6918,-149.1278C530.6445,-137.763 505.5981,-121.0654 486.4656,-108.3104"/>
<polygon fill="#000000" stroke="#000000" points="488.4031,-105.3956 478.1411,-102.7607 484.5201,-111.2199 488.4031,-105.3956"/>
</g>
<!-- ig1 -->
<g id="node4" class="node">
<title>ig1</title>
<ellipse fill="none" stroke="#000000" cx="675" cy="-90" rx="27" ry="18"/>
<text text-anchor="middle" x="675" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">IG</text>
</g>
<!-- 0&#45;&gt;ig1 -->
<g id="edge3" class="edge">
<title>0-&gt;ig1</title>
<path fill="none" stroke="#000000" d="M586.3082,-149.1278C603.3555,-137.763 628.4019,-121.0654 647.5344,-108.3104"/>
<polygon fill="#000000" stroke="#000000" points="649.4799,-111.2199 655.8589,-102.7607 645.5969,-105.3956 649.4799,-111.2199"/>
</g>
<!-- id1 -->
<g id="node5" class="node">
<title>id1</title>
<ellipse fill="none" stroke="#000000" cx="963" cy="-90" rx="27" ry="18"/>
<text text-anchor="middle" x="963" y="-85.8" font-family="Times,serif" font-size="14.00" fill="#000000">ID</text>
</g>
<!-- 0&#45;&gt;id1 -->
<g id="edge4" class="edge">
<title>0-&gt;id1</title>
<path fill="none" stroke="#000000" d="M593.2893,-157.2201C662.1457,-144.7008 847.8702,-110.9327 926.7603,-96.589"/>
<polygon fill="#000000" stroke="#000000" points="927.5074,-100.0107 936.72,-94.7782 926.2551,-93.1236 927.5074,-100.0107"/>
</g>
<!-- sg2 -->
<g id="node6" class="node">
<title>sg2</title>
<ellipse fill="none" stroke="#000000" cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">21</text>
</g>
<!-- sg1&#45;&gt;sg2 -->
<g id="edge5" class="edge">
<title>sg1-&gt;sg2</title>
<path fill="none" stroke="#000000" d="M149.1295,-79.0647C124.7778,-66.8889 85.238,-47.119 57.7715,-33.3858"/>
<polygon fill="#000000" stroke="#000000" points="59.114,-30.1439 48.6045,-28.8022 55.9835,-36.4049 59.114,-30.1439"/>
</g>
<!-- sd2 -->
<g id="node7" class="node">
<title>sd2</title>
<ellipse fill="none" stroke="#000000" cx="99" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="99" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">12</text>
</g>
<!-- sg1&#45;&gt;sd2 -->
<g id="edge6" class="edge">
<title>sg1-&gt;sd2</title>
<path fill="none" stroke="#000000" d="M155.7307,-74.7307C145.803,-64.803 132.6847,-51.6847 121.5637,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="123.7933,-37.8436 114.2473,-33.2473 118.8436,-42.7933 123.7933,-37.8436"/>
</g>
<!-- ig2 -->
<g id="node8" class="node">
<title>ig2</title>
<ellipse fill="none" stroke="#000000" cx="171" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="171" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">9</text>
</g>
<!-- sg1&#45;&gt;ig2 -->
<g id="edge7" class="edge">
<title>sg1-&gt;ig2</title>
<path fill="none" stroke="#000000" d="M171,-71.8314C171,-64.131 171,-54.9743 171,-46.4166"/>
<polygon fill="#000000" stroke="#000000" points="174.5001,-46.4132 171,-36.4133 167.5001,-46.4133 174.5001,-46.4132"/>
</g>
<!-- id2 -->
<g id="node9" class="node">
<title>id2</title>
<ellipse fill="none" stroke="#000000" cx="243" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="243" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">7</text>
</g>
<!-- sg1&#45;&gt;id2 -->
<g id="edge8" class="edge">
<title>sg1-&gt;id2</title>
<path fill="none" stroke="#000000" d="M186.2693,-74.7307C196.197,-64.803 209.3153,-51.6847 220.4363,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="223.1564,-42.7933 227.7527,-33.2473 218.2067,-37.8436 223.1564,-42.7933"/>
</g>
<!-- sg3 -->
<g id="node10" class="node">
<title>sg3</title>
<ellipse fill="none" stroke="#000000" cx="315" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="315" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">4</text>
</g>
<!-- sd1&#45;&gt;sg3 -->
<g id="edge9" class="edge">
<title>sd1-&gt;sg3</title>
<path fill="none" stroke="#000000" d="M437.1295,-79.0647C412.7778,-66.8889 373.238,-47.119 345.7715,-33.3858"/>
<polygon fill="#000000" stroke="#000000" points="347.114,-30.1439 336.6045,-28.8022 343.9835,-36.4049 347.114,-30.1439"/>
</g>
<!-- sd3 -->
<g id="node11" class="node">
<title>sd3</title>
<ellipse fill="none" stroke="#000000" cx="387" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="387" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">4</text>
</g>
<!-- sd1&#45;&gt;sd3 -->
<g id="edge10" class="edge">
<title>sd1-&gt;sd3</title>
<path fill="none" stroke="#000000" d="M443.7307,-74.7307C433.803,-64.803 420.6847,-51.6847 409.5637,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="411.7933,-37.8436 402.2473,-33.2473 406.8436,-42.7933 411.7933,-37.8436"/>
</g>
<!-- ig3 -->
<g id="node12" class="node">
<title>ig3</title>
<ellipse fill="none" stroke="#000000" cx="459" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="459" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">4</text>
</g>
<!-- sd1&#45;&gt;ig3 -->
<g id="edge11" class="edge">
<title>sd1-&gt;ig3</title>
<path fill="none" stroke="#000000" d="M459,-71.8314C459,-64.131 459,-54.9743 459,-46.4166"/>
<polygon fill="#000000" stroke="#000000" points="462.5001,-46.4132 459,-36.4133 455.5001,-46.4133 462.5001,-46.4132"/>
</g>
<!-- id3 -->
<g id="node13" class="node">
<title>id3</title>
<ellipse fill="none" stroke="#000000" cx="531" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="531" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">4</text>
</g>
<!-- sd1&#45;&gt;id3 -->
<g id="edge12" class="edge">
<title>sd1-&gt;id3</title>
<path fill="none" stroke="#000000" d="M474.2693,-74.7307C484.197,-64.803 497.3153,-51.6847 508.4363,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="511.1564,-42.7933 515.7527,-33.2473 506.2067,-37.8436 511.1564,-42.7933"/>
</g>
<!-- sg4 -->
<g id="node14" class="node">
<title>sg4</title>
<ellipse fill="none" stroke="#000000" cx="603" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="603" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">1</text>
</g>
<!-- ig1&#45;&gt;sg4 -->
<g id="edge13" class="edge">
<title>ig1-&gt;sg4</title>
<path fill="none" stroke="#000000" d="M659.7307,-74.7307C649.803,-64.803 636.6847,-51.6847 625.5637,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="627.7933,-37.8436 618.2473,-33.2473 622.8436,-42.7933 627.7933,-37.8436"/>
</g>
<!-- sd4 -->
<g id="node15" class="node">
<title>sd4</title>
<ellipse fill="none" stroke="#000000" cx="675" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="675" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">1</text>
</g>
<!-- ig1&#45;&gt;sd4 -->
<g id="edge14" class="edge">
<title>ig1-&gt;sd4</title>
<path fill="none" stroke="#000000" d="M675,-71.8314C675,-64.131 675,-54.9743 675,-46.4166"/>
<polygon fill="#000000" stroke="#000000" points="678.5001,-46.4132 675,-36.4133 671.5001,-46.4133 678.5001,-46.4132"/>
</g>
<!-- ig4 -->
<g id="node16" class="node">
<title>ig4</title>
<ellipse fill="none" stroke="#000000" cx="747" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="747" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">1</text>
</g>
<!-- ig1&#45;&gt;ig4 -->
<g id="edge15" class="edge">
<title>ig1-&gt;ig4</title>
<path fill="none" stroke="#000000" d="M690.2693,-74.7307C700.197,-64.803 713.3153,-51.6847 724.4363,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="727.1564,-42.7933 731.7527,-33.2473 722.2067,-37.8436 727.1564,-42.7933"/>
</g>
<!-- id4 -->
<g id="node17" class="node">
<title>id4</title>
<ellipse fill="none" stroke="#000000" cx="819" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="819" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">1</text>
</g>
<!-- ig1&#45;&gt;id4 -->
<g id="edge16" class="edge">
<title>ig1-&gt;id4</title>
<path fill="none" stroke="#000000" d="M696.8705,-79.0647C721.2222,-66.8889 760.762,-47.119 788.2285,-33.3858"/>
<polygon fill="#000000" stroke="#000000" points="790.0165,-36.4049 797.3955,-28.8022 786.886,-30.1439 790.0165,-36.4049"/>
</g>
<!-- sg5 -->
<g id="node18" class="node">
<title>sg5</title>
<ellipse fill="none" stroke="#000000" cx="891" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="891" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">0</text>
</g>
<!-- id1&#45;&gt;sg5 -->
<g id="edge17" class="edge">
<title>id1-&gt;sg5</title>
<path fill="none" stroke="#000000" d="M947.7307,-74.7307C937.803,-64.803 924.6847,-51.6847 913.5637,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="915.7933,-37.8436 906.2473,-33.2473 910.8436,-42.7933 915.7933,-37.8436"/>
</g>
<!-- sd5 -->
<g id="node19" class="node">
<title>sd5</title>
<ellipse fill="none" stroke="#000000" cx="963" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="963" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">31</text>
</g>
<!-- id1&#45;&gt;sd5 -->
<g id="edge18" class="edge">
<title>id1-&gt;sd5</title>
<path fill="none" stroke="#000000" d="M963,-71.8314C963,-64.131 963,-54.9743 963,-46.4166"/>
<polygon fill="#000000" stroke="#000000" points="966.5001,-46.4132 963,-36.4133 959.5001,-46.4133 966.5001,-46.4132"/>
</g>
<!-- ig5 -->
<g id="node20" class="node">
<title>ig5</title>
<ellipse fill="none" stroke="#000000" cx="1035" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="1035" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">3</text>
</g>
<!-- id1&#45;&gt;ig5 -->
<g id="edge19" class="edge">
<title>id1-&gt;ig5</title>
<path fill="none" stroke="#000000" d="M978.2693,-74.7307C988.197,-64.803 1001.3153,-51.6847 1012.4363,-40.5637"/>
<polygon fill="#000000" stroke="#000000" points="1015.1564,-42.7933 1019.7527,-33.2473 1010.2067,-37.8436 1015.1564,-42.7933"/>
</g>
<!-- id5 -->
<g id="node21" class="node">
<title>id5</title>
<ellipse fill="none" stroke="#000000" cx="1107" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="1107" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">27</text>
</g>
<!-- id1&#45;&gt;id5 -->
<g id="edge20" class="edge">
<title>id1-&gt;id5</title>
<path fill="none" stroke="#000000" d="M984.8705,-79.0647C1009.2222,-66.8889 1048.762,-47.119 1076.2285,-33.3858"/>
<polygon fill="#000000" stroke="#000000" points="1078.0165,-36.4049 1085.3955,-28.8022 1074.886,-30.1439 1078.0165,-36.4049"/>
</g>
</g>
</svg>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment