假设二叉树采用二叉链表存储结构,设计算法实现:(非递归)实现下列功能
假设二叉树采用二叉链表存储结构,设计算法实现:(非递归)实现下列功能
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);
}
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);
}
...全文
相关推荐
数据结构 建立二叉树二叉链表存储结构实现有关操作 实验报告
建立二叉树的二叉链表存储结构实现以下操作(选择其中的两个做) (1)输出二叉树 (2)先序遍历二叉树 (3) 中序遍历二叉树 (4)后序遍历二叉树 (5)层次遍历二叉树
二叉链表作存储结构,设计求二叉树高度的算法
以二叉链表作存储结构,设计求二叉树高度的算法。
数据结构课程设计二叉树采用二叉链表作为存储结构
编写按层次顺序(同一层自左至右)遍历二叉树的算法。 (1)二叉树采用二叉链表作为存储结构。 (2)按题集p44面题6.69所指定的格式输出建立的二叉树。 (3)输出层次遍历结果。 (4)测试用例自己设计。
二叉树的二叉链表表示与实现
二叉树的二叉链表表示与实现,包括二叉树的建立、遍历、深度、叶子节点数等
二叉树的链式存储结构-二叉链表
数据结构-二叉树的基本操作。 二叉树的链式存储结构-二叉链表 各种操作都有。 二叉树使用链表能避免顺序储存浪费空间的问题,算法和结构相对简单。


