二叉树遍历

#include <stdio.h>
#include
<stdlib.h>
#define NULL 0
typedef
char DataType;
typedef
struct node
{
    DataType data;
   
struct node *lchild, *rchild;
} BTNode;
BTNode
*createbintree()
{
    BTNode
*t;
   
char x;
    scanf(
"%c",&x);
   
if(x=='#')
        t
=NULL;
   
else
    {
        t
=(BTNode*)malloc(sizeof(BTNode));
        t
->data=x;
        t
->lchild=createbintree();
        t
->rchild=createbintree();
    }
   
return(t);
}
void preorder(BTNode *t)
{
   
if(t!=NULL)
    {
        printf(
"%c/t",t->data);
        preorder(t
->lchild);
        preorder(t
->rchild);
    }
}
void inorder(BTNode *t)
{
   
if(t!=NULL)
    {
        inorder(t
->lchild);
        printf(
"%c/t",t->data);
        inorder(t
->rchild);
    }
}
void postorder(BTNode *t)
{
   
if(t!=NULL)
    {
        postorder(t
->lchild);
        postorder(t
->rchild);
        printf(
"%c/t",t->data);
    }
}
void main()

    BTNode
*t;
    t
= createbintree();
    printf(
"xian xu bian li:");
    preorder(t);
    printf(
"zhong xu bian li:");
    inorder(t);
    printf(
"zhong xu bian li:");
    postorder(t);
}

你可能感兴趣的:(c,struct,null,include)