求指导,二叉树创建和遍历那里错了呢。
程序代码:
#include <malloc.h> #include <stdio.h> #include <stdlib.h> #include <string> #define OVERFLOW -1 #define ERROR 0 #define OK 1 #define STACK_INIT_SIZE 100; #define STACKINCREMENT 10; typedef int Status; typedef char TElemType; typedef struct BiTNode{ char data; struct BiTNode *lchild;//左右孩子指针 struct BiTNode *rchild; }BiTNode,*BiTree; typedef struct{ char *base; char *top; int stacksize; }SqStack; int InitStack(SqStack &S); int PopStack(SqStack &S,char e); int PushStack(SqStack &S,char e); int StackEmpty(SqStack S); void Postorder(BiTNode *p); int StackEmpty(SqStack S); int CreateBiTree(BiTree &T) //按先序次序输入二叉树中结点的值,空格字符表示空树 //构造二叉链表表示的二叉树T { char ch; printf("请输入元素:\n"); scanf("%c",&ch); if(ch==' ')T=NULL; else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))exit(OVERFLOW); T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return OK; }//CreateBiTree void Postorder( BiTNode *p) { if (p!=NULL) {Postorder( p->lchild ); Postorder(p->rchild ) ; printf("%c",p->data); } }//Postorder Status InitStack(SqStack &S) {S.base=(int *)malloc(STACK_INIT_SIZE *sizeof(char)); if(!S.base) exit(OVERFLOW); S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } //InitStack int InOrderTraverse(BiTree T,Status(*Visit)(char e)){ BiTree p; SqStack S; if(T){ {InitStack(S); p=T; while (p||!StackEmpty(S)){ if(p){PushStack(S,p->data);p=p->lchild;} else{ PopStack(S,p->data);if(!Visit(p->data))return ERROR; p=p->rchild; }//else }//while return OK; }//InOrderTraverse int StackEmpty(SqStack S) {if(S.top==S.base) return TRUE; else return FALSE; }//StackEmpty int PushStack(SqStack *S,char e) { if(S->top - S->base>=S->stacksize) { S->base=(SElemType *) realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); if(!S->base)exit(OVERFLOW); S->top=S->base+S->stacksize; S->stacksize += STACKINCREMENT; }//PushStack *(S->top++)=e; return OK; } int PopStack(SqStack *S,SElemType *e) { if(S->top==S->base) return ERROR; *e=*--S->top; return OK; }//PopStack int main() {SqStack S; InitStack(S); char e; struct BiTNode *T=0; T=CreateBiTree(); printf("中序遍历:\n"); Postorder(T); }