二叉树的创建和遍历
来源:互联网 发布:新疆商务厅 数据分析 编辑:程序博客网 时间:2024/05/21 19:21
#include<stdio.h>
#include<stdlib.h>
typedef struct NODE
{
char data;
struct NODE *Lchild;
struct NODE *Rchild;
}BiTree;
BiTree *ChangeTree(BiTree *root)
{
char ch;
ch=getchar();
if(ch=='#')
root=NULL;
else
{
root=(BiTree *)malloc(sizeof(BiTree));
root->data=ch;
root->Lchild=ChangeTree(root->Lchild);
root->Rchild=ChangeTree(root->Rchild);
}
return root;
}
void PreOder(BiTree *root)
{
if(root)
{
printf("%c",root->data);
PreOder(root->Lchild);
PreOder(root->Rchild);
}
}
void InOder(BiTree *root)
{
if(root)
{
InOder(root->Lchild);
printf("%c",root->data);
InOder(root->Rchild);
}
}
void PostOder(BiTree *root)
{
if(root)
{
PostOder(root->Lchild);
PostOder(root->Rchild);
printf("%c",root->data);
}
}
int main()
{
BiTree *root;
root=ChangeTree(root);
PreOder(root);
printf("\n");
InOder(root);
printf("\n");
PostOder(root);
printf("\n");
}
#include<stdlib.h>
typedef struct NODE
{
char data;
struct NODE *Lchild;
struct NODE *Rchild;
}BiTree;
BiTree *ChangeTree(BiTree *root)
{
char ch;
ch=getchar();
if(ch=='#')
root=NULL;
else
{
root=(BiTree *)malloc(sizeof(BiTree));
root->data=ch;
root->Lchild=ChangeTree(root->Lchild);
root->Rchild=ChangeTree(root->Rchild);
}
return root;
}
void PreOder(BiTree *root)
{
if(root)
{
printf("%c",root->data);
PreOder(root->Lchild);
PreOder(root->Rchild);
}
}
void InOder(BiTree *root)
{
if(root)
{
InOder(root->Lchild);
printf("%c",root->data);
InOder(root->Rchild);
}
}
void PostOder(BiTree *root)
{
if(root)
{
PostOder(root->Lchild);
PostOder(root->Rchild);
printf("%c",root->data);
}
}
int main()
{
BiTree *root;
root=ChangeTree(root);
PreOder(root);
printf("\n");
InOder(root);
printf("\n");
PostOder(root);
printf("\n");
}
0 0
- 二叉搜索树的创建和遍历
- 二叉树的创建,遍历和释放
- 简单的二叉树创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 数据结构---二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和前序中序后序遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉树的创建和遍历
- 二叉查找树的创建和遍历
- 二叉树的递归创建和遍历
- 二叉树的创建和遍历
- 5.Ext JS actioncolumn动态加载图标/提示
- ARM架构图
- python string
- jQuery_表单元素过滤选择器
- [Unity]利用Mesh在Unity中绘制扇形图片2
- 二叉树的创建和遍历
- MySQL学习日志 数据库的基本命令
- Eclipse最有用的快捷键
- 写2016,望2017
- Xamarin只言片语1——Xamarin下的弹框
- C++ 点(.)操作符和箭头(->)操作符
- AngularJS的$http服务基础用法
- 图片服务器
- spring的入门