二叉树的创建及其遍历(递归+非递归)
编译的时候总是出现 [Error] ld returned 1 exit status 这个错误
不知道怎么回事,求解!!!!!!
#define _CRT_SECURE_NO_WARNINGS
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int Status;
typedef int Stack;
typedef char ElemType;
typedef struct BiNode
{
ElemType data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
Status InitStack(Stack *S);
Status Push(Stack *S, BiTree p);
Status Pop(Stack *S, BiTree *p);
Status GetTop(Stack *S, BiTree *p);
Status StackEmpty(Stack *S);
Status CreatBiTree(BiTree *T);
Status PreOrderTraverse_Recursive(BiTree T, Status(*Visit)(ElemType e));
Status InOrderTraverse_Recursive(BiTree T, Status(*Visit)(ElemType e));
Status PostOrderTraverse_Recursive(BiTree T, Status(*Visit)(ElemType e));
Status PreOrderTraverse_NonRecursive(BiTree T, Status(*Visit)(ElemType e));
Status InOrderTraverse_NonRecursive(BiTree T, Status(*Visit)(ElemType e));
Status InOrderTraverse_NonRecursive_2(BiTree T, Status(*Visit)(ElemType e));
Status PostOrderTraverse_NonRecursive(BiTree T, Status(*Visit)(ElemType e));
Status PrintElement(ElemType e);
Status CreatBiTree(BiTree *T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')
{
(*T)=NULL;
}
else {
if(!((*T)=(BiTree)malloc(sizeof(BiNode))))
exit(OVERFLOW);
(*T)->data=ch;
CreatBiTree(&((*T)->lchild));
CreatBiTree(&((*T)->rchild));
return OK;
}
}
Status PreOrderTraverse_Recursive(BiTree T,Status(*Visit)(ElemType e))
{
if(T)
{
if(Visit(T->data))
if(PreOrderTraverse_Recursive(T->lchild,Visit))
if(PreOrderTraverse_Recursive(T->rchild,Visit))
return OK;
return ERROR;
}
else
{
return OK;
}
}
Status PreOrderTraverse_NonRecursive(BiTree T,Status(*Visit)(ElemType e))
{
Stack *S;
BiTree p;
S=(Stack*)malloc(sizeof(Stack));
InitStack(S);
Push(S,T);
while(!StackEmpty(S))
{
if(GetTop(S,&p)&&p)
{
if(!Visit(p->data))
return ERROR;
Push(S,p->lchild);
}
else
{
Pop(S,&p);
if(!StackEmpty(S))
{
Pop(S,&p);
Push(S,p->lchild);
}
}
}
return OK;
}
Status InOrderTraverse_Recursive(BiTree T,Status(*Visit)(ElemType e))
{
if(T)
{
if(InOrderTraverse_Recursive(T->lchild,Visit))
if(Visit(T->data))
if(InOrderTraverse_Recursive(T->rchild,Visit))
return OK;
return ERROR;
}
else
{
return OK;
}
}
Status InOrderTraverse_NonRecursive(BiTree T,Status(*Visit)(ElemType e))
{
Stack *S;
BiTree p;
S=(Stack*)malloc(sizeof(Stack));
InitStack(S);
Push(S,T);
while(!StackEmpty(S))
{
if(GetTop(S,&p)&&p)
{
Push(S,p->lchild);
}
else
{
Pop(S,&p);
if(!StackEmpty(S))
{
Pop(S,&p);
if(Visit(p->data))
return ERROR;
Push(S,p->rchild);
}
}
}
return OK;
}
Status InOrderTraverse_NonRecursive_2(BiTree T,Status(*Visit)(ElemType e))
{
Stack *S;
BiTree p;
S=(Stack*)malloc(sizeof(Stack));
InitStack(S);
while(p||!StackEmpty(S))
{
if(p)
{
Push(S,p);
p=p->lchild;
}
else
{
Pop(S,&p);
if(!Visit(p->data))
return ERROR;
p=p->rchild;
}
}
return OK;
}
Status PostOrderTraverse_Recursive(BiTree T,Status(*Visit)(ElemType e))
{
if(T)
{
if(PostOrderTraverse_Recursive(T->lchild,Visit))
if(PostOrderTraverse_Recursive(T->rchild,Visit))
if(Visit(T->data))
return OK;
return ERROR;
}
else
{
return OK;
}
}
Status PostOrderTraverse_NonRecursive(BiTree T,Status(*Visit)(ElemType e))
{
Stack *S;
BiTree p,pre=NULL;
S=(Stack*)malloc(sizeof(Stack));
InitStack(S);
Push(S,T);
while(!StackEmpty(S))
{
if(GetTop(S,&p)&&p->lchild&&pre!=p->lchild&&!(p->lchild&&pre==p->rchild))
{
Push(S,p->lchild);
}
else
{
if(p->rchild&&pre!=p->rchild)
Push(S,p->rchild);
else
{
Pop(S,&p);
if(!StackEmpty(S))
{
if(!Visit(p->data))
return ERROR;
}
}
}
}
return OK;
}
Status PrintElement(ElemType e)
{
putchar(e);
return OK;
}
int main()
{
BiTree T;
CreatBiTree(&T);
putchar('\n');
printf("递归先序为:\n");
PreOrderTraverse_Recursive(T,PrintElement);
putchar('\n');
putchar('\n');
printf("非递归先序为:\n");
PreOrderTraverse_NonRecursive(T,PrintElement);
putchar('\n');
putchar('\n');
printf("递归中序为:\n");
InOrderTraverse_Recursive(T,PrintElement);
putchar('\n');
putchar('\n');
printf("非递归中序为:\n");
InOrderTraverse_NonRecursive(T,PrintElement);
putchar('\n');
putchar('\n');
printf("非递归中序(2)为:\n");
InOrderTraverse_NonRecursive_2(T,PrintElement);
putchar('\n');
putchar('\n');
printf("递归后序为:\n");
PostOrderTraverse_Recursive(T,PrintElement);
putchar('\n');
putchar('\n');
printf("非递归后序为:\n");
PostOrderTraverse_NonRecursive(T,PrintElement);
return 0;
}