主题:先序遍历,请教!
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX_TREE_SIZE 100
#define STACK_INIT_SIZE 20
#define OVERFLOW -1
#define ElemType int
#define Status int
#define OK 1
typedef ElemType SqBiTree[MAX_TREE_SIZE];
SqBiTree bt;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*Bitree;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
(*S).base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!(*S).base)exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
Status CreatBiTree(Bitree &t){
ElemType ch,temp;
printf("输入一个元素: ");
scanf("%c",&ch);
temp=getchar();
if(ch==' ') t=NULL;
else{
if(!(t=(Bitree)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
t->data=ch;
CreatBiTree(t->lchild);
CreatBiTree(t->rchild);
}
return OK;
}
void precender(Bitree t)
{
SqStack *S=NULL;
InitStack(S);
int *top=0;
while(t||top)
{
while(t)
{
printf("%d\n",t->data);
top++;
S(top)=t->data;
t=t->lchild;
}
if(top)
{
t=S(top)->rchild;
top--;
}
}
}
void main()
{
Bitree t;
printf("输出先序:");
}
有几个地方没想同,请高手们看看!指导指导!!
#include<stdlib.h>
#include<malloc.h>
#define MAX_TREE_SIZE 100
#define STACK_INIT_SIZE 20
#define OVERFLOW -1
#define ElemType int
#define Status int
#define OK 1
typedef ElemType SqBiTree[MAX_TREE_SIZE];
SqBiTree bt;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*Bitree;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
(*S).base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!(*S).base)exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
Status CreatBiTree(Bitree &t){
ElemType ch,temp;
printf("输入一个元素: ");
scanf("%c",&ch);
temp=getchar();
if(ch==' ') t=NULL;
else{
if(!(t=(Bitree)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
t->data=ch;
CreatBiTree(t->lchild);
CreatBiTree(t->rchild);
}
return OK;
}
void precender(Bitree t)
{
SqStack *S=NULL;
InitStack(S);
int *top=0;
while(t||top)
{
while(t)
{
printf("%d\n",t->data);
top++;
S(top)=t->data;
t=t->lchild;
}
if(top)
{
t=S(top)->rchild;
top--;
}
}
}
void main()
{
Bitree t;
printf("输出先序:");
}
有几个地方没想同,请高手们看看!指导指导!!