如何生成一个二叉树,求解决
程序代码:
别的函数没有错,creat_tree这个函数错了,不知道怎么用递归的形式生成一个二叉树,这个函数为错在哪里? #include<stdio.h> #include<stdlib.h> #include<malloc.h> #define N 30 typedef struct tree { int date; struct tree *lchild; struct tree *rchild; }tree,*T;//定义树的数据结构; typedef struct stack { tree **base; int top; int size; int incresize; }stack,*S;//定义一个栈; void init_stack(S s) { s->size=N; s->base=(tree**)malloc(s->size*sizeof(tree*)); s->top=0; }//初始化一个栈 void push_stack(S s,T e) { tree **newbase; if(s->top==s->size-1) { newbase=(T*)realloc(s->base,(s->incresize+s->size)*sizeof(tree*)); if(newbase==NULL) printf("没能申请到空间\n"); else { s->base=newbase; s->top=s->size-1; s->size=s->size+s->incresize; } } else s->base[s->top++]=e; }//进栈 T pop_stack(S s) { T e; if(s->top==0) { printf(" the stack is empty\n"); return 0; } else e=s->base[--s->top]; return e; }//出栈 int empty_stack(S s) { if(s->top==0) return 1; else return 0; } //判断栈是否为空 void pre_traverse_d(T t) { int e; if(t!=NULL) { e=t->date; printf(" %d ",e); pre_traverse_d(t->lchild); pre_traverse_d(t->rchild); } }//先序遍历的递归形式 void pre_traverse_s1(T t,S s) { int e; T p; p=t; init_stack(s); while(p||!empty_stack(s)) { if(p) { e=p->date; printf(" %d ",e); push_stack(s,p); p=p->lchild; } else { p=pop_stack(s); p=p->rchild; } } return ; }//先序遍历的堆栈形式 void pre_traverse_s2(T t,S s) { T p; p=t; init_stack(s); while(p||!empty_stack(s)) { if(p) { push_stack(s,t); p=p->lchild; } else { p=pop_stack(s); printf(" %d ",p->date); p=p->rchild; } } }//中序遍历的堆栈形式 void creat_tree(T t) { int e;char c; printf("输入二叉树节点控制量\n"); scanf("%c",&c); if(c=='\0') t=NULL; else { printf("输入二叉树节点数据\n"); scanf("%d",&e); t->date=e; t->lchild=(T)malloc(sizeof(tree)); creat_tree(t->lchild); t->rchild=(T)malloc(sizeof(tree)); creat_tree(t->rchild); } } void main() { tree t;stack s; creat_tree(&t); pre_traverse_s2(&t,&s); }