二叉树的 前中后遍历
2017-02-18 11:09
155 查看
#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;
}
相关文章推荐
- 二叉树的前中后遍历
- 数据结构——树(3):二叉树的前中后层序遍历,Morris遍历比较试验
- 二叉树前中后序递归/非递归遍历以及按层遍历
- LinCode 二叉树的前中后遍历
- 程序3——二叉树的前中后层序遍历
- 数据结构(十二) 二叉树的基本操作 --- 创建一个二叉树 前中后序遍历二叉树
- Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】
- 【二叉树5】前中后遍历二叉树的递归和非递归方法
- 学习日志---非递归二叉树游标遍历(前中后层序)
- 二叉树非递归遍历(前中后)
- Java实现二叉树的遍历(多种方式实现前中后序的遍历)
- 二叉树的前中后遍历递归非递归实现
- C++二叉树的前序建立与前中后遍历
- java实现二叉树的建立,前中后序遍历,层次遍历,深度,节点个数等
- 二叉树递归,非递归,前中后遍历
- 二叉树的简单操作(前中后序遍历,层次遍历,树高,叶子数,交换二叉树等)
- java实现二叉树的前中后遍历(递归和非递归)
- 数据结构-二叉树的各种遍历(先中后层序!!)
- 二叉树,前中后遍历,层遍历
- 二叉树的遍历 -前中后--递归非递归 --链表,数组