[此贴子已经被作者于2006-10-31 22:18:45编辑过]
多也得看哦 慢慢积累就好了 草草的写了一个 蛮简单的 自己看看吧
#include<stdio.h>
#include<malloc.h>
#include<string.h>typedef struct Node//二叉树结构
{
char name[15];
int age;
char job[15];
struct Node *left;
struct Node *right;
}*BiTree;BiTree Create() {//建立二叉树(先序递归)
BiTree T;
printf(\"please intput data:\n\");
printf(\"name:\");
char name[15];
scanf(\"%s\", &name);
printf(\"age:\");
int age;
scanf(\"%d\", &age);
printf(\"job:\");
char job[15];
scanf(\"%s\", job);
getchar();
if(age==0) T=NULL;//当年龄为0时,次节点为空
else {
if(!(T= (BiTree)malloc(sizeof(struct Node)))) printf(\"OverFlow\n\");//建立节点
strcpy(T->name,name);
T->age=age;
strcpy(T->job,job);
T->left=Create();
T->right=Create();
}
return T;
}void preorder(BiTree T)//先序遍历
{
if(T!=NULL)
{
printf(\"%s\n%d\n%s\n\n\",T->name,T->age,T->job);
preorder(T->left);
preorder(T->right);
}
}void inorder(BiTree T)//中序遍历
{
if(T!=NULL)
{
printf(\"%s\n%d\n%s\n\n\",T->name,T->age,T->job);
inorder(T->left);
inorder(T->right);
}
}void postorder(BiTree T)//后序遍历
{
if(T!=NULL)
{
printf(\"%s\n%d\n%s\n\n\",T->name,T->age,T->job);
postorder(T->left);
postorder(T->right);
}
}
void main()
{
BiTree T=Create();
printf(\"先序遍历:\n\");preorder(T);
printf(\"中序遍历\n\");inorder(T);
printf(\"后序遍历\n\");postorder(T);
}