#include <stdio.h>
#include <malloc.h>
#include <iostream>
using namespace std;
typedef struct BiNode
{
char data;
struct BiNode *lch;
struct BiNode *rch;
}BiNode,*BiTree;
void creat(BiTree &T)
{
T=(BiNode*)malloc(sizeof(BiNode));
scanf("%c",&T->data);
if(T->data==',') T=NULL;
if(T)
{
creat(T->lch);
creat(T->rch);
}
}
//先序遍历
void xian(BiTree T)
{
if(T)
{
printf("%c",T->data);
xian(T->lch);
xian(T->rch);
}
}
//中序遍历
void zhong(BiTree T)
{
if(T)
{
zhong (T->lch);
printf("%c",T->data);
zhong (T->rch);
}
}
//后序遍历
void hou(BiTree T)
{
if(T)
{
hou(T->lch);
hou(T->rch);
printf("%c",T->data);
}
}
int main()
{
BiTree T;
creat(T);
xian(T);
cout<<endl;
zhong (T);
cout<<endl;
hou(T);
cout<<endl;
}
二叉树的建立及前中后序遍历
最新推荐文章于 2023-03-07 09:42:55 发布