数据结构(C语言)关于树、二叉树、图的基本操作。

时间:2023-03-09 20:53:31
数据结构(C语言)关于树、二叉树、图的基本操作。

1) 编写算法函数int equal(tree t1, tree t2),判断两棵给定的树是否等价;

  int equal(tree t1,tree t2)
{
int k;
if(t1==NULL&&t2==NULL)
return TRUE;
else if(t1!=NULL&&t2==NULL||t1==NULL&&t2!=NULL)
{
return FALSE;
}
else if(t1->data!=t2->data)
{
return FALSE;
}
for(k=;k<m;k++)
{
equal(t1->child[k],t2->child[k]);
if(equal(t1->child[k],t2->child[k])==FALSE)
{
return FALSE;
}
else
return TRUE;
}
}

2) 编写算法函数void preorder(bintree t)实现二叉树t的非递归前序遍历;

 void preorder1(bintree t)
{
seqstack s;
init(&s);
while(t||!empty(&s))
{
if(t)
{
printf("%c",t->data);
push(&s,t);
t=t->lchild;
}
else if(!empty(&s))
{
t=pop(&s);
t=t->rchild;
}
}

3)编写算法函数degree(LinkedGraph g)输出以邻接表为存储结构的无向图的各顶点的度。

 void degree(LinkedGraph g)
{
int k;
int n;
EdgeNode *p;
for(k=;k<g.n;k++)
{
p=g.adjlist[k].FirstEdge;
n=;
while(p!=NULL)
{
n++;
p=p->next;
}
if(k==)
{
printf("%d\n",n);
}
else
{
printf("%d\n",n);
}
}
}