二叉树

//二叉树建立,以及三种顺序的输出

#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

typedef char ElemType;
typedef struct Node
{
    ElemType data;
    struct Node *lChild;
    struct Node *rChild;
}BiNode, *BiTree;

BiTree CreatBiTree()
{
    char ch;
    BiTree t;
    ch=getchar();
    if(ch=='1')
        t=NULL;
    else
    {
        t=(BiTree)malloc(sizeof(BiNode));
        t->data=ch;
        t->lChild=CreatBiTree();
        t->rChild=CreatBiTree();
    }
    return t;
}

void preOrder(BiTree t)
{
    if(t)
    {
        putchar(t->data);
        preOrder(t->lChild);
        preOrder(t->rChild);
    }
}

void inOrder(BiTree t)
{
    if(t)
    {
        inOrder(t->lChild);
        putchar(t->data);
        inOrder(t->rChild);
    }
}

void posOrder(BiTree t)
{
    if(t)
    {
        posOrder(t->lChild);
        posOrder(t->rChild);
        putchar(t->data);
    }
}

int main()
{
    BiTree t;
    t=CreatBiTree();
    printf("先序遍历:");
    preOrder(t);
    printf("\n");
    printf("中序遍历:");
    inOrder(t);
    printf("\n");
    printf("后序遍历:");
    posOrder(t);
    printf("\n");
    return 0;
}

 

posted on 2016-11-07 19:37  南风丶丶  阅读(117)  评论(0编辑  收藏  举报