#include <stdio.h> #include <malloc.h> typedef struct BTNode { int data; struct BTNode * pLchild; struct BTNode * pRchild; }BTNODE,*PBTNODE; PBTNODE CreateBTree(void); void PreTraverseBTree(PBTNODE PT); void InTraverseBTree(PBTNODE PT); void PostTraverseBTree(PBTNODE PT); int main(void) { PBTNODE pT=CreateBTree(); // PreTraverseBTree(pT); // InTraverseBTree(pT); PostTraverseBTree(pT); return 0; } PBTNODE CreateBTree(void) { PBTNODE PA=(PBTNODE)malloc(sizeof(BTNODE)); PBTNODE PB=(PBTNODE)malloc(sizeof(BTNODE)); PBTNODE PC=(PBTNODE)malloc(sizeof(BTNODE)); PBTNODE PD=(PBTNODE)malloc(sizeof(BTNODE)); PBTNODE PE=(PBTNODE)malloc(sizeof(BTNODE)); PA->data='A'; PB->data='B'; PC->data='C'; PD->data='D'; PE->data='E'; PA->pLchild=PB; PA->pRchild=PC; PB->pLchild=PB->pRchild=NULL; PC->pLchild=PD; PC->pRchild=NULL; PD->pLchild=NULL; PD->pRchild=PE; PE->pLchild=PE->pRchild=NULL; return PA; } //先序遍历 void PreTraverseBTree(PBTNODE PT) { if(NULL != PT) { printf("%c\n",PT->data); if(NULL != PT->pLchild) { PreTraverseBTree(PT->pLchild); } if(NULL != PT->pRchild) { PreTraverseBTree(PT->pRchild); } } } //中序遍历 void InTraverseBTree(PBTNODE PT) { if(NULL != PT) { if(NULL != PT->pLchild) { InTraverseBTree(PT->pLchild); } printf("%c\n",PT->data); if(NULL != PT->pRchild) { InTraverseBTree(PT->pRchild); } } } //后序遍历 void PostTraverseBTree(PBTNODE PT) { if(NULL != PT) { if(NULL != PT->pLchild) { PostTraverseBTree(PT->pLchild); } if(NULL != PT->pRchild) { PostTraverseBTree(PT->pRchild); } printf("%c\n",PT->data); } }