Skip to content
Snippets Groups Projects
Commit 9cf596f4 authored by dario.genga's avatar dario.genga
Browse files

Add unfinished ex 4 and 5

parent bbf5f2d9
Branches
No related tags found
No related merge requests found
...@@ -8,6 +8,93 @@ ...@@ -8,6 +8,93 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#define QUADTREE_SIZE 16
typedef struct _node {
int info;
struct _node *child[4]; // or struct _node **child;
} node;
bool is_leaf(node *tree) {
return (NULL == tree->child[0]);
}
int max(int x, int y) {
return (x >= y ? x : y);
}
int max_depth(int depths[4]) {
int m = depths[0];
for (int i = 1; i < 4; ++i) {
m = max(m, depths[i]);
}
return m;
}
int depth(node *qt) {
int depths[] = {0, 0, 0, 0};
if (is_leaf(qt)) {
return 0;
} else {
for (int i = 0; i < 4; ++i) {
depths[i] = depth(qt->child[i]);
}
return 1 + max_depth(depths);
}
}
node *qt_create(int depth) {
node *n = calloc(1, sizeof(node));
if (depth > 0) {
for (int i = 0; i < 4; i++) {
n->child[i] = qt_create(depth - 1);
}
}
return n;
}
node *position(int row, int col, node* tree) {
int d = depth(tree);
while (d > 1) {
int index = 2 * ((row % (int)pow(2, d)) / (int)pow(2, (d - 1))) + (col % (int)pow(2, d)) / (int)pow(2, (d - 1));
tree = tree->child[index];
d -= 1;
}
return tree;
}
node *create_tree_from_matrix(int nb_row, int nb_col, int matrix[nb_row][nb_col], int depth) {
node *qt = qt_create(depth);
for (int row = 0; row < nb_row; row++) {
for (int col = 0; col < nb_col; col++) {
node *current = position(row, col, qt);
current->info = matrix[row][col];
}
}
return qt;
}
int main() { int main() {
int row = QUADTREE_SIZE;
int col = QUADTREE_SIZE;
int matrix[16][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}
};
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* Author : Dario GENGA /* Author : Dario GENGA
* Date : 03.05.2022 * Date : 03.05 .2022
* Description : Contrôle continue 3 - Exercice 5 * Description : Contrôle continue 3 - Exercice 5
*/ */
...@@ -7,7 +7,120 @@ ...@@ -7,7 +7,120 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <math.h>
#define QUADTREE_SIZE 16
typedef struct _node {
int info;
struct _node *child[4]; // or struct _node **child;
} node;
bool is_leaf(node *tree) {
return (NULL == tree->child[0]);
}
int max(int x, int y) {
return (x >= y ? x : y);
}
int max_depth(int depths[4]) {
int m = depths[0];
for (int i = 1; i < 4; ++i) {
m = max(m, depths[i]);
}
return m;
}
int depth(node *qt) {
int depths[] = {0, 0, 0, 0};
if (is_leaf(qt)) {
return 0;
} else {
for (int i = 0; i < 4; ++i) {
depths[i] = depth(qt->child[i]);
}
return 1 + max_depth(depths);
}
}
node *qt_create(int depth) {
node *n = calloc(1, sizeof(node));
if (depth > 0) {
for (int i = 0; i < 4; i++) {
n->child[i] = qt_create(depth - 1);
}
}
return n;
}
node *position(int row, int col, node* tree) {
int d = depth(tree);
while (d > 1) {
int index = 2 * ((row % (int)pow(2, d)) / (int)pow(2, (d - 1))) + (col % (int)pow(2, d)) / (int)pow(2, (d - 1));
tree = tree->child[index];
d -= 1;
}
return tree;
}
node *create_tree_from_matrix(int nb_row, int nb_col, int matrix[nb_row][nb_col], int depth) {
node *qt = qt_create(depth);
for (int row = 0; row < nb_row; row++) {
for (int col = 0; col < nb_col; col++) {
node *current = position(row, col, qt);
current->info = matrix[row][col];
}
}
return qt;
}
void transform(node *qt, int size, int indices[size]) {
node *current = qt;
for (int i = 0; i < size; i++) {
current = qt->child[indices[i]];
int childs[4] = {
current->child[0]->info,
current->child[1]->info,
current->child[2]->info,
current->child[3]->info,
};
int max = max_depth(childs);
}
}
int main() { int main() {
int row = QUADTREE_SIZE;
int col = QUADTREE_SIZE;
int matrix[16][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0, 11, 11, 11},
{0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0},
{0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11}
};
int parcours[2] = {3, 1};
node* qTree = create_tree_from_matrix(row, col, matrix, 4);
transform(qTree, 3, parcours);
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
printf("%d ", matrix[r][c]);
}
printf("\n");
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment