您的位置:首页 > 其它

二叉树的 前中后遍历

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: