c语言 多叉树结构相关

实现多叉树时,由于非叶子节点的子节点的个数不同,所以如何动态的创建节点是个问题


数据结构:
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的,自己理解吧... */
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值