Skip to content
Snippets Groups Projects
Commit 8e7bbea2 authored by Boris Stefanovic's avatar Boris Stefanovic
Browse files

save

parent e782027f
Branches
No related tags found
No related merge requests found
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define QUAD 4
#define SIDE 16
typedef struct node {
int value;
struct node* children[QUAD];
} node_t;
node_t* node_create(const int value) {
node_t *node = malloc(sizeof(node_t));
node->value = value;
for (int i = 0; i < QUAD; ++i) node->children[i] = NULL;
return node;
}
bool is_leaf(node_t* node) {
return node->children[0] = NULL;
}
void mat_to_qtr(
const int** a,
const int x1, const int x2,
const int y1, const int y2,
node_t* node
) {
if (x2-x1 != y2-y1) printf("PROBLEM!\n");
const int size = x2 - x1;
switch (size) {
case 1:
node->value = a[y1][x1];
return;
default:
for (int i = 0; i < QUAD; ++i)
node->children[i] = node_create(0);
mat_to_qtr(a,
x1, x1 + (x2 - x1) / 2,
y1, y1 + (y2 - y1) / 2,
node->children[0]);
mat_to_qtr(a,
x1 + (x2 - x1) / 2, x2,
y1, y1 + (y2 - y1) / 2,
node->children[1]);
mat_to_qtr(a,
x1, x1 + (x2 - x1) / 2,
y1 + (y2 - y1) / 2, y2,
node->children[2]);
mat_to_qtr(a,
x1 + (x2 - x1) / 2, x2,
y1, y1 + (y2 - y1) / 2,
node->children[3]);
}
}
node_t* qtr_copy(node_t* node) {
if (node == NULL) return NULL;
node_t *dest = malloc(sizeof(node_t));
dest->value = node->value;
for (int i = 0; i < QUAD; ++i) {
dest->children[i] = qtr_copy(node->children[i]);
}
return dest;
}
const int initlist[] = {
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 main() {
int a[16][16];
for (int y = 0; y < 16; ++y)
for (int x = 0; x < 16; ++x)
a[y][x] = initlist[16 * y + x];
node_t *root = malloc(sizeof(node_t));
mat_to_qtr(a, 0, 16, 0, 16, root);
node_t *copy = qtr_copy(root);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment