二叉树的 前中后遍历
来源:互联网 发布:nginx conf 配置 编辑:程序博客网 时间:2024/06/01 09:27
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct BiNode
{
char data;
BiNode *left;
BiNode *right;
}BiNode,*BinaryTre;//定义一个结构体指针 相当于 typedef BiNode *BinaryTre
void visit(BinaryTre t)
{
if(t!=NULL)
cout <<( t->data)<<' ';//t->相当于(*t).
}
int CreatTree(BinaryTre &t)
{
char a;
cin >> a;
if (a == '#')
t=NULL;
else
{
t = (BinaryTre)malloc(sizeof(BiNode));
t->data = a;
CreatTree(t->left);
CreatTree(t->right);
}
return 0;
}
void mid(BinaryTre t)
{
if(t!=NULL)
{
mid(t->left);
visit(t);
mid(t->right);
}
}
void Pre(BinaryTre t)
{
if (t != NULL)
{
visit(t);
Pre(t->left);
Pre(t->right);
}
}
void Bhend(BinaryTre t)
{
if (t != NULL)
{
Bhend(t->left);
Bhend(t->right);
visit(t);
}
}
int main()
{
BinaryTre t;
CreatTree(t);
Pre(t);
cout << endl;
mid(t);
cout << endl;
Bhend(t);
system("pause");
return 0;
}
#include<iostream>
using namespace std;
typedef struct BiNode
{
char data;
BiNode *left;
BiNode *right;
}BiNode,*BinaryTre;//定义一个结构体指针 相当于 typedef BiNode *BinaryTre
void visit(BinaryTre t)
{
if(t!=NULL)
cout <<( t->data)<<' ';//t->相当于(*t).
}
int CreatTree(BinaryTre &t)
{
char a;
cin >> a;
if (a == '#')
t=NULL;
else
{
t = (BinaryTre)malloc(sizeof(BiNode));
t->data = a;
CreatTree(t->left);
CreatTree(t->right);
}
return 0;
}
void mid(BinaryTre t)
{
if(t!=NULL)
{
mid(t->left);
visit(t);
mid(t->right);
}
}
void Pre(BinaryTre t)
{
if (t != NULL)
{
visit(t);
Pre(t->left);
Pre(t->right);
}
}
void Bhend(BinaryTre t)
{
if (t != NULL)
{
Bhend(t->left);
Bhend(t->right);
visit(t);
}
}
int main()
{
BinaryTre t;
CreatTree(t);
Pre(t);
cout << endl;
mid(t);
cout << endl;
Bhend(t);
system("pause");
return 0;
}
0 0
- 二叉树的前中后遍历
- 二叉树的前中后遍历
- 二叉树的 前中后遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- OpenStack 升级实践文章
- hdu2602 Bone Collector 01背包问题
- UWP Windows10开发获取设备位置(经纬度)
- 字符串算法——求最长公共子序列(摘自算法基础,没有代码)
- 专业的个人记帐软件 爱上记帐 1.0.1
- 二叉树的 前中后遍历
- json文件解析为字符串的操作
- BZOJ2792: [Poi2012]Well
- 【C语言简单说】八:分支结构之if(1)
- 前端开源项目持续集成三剑客
- 数据持久化
- JS去除右边逗号的简单方法
- 深度学习(四十八)InfoGAN学习笔记
- 论文免费查重