1.编写算法函数int equal(tree t1, tree t2),判断两棵给定的树是否等价;程序员
int equal(tree t1,tree t2) 算法
{ int k; if(t1==NULL&&t2==NULL) 编程
return TRUE; elseif(t1!=NULL&&t2==NULL||t1==NULL&&t2!=NULL) 数据结构
{ return FALSE; 编程语言
}elseif(t1->data!=t2->data)函数
{return FALSE;学习
}for(k=0;kchild[k],t2->child[k]);设计
if(equal(t1->child[k],t2->child[k])==FALSE)blog
{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;
}elseif(!empty(&s))
{t=pop(&s);t=t->rchild;
}18}
3.编写算法函数degree(LinkedGraph g)输出以邻接表为存储结构的无向图的各顶点的度。
void degree(LinkedGraph g)
{ int k; int n; EdgeNode *p;
for(k=0;knext; }
if(k==0)
{printf("%d\n",n);
}else {printf("%d\n",n);} }}
以上就介绍了数据结构(C语言)关于树、二叉树、图的基本操做。但愿有所帮助。
若是你也想成为程序员,想要快速掌握编程,赶忙关注小编加入学习企鹅圈子吧!
里面有资深专业软件开发工程师,在线解答你的全部疑惑~编程语言入门“so easy”
免费学习资料: