SDUT 3341 数据结构实验之二叉树二:遍历二叉树

#include <stdio.h>
#include <stdlib.h>

struct BinTree//节点
{
    char ch;
    struct BinTree *left, *right;
};
typedef struct BinTree *Node; //建立节点指针类型



char pre[150];
int i;

Node Creat()//按照先序遍历创建
{
    Node T;
    if(pre[i] == ',')
    {
        i++;
        return NULL;
    }
    T = (Node)malloc(sizeof(struct BinTree));
    T->ch = pre[i++];

    T->left = Creat();
    T->right = Creat();


    return T;
}

void In(Node T)//中序左根右
{
    if(T == NULL)return;
    In(T->left);
    printf("%c", T->ch);
    In(T->right);
}
void Po(Node T)//后续左右跟
{
    if(T == NULL)return;
    Po(T->left);
    Po(T->right);
    printf("%c", T->ch);
}


int main()
{

    while(~scanf("%s", pre))
    {
        i = 0;
        Node Tree;
        Tree = Creat();
        In(Tree);
        printf("\n");
        Po(Tree);
        printf("\n");
    }


    return 0;
}

posted @ 2019-03-18 17:27  Probie_Tao  阅读(177)  评论(0编辑  收藏  举报