二叉树的建立及前中后序遍历

来源:互联网 发布:mac软件哪个网站好 编辑:程序博客网 时间:2024/05/28 11:50
#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;}

0 0
原创粉丝点击