實作多叉樹時,由于非葉子節點的子節點的個數不同,是以如何動态的建立節點是個問題
資料結構:
struct list
{
/* other data */
int effectif_class_1;
int effectif_class_2;
struct list *parent;
struct list *child[];
}
struct list * findNode(struct list *point)
{
int i;
if(point->data == data)
return point;
i=0;
while(point->child[i] != NULL){
if( (findNode(point->child[i])) == point->child[i])
return point->child[i];
i++;
}
return NULL;
}
void addNode_changeNoeud(struct list *point)
{
int i=0;
while(point->child[i] != NULL)
{
point->child[i]->Noeud ++;
addNode_changeNoeud(point->child[i]);
i++;
}
}
void addNode(struct list *point, /* data */)
{ //在point的最右端插入一個子節點
int i=0;
/* 定位到最右端 */
while(point->child[i] != NULL){
i++;
}
point->child[i] = (struct list *)malloc(sizeof(struct list));
/* 儲存其他資料到 point->child[i]->data */
/* 更改所有父節點的 effectif_class */
struct list *tmp;
tmp = point->parent;
while(tmp != NULL){
tmp->effectif_class_1 = tmp->effectif_class_1 + /* effectif_class_1 */ ;
tmp->effectif_class_2 = tmp->effectif_class_2 + /* effectif_class_2 */ ;
tmp = tmp->parent;
}
/* 插入節點後,更新編号 */
point->child[i] = point->child[i-1] + 1;
addNode_changeNoeud(point); /* 這個不好說,用于更新編号Noeud的,自己了解吧... */
}