[英]Creating nodes in a tree
I am having problems with this code: 我在使用此代码时遇到问题:
#include<stdlib.h>
#include<stdio.h>
struct quadtree
{
char colour;
int x_coordinate;
int y_coordinate;
int size;
struct quadtree *NW, *NE, *SE, *SW, *p;
};
static struct quadtree *p = NULL;
int size;
int i;
int pixels;
int width;
int black_pixels;
void insert(struct quadtree **p , char colour , int size , int x_coordinate , int y_coordinate)
{
struct quadtree *new;
new = (struct quadtree *) malloc ( sizeof(struct quadtree) );
new->NW = new->NE = new->SE = new->SW = NULL;
new->colour = colour;
new->size = size;
new->x_coordinate = x_coordinate;
new->y_coordinate = y_coordinate;
*p = new;
/* printf("%c\n" , p->colour); */
printf("%c\n" , new->colour);
return;
}
void colour_test(int x[] , int y[] , int size , int x_coordinate , int y_coordinate , struct quadtree *p)
{
pixels = 0;
for (i = 0 ; i < black_pixels ; i++)
if (x[i] >= x_coordinate && x[i] < (size + x_coordinate) && y[i] <= y_coordinate && y[i] > (y_coordinate - size))
pixels++;
if (pixels == 0)
{
insert(&p , 'W' , size , x_coordinate , y_coordinate);
/* printf("Node has coordinates (%d,%d) and a size of %d\n" , x_coordinate , y_coordinate , size); */
}
else if (pixels == size*size)
{
insert(&p , 'B' , size , x_coordinate , y_coordinate);
/* printf("Node has coordinates (%d,%d) and a size of %d\n" , x_coordinate , y_coordinate , size); */
}
else
{
insert(&p , 'G' , size , x_coordinate , y_coordinate);
/* printf("Node has coordinates (%d,%d) and a size of %d\n" , x_coordinate , y_coordinate , size); */
colour_test(x , y , size/2 , x_coordinate , (y_coordinate - (size/2)) , p->NW);
colour_test(x , y , size/2 , (x_coordinate + (size/2)) , (y_coordinate - (size/2)) , p->NE);
colour_test(x , y , size/2 , (x_coordinate + (size/2)) , y_coordinate , p->SE);
colour_test(x , y , size/2 , x_coordinate , y_coordinate , p->SW);
}
}
int main()
{
scanf("%d" , &width);
scanf("%d" , &black_pixels);
int x[black_pixels];
int y[black_pixels];
for (i = 0 ; i < black_pixels ; i++)
scanf("%d%*[ ]%d" , &x[i] , &y[i]);
/*
printf("Image width = %d\n" , width );
printf("Total black pixels = %d\n" , black_pixels);
for (i = 0 ; i < black_pixels ; i++)
printf("%d %d\n" , x[i] , y[i]);
*/
size = width;
colour_test(x , y , size , width - size , (width - 1) , p);
return 0;
}
It works almost as intended, proccessing an image in quadrants of the same colour. 它几乎按预期工作,以相同颜色的象限处理图像。 However I am having trouble creating nodes in the tree.
但是我在树中创建节点时遇到了麻烦。 I am attempting to creating a pointer to a new, then load that new node with data and then join the new node to it's parent node.
我试图创建一个指向新节点的指针,然后用数据加载该新节点,然后将新节点加入其父节点。
I tried to print out the node once this was done but I get an error message saying 完成此操作后,我尝试打印出该节点,但收到一条错误消息,提示
request for member 'colour' in something not a structure or union
要求成员在非结构或联合体中使用“颜色”
but when I remove that line I am able to print the data in the new node fine. 但是当我删除该行时,我可以在新节点中正常打印数据。 How can I make sure that the node is actually joining to the tree?
如何确保该节点实际上已加入树中? Do I need to create structure variations to do this: I had a previous piece of code working for a binary tree and I have been trying to adapt it from that.
我是否需要创建结构变体才能做到这一点:我之前有一段代码可用于二叉树,并且我一直在尝试从中进行修改。
p
is declared on the first line as a pointer to a pointer, but you are using it as a pointer when trying to print in the line below. 在第一行中将
p
声明为指向指针的指针,但是在尝试在下面的行中打印时,您将其用作指针。
struct quadtree **p
printf("%c\n" , p->colour);
You need to dereference p
to use it as a pointer and extract colour
您需要取消引用
p
才能将其用作指针并提取colour
Also, you may want to look at adding a printTree()
function that can walk your tree and print each node. 另外,您可能想看看添加一个
printTree()
函数,该函数可以遍历树并打印每个节点。 That might help with being able to debug and visualize your tree. 这可能有助于调试和可视化您的树。
This is because you have a double pointer in your code, which you would have to dereference in order to make that line work: printf("%c\\n" , p->colour);
这是因为您的代码中有一个双指针,为了使该行正常工作,必须将其取消引用:
printf("%c\\n" , p->colour);
That is, for example: 也就是说,例如:
printf("%c\n" , (*p)->colour);
Also, note that you have a global static variable also named p
: static struct quadtree *p = NULL;
另外,请注意,您还有一个全局静态变量,也称为
p
: static struct quadtree *p = NULL;
. 。 You can change the name of the global variable in order to avoid ambiguity.
您可以更改全局变量的名称以避免歧义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.