首先,对不起,如果那里的代码很大。我将尝试模块化以使其简单。其次,这是一个奇怪的错误,所以我在示例中使用了一个直观的 main,如果有人想测试它。我希望它在代表递归象限的四叉树上插入点。如果插入的点碰到上限,则将其除以 4,并且将限制分为 4 个不同的象限。我试图插入点 (16.0,16.0) (双)但不起作用。我尝试过的所有其他点都可以正常工作(它是一个双变量,所以很明显,我不能测试所有的值)。当它击中上限时,完美地划分和新点。只有 16,16 没有(我尝试了很多数字......几十个)。编辑:当我使四叉树变大时,它开始不 t 插入一些点(即使是之前插入的一些点,当它较小时)。好的:这是最高级别的功能:
//This will insert on a tree (that have a root)
void insert_dot(Tree * A, Dot b){
if (b.x<0 || b.y<0.0){
printf("Invalid coordinates");
return;
}
Quad_node * Aux;
if (A->start_->key->used_capacity > A->start_->key->max_capacity){
printf("invalid result\n");
}
else if (A->start_->key->used_capacity < A->start_->key->max_capacity){
A->start_->key->dots_[(A->start_->key->used_capacity)].x=b.x;
A->start_->key->dots_[(A->start_->key->used_capacity)].y=b.y;
A->start_->key->used_capacity++;
}
else{
Aux=return_leaf_tree(A,b);
insert_dot_quadrant(Aux,b);
}
}
//This is the auxiliar function
void insert_dot_quadrant(Quad_node * A,Dot b ){
Quad_node * Aux,* Aux2;
Dot * aux=(Dot*)malloc(A->key->max_capacity*sizeof(Dot));
int i;
if (A->key->used_capacity > A->key->max_capacity){
printf("invalid result\n");
return;
}
else if (A->key->used_capacity < A->key->max_capacity){
A->key->dots_[(A->key->used_capacity)].x=b.x;
A->key->dots_[(A->key->used_capacity)].y=b.y;
A->key->used_capacity++;
}
else{
for(i=0;i<A->key->max_capacity;i++){
aux[i].x=A->key->dots_[i].x;
aux[i].y=A->key->dots_[i].y;
A->key->dots_[i].x=-1.0;
A->key->dots_[i].y=-1.0;
}
Aux=divide_quadrant(A);
for (i=0;i<A->key->max_capacity;i++){
Aux2=return_leaf_node(Aux,aux[i]);
insert_dot_quadrant(Aux2,aux[i]);
}
}
free(aux);
}
辅助功能(相关):
Quad_node * return_leaf_tree(Tree * A,Dot b){
Quad_node * Aux=(Quad_node_Pointer)malloc(sizeof(Quad_node));
Aux=return_leaf_node(START_(A),b);
return (Aux);
}
Quad_node * return_leaf_node(Quad_node * A,Dot b){
if (A==NULL){
printf("invalid node\n");
return (NULL);
}
Quad_node * Aux,*Aux2;
Aux=(Quad_node_Pointer) malloc (sizeof(Quad_node));
Aux2=(Quad_node_Pointer) malloc (sizeof(Quad_node));
Aux=A;
Aux2=A;
while (Aux!=NULL && Aux->key->used_capacity == Aux->key->max_capacity){
if (b.x==16)
printf("16 entered\n");
if ((Aux->key->max.x)/2 <= b.x && b.x <= Aux->key->max.x && (Aux->key->max.y)/2 <= b.y && b.y <= Aux->key->max.y){
Aux->father=Aux;
Aux=Aux->child[0];
if (b.x==16)
printf("16 here on child[0]\n");
}
else if (0 <= b.x && b.x < (Aux->key->max.x)/2 && (Aux->key->max.y)/2 <= b.y && b.y <= (Aux->key->max.y)){
Aux->father=Aux;
Aux=Aux->child[1];
if (b.x==16)
printf("16 here on child[1]\n");
}
else if (0 <= b.x && b.x < (Aux->key->max.x)/2 && 0 <= b.y && b.y < (Aux->key->max.y)/2){
Aux->father=Aux;
Aux=Aux->child[2];
if (b.x==16)
printf("16 here on child[2]\n");
}
else if ((Aux->key->max.x)/2 <= b.x && b.x <= Aux->key->max.x && 0 <= b.y && b.y < (Aux->key->max.y)/2){
Aux->father=Aux;
Aux=Aux->child[3];
if (b.x==16)
printf("16 here on child[3]\n");
}
else{
printf("x=%f , y=%f\n",b.x,b.y);
printf("max x= %f , max y=%f\nmin x=%f , min y=%f\n",Aux->key->max.x,Aux->key->max.y,Aux->key->min.x,Aux->key->min.y);
printf("erro\n");
if (b.x==16)
printf("16 here on invalid position\n");
return (EXIT_SUCCESS);
}
if (Aux!=NULL){
Aux2=Aux;
}
}
return (Aux2);
}
Quad_node * divide_quadrant(Quad_node * A){
if(A==NULL){
printf("invalid Quad_node");
return (NULL);
}
int i;
Dot max_aux,min_aux;
for (i=0;i<4;i++){
A->child[i]=(Quad_node_Pointer) malloc (sizeof(Quad_node));
}
max_aux.x= (A->key->max.x);
min_aux.x=(A->key->max.x)/2;
max_aux.y=(A->key->max.y);
min_aux.y=(A->key->max.y)/2;
A->child[0]->key=Create_quadrant(A->key->max_capacity,max_aux,min_aux);
max_aux.x=(A->key->max.x)/2;
min_aux.x=0;
max_aux.y=A->key->max.y;
min_aux.y=(A->key->max.y)/2;
A->child[1]->key=Create_quadrant(A->key->max_capacity,max_aux,min_aux);
max_aux.x=(A->key->max.x)/2;
min_aux.x=0;
max_aux.y=(A->key->max.y)/2;
min_aux.y=0;
A->child[2]->key=Create_quadrant(A->key->max_capacity,max_aux,min_aux);
max_aux.x=A->key->max.x;
min_aux.x=(A->key->max.x)/2;
max_aux.y=0;
min_aux.y=(A->key->max.y)/2;
A->child[3]->key=Create_quadrant(A->key->max_capacity,max_aux,min_aux);
return (A);
}
主要功能和其他不太相关的功能(我几乎可以肯定它们 100% 没问题。只是为了运行主要功能):
Quadrant * Create_quadrant (int capacity, Dot max_, Dot min_){
Quadrant * A;
A=(Quadrant*)malloc(sizeof(Quadrant));
if (A==NULL){
printf("null value");
return (NULL);
}
A->dots_ = (Dot*) malloc (capacity * sizeof(Dot));
int i;
for (i=0;i<capacity;i++){
A->dots_[i].x=-1;
A->dots_[i].y=-1;
}
A->max_capacity=capacity;
A->used_capacity=0;
A->max.x=max_.x;
A->max.y=max_.y;
A->min.y=min_.y;
A->min.x=min_.x;
return (A);
}
Tree * Create_tree (int capacity,Dot max){
Tree * A;
A=(Tree*)malloc(sizeof(Tree));
Dot min;
min.x=0;
min.y=0;
A->start_=(Quad_node_Pointer) malloc (sizeof(Quad_node));
A->start_->key=Create_quadrant(capacity,max,min);
return (A);
}
void tree_walk (Quad_node * a){
int i;
if (a!=NULL){
for(i=0;i<a->key->used_capacity;i++){
if (a->key->dots_[i].x >= 0)
printf("x= %f y=%f\n", a->key->dots_[i].x,a->key->dots_[i].y);
}
for(i=0;i<4;i++){
if (a->child[i]!=NULL){
printf("child[%i] not null\n",i);
tree_walk(a->child[i]);
}
else
printf("null child[%i] node\n",i);
}
}
else
return;
}
void read_tree (Tree * A){
tree_walk(START_(A));
}
Quad_node * START_(Tree * A){
return A->start_;
}
主要的:
int main(int argc, char *argv[])
{
printf("\n---------------\n");
Tree * A;
int i;
Dot b,max,teste[12];
b.x=5.0;
b.y=6.0;
max.x=30;
max.y=30;
A = Create_tree(8,max);
for (i=0;i<12;i++){
teste[i].x=(double)2.0*i;
teste[i].y=(double)2.0*i;
printf("dot %i:\n x=%f , y=%f\n",i,teste[i].x,teste[i].y);
insert_dot(A,teste[i]);
}
insert_dot(A,b);
printf("dot 10:\n x=%f , y=%f\n",b.x,b.y);
printf("\n---------------\n\n");
return_leaf_node(A->start_,teste[9]);
read_tree(A);
printf("success\n");
free(A);
return EXIT_SUCCESS;
}
OBS:只能插入正值。任何人都可以帮助我吗?感谢阅读/回答!