二叉树的创建和遍历

来源:互联网 发布:新疆商务厅 数据分析 编辑:程序博客网 时间:2024/05/21 19:21
#include<stdio.h>
#include<stdlib.h>
typedef  struct NODE
{
char data;
struct NODE *Lchild;
struct NODE *Rchild;
}BiTree;
BiTree *ChangeTree(BiTree *root)
{
char ch;
ch=getchar();
if(ch=='#')
root=NULL;
else
{
root=(BiTree *)malloc(sizeof(BiTree));
root->data=ch;
root->Lchild=ChangeTree(root->Lchild);
root->Rchild=ChangeTree(root->Rchild);
}
return root;
}


void PreOder(BiTree *root)
{
if(root)
{
printf("%c",root->data);
PreOder(root->Lchild);
PreOder(root->Rchild);
}
}
void InOder(BiTree *root)
{

if(root)
{
InOder(root->Lchild);
printf("%c",root->data);
InOder(root->Rchild);
}
}
void PostOder(BiTree *root)
{
if(root)
{
PostOder(root->Lchild);
PostOder(root->Rchild);
printf("%c",root->data);
}
}
int main()
 {
BiTree *root;
root=ChangeTree(root);
PreOder(root);
printf("\n");
InOder(root);
printf("\n");
PostOder(root);
printf("\n");
}
0 0
原创粉丝点击