Skip to content
Snippets Groups Projects
Commit b5c8b2a1 authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Added quad tree done in algo

parent 63ae1bc7
No related branches found
No related tags found
No related merge requests found
File added
No preview for this file type
#include <stdio.h>
typedef int value_t;
typedef struct node_
{
value_t value;
node_ *child[4]
} node_t;
/**
* @brief Create a tree with a specified depth
*
* @param depth
* @return created node_t*
*/
node_t *create_tree(int depth)
{
if (depth == 0)
return NULL;
node_t *node = malloc(sizeof(node_t));
node->value = 0;
for (int i = 0; i < 4; i++)
{
node->child[i] = create_tree(depth - 1);
}
return node;
}
/**
* @brief Recursivly count the number of nodes in a tree
*
* @param tree
* @return int - Number of nodes
*/
int count_nodes(node_t *tree)
{
int count = 0;
if (tree == NULL)
return 0;
for (int i = 0; i < 4; i++)
{
count += count_nodes(tree->child[i]);
}
}
value_t jmax(value_t a, value_t b)
{
return (a > b) ? a : b;
}
/**
* @brief Count the maximum depth of a tree
*
* @param tree
* @return int - Depth
*/
int count_depth(node_t *tree)
{
int depth = 0;
if (tree == NULL)
return 0;
for (int i = 0; i < 4; i++)
{
// replace depth with new depth if it's bigger
depth = max(depth, count_depth(tree->child[i]));
}
return depth; // + 1 ?
}
node_t *position(node_t *tree, int x, int y)
{
if (tree == NULL)
return NULL;
int d = count_depth(tree);
if (d == 0)
return tree;
while (d > 1)
{
int i = (2 * (y % pow(2, d)) / pow(2, d - 1)) + ((x % pow(2, d)) / pow(2, d - 1));
tree = tree->child[i];
d--;
}
return tree;
}
/**
* @brief Create and fill a tree with image pixels
*
* @param image - image data
* @param x - side size of image
* @return node_t*
*/
node_t *fill_tree(int x, int image[x][x])
{
node_t *tree = create_tree(x);
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x; j++)
{
position(tree, i, j)->value = image[i][j];
}
}
return tree;
}
/**
* @brief Create and fill a matrix from a tree
*
* @param tree - Source tree
* @return int** - Created matrix
*/
int **fill_matrix_from_tree(node_t *tree)
{
int d = pow(count_depth(tree), 2);
// Allocate matrix
int **matrix = malloc(sizeof(int *) * d);
for (int i = 0; i < d; i++)
{
matrix[i] = malloc(sizeof(int) * d);
}
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
matrix[i][j] = position(tree, i, j)->value;
}
}
return matrix;
}
node_t *tree_vert_sym(node_t *tree)
{
int **m = matrix_from_tree(tree);
matrix_vert_sym(m);
tree = fill_tree(m);
return tree;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment