数据结构试验(用C语言)建立一棵二叉树,并用递归或者非递归的算法分别用先序.中序和后序遍历、谢谢
来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/04/29 10:49:58
数据结构试验(用C语言)建立一棵二叉树,并用递归或者非递归的算法分别用先序.中序和后序遍历、谢谢
能附一点实验思路更好
能附一点实验思路更好
#define LEN sizeof(struct tree)
#define NULL 0
#include
#include
struct tree
{
char data;
struct tree *lchild,*rchild;
};
//创建二叉树
struct tree *creat()
{
char c;
struct tree *t;
c=getchar();
if(c==' ')
t=NULL;
else
{
t=(struct tree*)malloc(LEN);
t->data=c;
t->lchild=creat();
t->rchild=creat();
}
return t;
}
//前序遍历
void Preprint(struct tree*t)
{
if(t!=NULL)
{
printf("%c->",t->data);
Preprint(t->lchild);
Preprint(t->rchild);
}
}
//中序遍历
void Inprint(struct tree*t)
{
if(t!=NULL)
{
Inprint(t->lchild);
printf("%c->",t->data);
Inprint(t->rchild);
}
}
//后序遍历
void Postprint(struct tree*t)
{
if(t!=NULL)
{
Postprint(t->lchild);
Postprint(t->rchild);
printf("%c->",t->data);
}
}
main()
{
struct tree *t;
printf("Please input tree in order:\n");
t=creat();
printf("The result of Preorder traversal is\n");
Preprint(t);
printf("^\nThe result of Inorder traversal is\n");
Inprint(t);
printf("^\nThe result of Postorder traversal is\n");
Postprint(t);
printf("^\n");
getch();
}
#define NULL 0
#include
#include
struct tree
{
char data;
struct tree *lchild,*rchild;
};
//创建二叉树
struct tree *creat()
{
char c;
struct tree *t;
c=getchar();
if(c==' ')
t=NULL;
else
{
t=(struct tree*)malloc(LEN);
t->data=c;
t->lchild=creat();
t->rchild=creat();
}
return t;
}
//前序遍历
void Preprint(struct tree*t)
{
if(t!=NULL)
{
printf("%c->",t->data);
Preprint(t->lchild);
Preprint(t->rchild);
}
}
//中序遍历
void Inprint(struct tree*t)
{
if(t!=NULL)
{
Inprint(t->lchild);
printf("%c->",t->data);
Inprint(t->rchild);
}
}
//后序遍历
void Postprint(struct tree*t)
{
if(t!=NULL)
{
Postprint(t->lchild);
Postprint(t->rchild);
printf("%c->",t->data);
}
}
main()
{
struct tree *t;
printf("Please input tree in order:\n");
t=creat();
printf("The result of Preorder traversal is\n");
Preprint(t);
printf("^\nThe result of Inorder traversal is\n");
Inprint(t);
printf("^\nThe result of Postorder traversal is\n");
Postprint(t);
printf("^\n");
getch();
}
数据结构试验(用C语言)建立一棵二叉树,并用递归或者非递归的算法分别用先序.中序和后序遍历、谢谢
二叉树中序遍历非递归算法(c语言实现)
数据结构C递归的方法 前序 中序 后序 交换二叉树每个结点的左孩子和右孩子 结点个数 深度 叶结点个数
用C语言构造一棵线索二叉树,后序遍历线索二叉树如何遍历
二叉数的前序、中序、后续三种方式的递归与非递归的算法.
已知二叉树的先根遍历和中序遍历,求后序遍历的算法?
数据结构已知一个二叉树中结点的左右孩子为left和right,r指向二叉树的某一结点.请用C编一个非递归函数postfi
一棵二叉树的先序遍历次序为ABDGECFH,中序遍历次序为DGBEAFHC,则其后序遍历次序为多少呢?(数据结构试题
C语言程序题:1、编写一个求n!的函数fact(n),要求fact函数分别用递归和非递归两种方法实现
C语言用非递归算法求数列.
c语言基础知识的二叉树的遍历算法
已知二叉树的后序遍历序列和中序遍历序列,怎样求其前序遍历序列!