假设二叉树采用二叉链表存储结构,设计算法实现:(非递归)实现下列功能

三月神 三月神 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
假设二叉树采用二叉链表存储结构,设计算法实现:(非递归)实现下列功能
1、对于用户输入的任何结点值计算出该二叉树中的层次。
2、设计1个算法输出从没个叶子结点到根结点的路径。



建立二叉树链表代码已给出,请帮忙做出2个功能
#include<stdio.h>
#include<malloc.h>
#define M 10
typedef int DataType;/*元素的数据类型*/
typedef struct node
{
DataType data;
struct node *lchild,*rchild;
}BitTNode,*BiTree;
int front=0,rear=0;
BitTNode *que[10];
BitTNode *creat()
{
BitTNode *t;
DataType x;
scanf("%d",&x);
if(x==0) t=NULL;
else
{
t=(BitTNode *)malloc(sizeof(BitTNode));
t->data=x;
t->lchild=creat();
t->rchild=creat();
}
return(t);
}/*creat*/
void enqueue(BitTNode *t)//入队
{
if (front!=(rear+1)%M)
{
rear=(rear+1)%M;
que[rear]=t;
}
}/*enqueue*/
BitTNode * delqueue()//出队
{
if(front==rear)return NULL;
{
front=(front+1)%M;
return (que[front]);
}
}
void levorder(BiTree t)//层次遍历
{
BitTNode *p;
if(t!=NULL)
{
enqueue(t);
while (front!=rear)
{
p=delqueue();
printf("%4d",p->data);
if(p->lchild!=NULL) enqueue(p->lchild);
}
}
}
void main()
{
BitTNode *root;
root=creat();
printf("\n层次顺序遍历二叉树");
levorder(root);
}
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
三月神
数据结构与算法 33,027 成员 35,334 内容