数据结构 二叉树遍历 求改
#include "stdio.h"#include "conio.h"
typedefchar 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()
{
charch;
scanf("%c",&ch);
BitreeNode* T=NULL;
if(ch!='#')
{
T=(structBitreeNode*)(malloc(sizeof(structBitreeNode)));
T->data=ch;
T->lchild=CreateBitree();
T->rchild=CreateBitree();
}
returnT;
}
void Preorder(structBitreeNode *p)
{
if(p!=NULL)
{
printf("%c",p->data);
Preorder(p->lchild);
Preorder(p->rchild);
}
}
void Postorder(structBitreeNode *p)
{
if(p!=NULL)
{
Postorder(p->lchild);
Postorder(p->rchild);
printf("%c",p->data);
}
}
void Inorder2(BitreeNode*T)
{
BitreeNode *p;
BitreeNode * stack;
int top=0;
stack=T;
while (top>0)
{
p= stack;
while (p)
{
stack= p->lchild;
p=stack;
}
p= stack[--top];
if (top>0 )
{
p=stack[--top];
cout<<p->data;
stack= p->rchild;
}
}
}
void DeleteTree(structBitreeNode *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);
}
必须的顶{:5_106:} 路过学习 没有注释:cry哦
页:
[1]