在树中创建节点

时间:2015-05-04 16:21:13

标签: c treenode quadtree

我遇到了这段代码的问题:

#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;
   }

它几乎按预期工作,在相同颜色的象限中处理图像。但是我在树中创建节点时遇到了问题。我正在尝试创建一个指向new的指针,然后用数据加载该新节点,然后将新节点加入到它的父节点。

一旦完成,我尝试打印出节点,但收到错误消息

  

请求非结构或联合的成员'color'

但是当我删除该行时,我能够在新节点中打印数据。如何确保节点实际加入树?我是否需要创建结构变体来执行此操作:我有一段代码用于二叉树,我一直在努力使其适应。

2 个答案:

答案 0 :(得分:3)

class matrix { public: matrix(); matrix(int row, int column); ~matrix(); private: const int DEFAULT_SIZE; int size_row, size_column; double *entry; }; // main function int main() { //make a matrix of default size matrix A; /* no error */ delete A; //make 5 matrices of default size matrix *B = new matrix [5]; /* no error */ delete [] B; //make a matrix of size 15x15 matrix C(15, 15); /* no error */ delete C; //make 5 matrices of size 15x15 matrix *D = new matrix(15, 15) [5]; /* compile error !! */ return 0; } //Define functions matrix::matrix() : DEFAULT_SIZE(10) { size_row = DEFAULT_SIZE; size_column = DEFAULT_SIZE; entry = new double [size_row*size_column]; } matrix::matrix(int row, int column) : DEFAULT_SIZE(10) { size_row = row; size_column = column; entry = new double [size_row*size_column]; } matrix::~matrix() { delete [] entry; } 在第一行声明为指向指针,但在尝试在下面的行中打印时,它将它用作指针。

p

您需要取消引用struct quadtree **p printf("%c\n" , p->colour); 才能将其用作指针并提取p

此外,您可能希望查看添加colour函数,该函数可以遍历您的树并打印每个节点。这可能有助于调试和可视化您的树。

答案 1 :(得分:3)

这是因为您的代码中有一个双指针,为了使该行有效,您必须取消引用该指针:printf("%c\n" , p->colour);

即,例如:

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

另请注意,您有一个名为p的全局静态变量:static struct quadtree *p = NULL;。您可以更改全局变量的名称以避免歧义。