0

我正在编写一个程序来创建一个随机大小的三叉树。也就是我给每个节点随机添加0-3个分支,然后看看这棵树有多大。目标是运行几千次,然后进行统计——大小、直径、深度等。

我使用一个基本的树结构,然后使用一个指针指针,以便能够根据节点(0、1、2、3)的创建时间对它们进行排序,称为 tab_of_order。

但是,当前代码随机崩溃(可能 5 次中有 1 次)。在底部,我给出了一个崩溃的例子。ArbreAlea() 似乎运行良好,但返回时崩溃。我也不明白为什么。在它停止崩溃之前,我无法想象为 1000 个案例做一个循环。

另一方面,在创建随机树的函数开始时,我收到一个指针警告“警告:来自不兼容指针类型的赋值[默认启用]”。这会导致我的功能崩溃吗?知道如何解决吗?

我没有粘贴的两个函数只是用于创建概率变化的随机数(0,1,2,3,4)的函数。

非常感谢任何帮助!

#include<stdlib.h>
#include<stdio.h>

// Structure of the tree. Position is the value of the node. The nodes are 
// numbered 1 to n thus the number gives it's position 
struct tree_el {
    int position;
    struct tree_el * first, * second, * third, * fourth;
};
typedef struct tree_el myNode;

//Structure of the data to be analysed
struct donnees {
 int nb_vertices;
 int nb_feuilles;
 int profondeur;
 int largeur;
 int diametre;
};

//Simplified queue structure
struct queue {
int debut;
int fin;
};

//initialize functions
int get_rnd_value (int min, int max);
int rand_loi1 (int iteration);
void add_node (int nb_node_to_add, myNode **tab_of_order, struct queue *arbreQ);
struct donnees *  ArbreAlea (void);

int main(void)
{
    srand((unsigned)time(NULL)); //seed random number generator
int nb_node=0;
struct donnees *resultat;
resultat=(struct donnees*)malloc(sizeof(struct donnees)*1);
    resultat = ArbreAlea();
    printf("\n\nNombre Vertices : %d \n",resultat->nb_vertices);

    return 0;
}

void add_node(int nb_node_to_add, myNode **tab_of_order, struct queue *arbreQ){
    int i;
    int next = arbreQ->debut;

    for (i=1; i <= nb_node_to_add; i++){
        myNode *tmp_node;
        tmp_node = (myNode*) malloc(sizeof(myNode)*1);
        tmp_node->position = arbreQ->fin + i;
        if(i==1){
            tab_of_order[next]->first = tmp_node;
        }
        else if(i==2) {
            tab_of_order[next]->second = tmp_node;
        }
        else if(i==3) {
            tab_of_order[next]->third = tmp_node;
        }
        else if(i==4) {
            tab_of_order[next]->fourth = tmp_node;
        }
        tab_of_order[tmp_node->position] = tmp_node;
        //printf("Tab_of_order[ %d ] %d connected to node %d\n", next , i, tab_of_order[tmp_node->position]->position);
    }
    //printf("add to node %d, %d node(s) is/are added\n", next , nb_node_to_add);

    arbreQ->fin = arbreQ->fin + nb_node_to_add;
    arbreQ->debut +=1;
    tab_of_order[arbreQ->fin + 1] = NULL;
    //printf("\nDebut %d Fin %d\n",arbreQ->debut,arbreQ->fin);

    return;
}

struct donnees * ArbreAlea (void) {
    struct donnees *data;
    //Here i get "warning: assignment from incompatible pointer type [enabled by default]"
    data = (struct donnnes *)malloc(sizeof(struct donnees)*1);
    struct queue *arbreQ;
    arbreQ = (struct queue *)malloc(sizeof(struct queue)*1);
    arbreQ->debut=0;
    arbreQ->fin=0;

    int nb_node = 0;  //nb_node is the number total of nodes in tree.
    int nb_node_to_add;  // nb_node_to_add is how many nodes you have to add for each node.

// this table keeps track of the order of nodes to quickly look up where to add the next node. 
    myNode ** tab_of_order;
    tab_of_order = (myNode **) malloc(sizeof(myNode*) * nb_node);

// First we make the begninning of the tree. This is the only time it can have 4 branches.
// There is a 1/4 chance of having a tree of size one.
    myNode *tree;
    tree = (myNode*) malloc(sizeof(myNode) * 1 ) ;
    tree->position = 0;
    tab_of_order[0] = tree;
    //next =  0;
    nb_node_to_add = rand_loi1(arbreQ->debut);

    if (nb_node_to_add == 0){
        data->nb_vertices=1;
        data->nb_feuilles=0;
        data->profondeur=0;
        data->largeur=0;
        data->diametre=0;
        printf("\nZero Tree");
        return(data);
    }
    else {      
        do {
            nb_node_to_add=rand_loi1(arbreQ->debut);
            printf("\nAdding %d nodes, debut %d, fin %d",nb_node_to_add,arbreQ->debut,arbreQ->fin);
            if (nb_node_to_add==0)
                arbreQ->debut += 1;
            else if (nb_node_to_add>=1)
                add_node(nb_node_to_add, tab_of_order, arbreQ);
            nb_node = nb_node + nb_node_to_add;
            if (tab_of_order[arbreQ->debut]==NULL)
                printf("\n Next is null : %d",arbreQ->debut);
        }while (tab_of_order[arbreQ->debut]!=NULL);                     
    }
    data->nb_vertices=nb_node+1;
    data->profondeur=0;
    data->largeur=0;
    printf("\nTree debut %d fin %d nodes %d",arbreQ->debut,arbreQ->fin,nb_node+1);
    return (data);
}
4

1 回答 1

0

问题是索引越界!

于 2013-08-23T09:53:03.620 回答