二叉树的遍历
来源:互联网 发布:网络有什么坏处 编辑:程序博客网 时间:2024/05/23 16:51
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
typedef struct BiTNode
{
char data;
struct BiTNode *Lchild,*Rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
printf("Error!");
T->data=ch;
T->Lchild=Create(T->Lchild);
T->Rchild=Create(T->Rchild);
}
return T;
}
void Preorder(BiTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->Lchild);
Preorder(T->Rchild);
}
}
void zhongxu(BiTree T)
{
if(T)
{
zhongxu(T->Lchild);
printf("%c",T->data);
zhongxu(T->Rchild);
}
}
void houxu(BiTree T)
{
if(T)
{
houxu(T->Lchild);
houxu(T->Rchild);
printf("%c",T->data);
}
}
void main()
{
BiTree T;
printf("请输入数据:\n");
T=Create(T);
printf("先序遍历为:\n");
Preorder(T);
printf("中序遍历为:\n");
zhongxu(T);
printf("后序遍历为:\n");
houxu(T);
printf("\n");
}
#include<string.h>
#include<malloc.h>
#define NULL 0
typedef struct BiTNode
{
char data;
struct BiTNode *Lchild,*Rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
printf("Error!");
T->data=ch;
T->Lchild=Create(T->Lchild);
T->Rchild=Create(T->Rchild);
}
return T;
}
void Preorder(BiTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->Lchild);
Preorder(T->Rchild);
}
}
void zhongxu(BiTree T)
{
if(T)
{
zhongxu(T->Lchild);
printf("%c",T->data);
zhongxu(T->Rchild);
}
}
void houxu(BiTree T)
{
if(T)
{
houxu(T->Lchild);
houxu(T->Rchild);
printf("%c",T->data);
}
}
void main()
{
BiTree T;
printf("请输入数据:\n");
T=Create(T);
printf("先序遍历为:\n");
Preorder(T);
printf("中序遍历为:\n");
zhongxu(T);
printf("后序遍历为:\n");
houxu(T);
printf("\n");
}
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 像素大小的计算
- android 菜单如何显示图标.
- 基于JavaScript、Javabean、Servlet、ajax的异步请求登录注册找回密码Javaweb项目
- WepAPI下载与上传文件
- iOS开发系列---如何从菜鸟到专家
- 二叉树的遍历
- java继承
- Java—内部类(普通,静态,局部,匿名)
- Maven学习总结(一)——Maven入门
- gcj 2008 apac problem c
- Golang 语法 知识点提要
- 类。字段。方法。属性之间的定义及部分区别。
- 使用iscroll的问题与解决办法
- python json操作