二叉树的 前中后遍历

来源:互联网 发布: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;
}
0 0
原创粉丝点击