哪位大大帮我看看这个关于树的操作有没有问题,感激不尽!
程序代码:
#include<stdio.h> #include<stdlib.h> #define TElemType char typedef struct BiTNode{ TElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; void CreateBiTree(BiTree *T) { TElemType ch; printf("Please enter the tree:"); scanf("%c",&ch); if(ch==' ') *T=NULL; else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(-1); (*T)->data=ch; CreateBiTree((*T)->lchild); CreateBiTree((*T)->rchild); } } void preordertraverse(BiTree T) { if(T) { printf("%c",T->data); preordertraverse(T->lchild); preordertraverse(T->rchild); } } void inordertraverse(BiTree T) { if(T) { inordertraverse(T->lchild); printf("%c",T->data); inordertraverse(T->rchild); } } void postordertraverse(BiTree T) { if(T) { postordertraverse(T->lchild); postordertraverse(T->rchild); printf("%c",T->data); } } void searchparent(BiTree T,TElemType e) { if(T) { if(T->lchild->data==e||T->rchild->data==e) { printf("%c",T->data); return;} searchparent(T->lchild,e); searchparent(T->rchild,e); } } main() { BiTree T; TElemType e; CreateBiTree(&T); preordertraverse(T); inordertraverse(T); postordertraverse(T); searchparent(T,e); }