二叉树的遍历和建立解决思路

二叉树的遍历和建立二叉树的遍历和建立(前序遍历,中序遍历,后序遍历)[解决办法]http://www.chineselinuxun

二叉树的遍历和建立
二叉树的遍历和建立(前序遍历,中序遍历,后序遍历)

[解决办法]
http://www.chineselinuxuniversity.net/articles/22902.shtml
[解决办法]

C/C++ code
#include <stdio.h>#include <stdlib.h>typedef struct BiTNode{    char data;    struct BiTNode *lchild;    struct BiTNode *rchild;} BiTNode;BiTNode *CreatBiTree();void PreOrderTraverse(BiTNode *);void InOrderTraverse(BiTNode *);void PostOrderTraverse(BiTNode *);void DestroyBiTree(BiTNode **);int main(){    BiTNode *pT = CreatBiTree();    PreOrderTraverse(pT);    printf("\n");    InOrderTraverse(pT);    printf("\n");    PostOrderTraverse(pT);    printf("\n");    DestroyBiTree(&pT);    printf("\n");    return 0;}BiTNode *CreatBiTree(){    char ch;    BiTNode *p;    ch = getchar();    if (ch == EOF)        return NULL;    getchar();    p = (BiTNode *)malloc(sizeof(BiTNode));    p->data = ch;    p->lchild = CreatBiTree();    p->rchild = CreatBiTree();    return p;}void PreOrderTraverse(BiTNode *pT){    if (pT != NULL)    {        printf("%c ", pT->data);        PreOrderTraverse(pT->lchild);        PreOrderTraverse(pT->rchild);    }}void InOrderTraverse(BiTNode *pT){    if (pT != NULL)    {        InOrderTraverse(pT->lchild);        printf("%c ", pT->data);        InOrderTraverse(pT->rchild);    }}void PostOrderTraverse(BiTNode *pT){    if (pT != NULL)    {        PostOrderTraverse(pT->lchild);        PostOrderTraverse(pT->rchild);        printf("%c ", pT->data);    }}void DestroyBiTree(BiTNode **pT){    if (*pT != NULL)    {         DestroyBiTree(&(*pT)->lchild);         DestroyBiTree(&(*pT)->rchild);         free(*pT);         *pT = NULL;    }}
[解决办法]
一个先序遍历的模板,中序后序稍微修改即可。

C/C++ code
void scan(btree *bt) {    if(bt == NULL)        return;    visit(bt);    if(bt->lc != NULL) visit(bt->lc);    if(bt->rc != NULL) visit(bt->rc);}