二叉树的前序,中序,后序遍历

来源:互联网 发布:jsp中注释java代码 编辑:程序博客网 时间:2024/05/20 06:07


#include<stdio.h>#include<stdlib.h>#define ElemType chartypedef struct Node{    char data;    struct Node*LChild;    struct Node*RChild;} BiTNode,*BiTree;void GreateBiTree(BiTree *root){    char ch;    ch=getchar();    if(ch=='.')*root=NULL;    else    {        *root=(BiTree)malloc(sizeof(BiTNode));        (*root)->data=ch;        GreateBiTree(&((*root)->LChild));        GreateBiTree(&((*root)->RChild));    }}void PreOrder(BiTree root){    if(root!=NULL)    {        printf("%c ",root->data);        PreOrder(root->LChild);        PreOrder(root->RChild);    }}void InOrder(BiTree root){    if(root!=NULL)    {        InOrder(root->LChild);        printf("%c ",root->data);        InOrder(root->RChild);    }}void PostOrder(BiTree root){    if(root!=NULL)    {        PostOrder(root->LChild);        PostOrder(root->RChild);        printf("%c ",root->data);    }}int main(){    BiTree root;    GreateBiTree(&root);    PreOrder(root);    printf("\n");    InOrder(root);    printf("\n");    PostOrder(root);    printf("\n");}


原创粉丝点击