简体   繁体   中英

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.

struct quadtree **p
printf("%c\n" , p->colour);

You need to dereference p to use it as a pointer and extract colour

Also, you may want to look at adding a printTree() function that can walk your tree and print each node. 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);

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; . You can change the name of the global variable in order to avoid ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM