|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include "stdio.h"
#include "conio.h"
typedef char DataType;
typedef struct BitreeNode
{
DataType data;
struct BitreeNode *lchild;
struct BitreeNode *rchild;
};
void PreOrder(Bitree root)
{
if(root != NULL)
{
Visit( root-> data );
PreOrder( root -> LChild );
PreOrder( root -> RChild );
}
}
void InOrder(Bitree root)
{
if(root != NULL)
{
InOrder( root -> LChild );
Visit( root-> data );
InOrder( root -> RChild );
}
}
void PostOrder(Bitree root)
{
if(root != NULL)
{
PostOrder( root -> LChild );
PostOrder( root -> RChild );
Visit( root-> data );
}
}
BitreeNode *CreateBitree()
{
char ch;
scanf("%c",&ch);
BitreeNode* T=NULL;
if(ch!='#')
{
T= (struct BitreeNode*)(malloc(sizeof(struct BitreeNode)));
T->data=ch;
T->lchild=CreateBitree();
T->rchild=CreateBitree();
}
return T;
}
void Preorder(struct BitreeNode *p)
{
if (p!=NULL)
{
printf("%c",p->data);
Preorder( p->lchild );
Preorder(p->rchild );
}
}
void Postorder(struct BitreeNode *p)
{
if (p!=NULL)
{
Postorder( p->lchild );
Postorder(p->rchild );
printf("%c",p->data);
}
}
void Inorder2(BitreeNode *T)
{
BitreeNode *p;
BitreeNode * stack[100];
int top=0;
stack[top++]=T;
while (top>0)
{
p= stack[top-1];
while (p)
{
stack[top++]= p->lchild;
p= stack[top-1];
}
p= stack[--top];
if (top>0 )
{
p=stack[--top];
cout<<p->data;
stack[top++]= p->rchild;
}
}
}
void DeleteTree(struct BitreeNode *p)
{
if (p!=NULL)
{
DeleteTree( p->lchild );
DeleteTree(p->rchild );
free(p);
}
}
void main()
{
struct BitreeNode *T=0;
T=CreateBitree();
printf("\nPreorder:\n");
Preorder(T)
printf("\nPostorder:\n");
Postorder(T);
printf("\nInorder2:\n");
Inorder2(T);
printf("\n");
DeleteTree(T);
}
|
|