递归二叉树问题
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define OK 1#define ERROR -1typedef int Status;typedef char TElemType;typedef struct BiTNode{ TElemType data; struct BiTNode * lchild, * rchild;}BiTNode, * BiTree;//------------------------------------------BiTree CreateBiTree( BiTree &pt ); //此处生成树传回的是指针(指针变量引用)//------------------------------------------int main(){ BiTree T; int choice, status; puts( "welcome!" ); puts( "No1 CreateBiTree" ); printf( "please enter your choice:" ); scanf( "%d", &choice ); switch( choice ) { case 1: printf( "please input some chars:" ); CreateBiTree( T ); break; default: break; } return 0;}BiTree CreateBiTree( BiTree &pt ){ char ch; if( (ch = getchar()) == '@' ) pt = NULL; else { pt = (BiTree)malloc( sizeof(BiTNode) ); if( !pt ) exit(0); pt->data = ch; printf( "%c\n", pt->data ); pt->lchild = CreateBiTree( pt->lchild ); pt->rchild = CreateBiTree( pt->rchild ); } return pt;}
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;}