实验要求
实验源代码
#include <stdio
.h
>
#include <malloc
.h
>
typedef struct TREE{
int
data;
struct TREE
*left;
struct TREE
*right;
}Tree,
*linkTree;
linkTree creatTree(int
data);
void inorderTree(linkTree tree);
void preorderTree(linkTree tree);
void postorderTree(linkTree tree);
int main(){
linkTree head
= creatTree(
1);
head
->left
= creatTree(
2);
head
->right
= creatTree(
3);
head
->right
->left
= creatTree(
4);
head
->right
->left
->left
= creatTree(
6);
head
->right
->right
= creatTree(
5);
preorderTree(head);
printf(
"\n");
postorderTree(head);
printf(
"\n");
inorderTree(head);
printf(
"\n");
return 0;
}
linkTree creatTree(int
data){
linkTree tree
= (linkTree)malloc(sizeof(Tree));
tree
->data = data;
tree
->left
= NULL;
tree
->right
= NULL;
return tree;
}
void preorderTree(linkTree tree){
if(tree
!= NULL){
printf(
"%d\t",tree
->data);
preorderTree(tree
->left);
preorderTree(tree
->right);
}
}
void postorderTree(linkTree tree){
if(tree
!= NULL){
postorderTree(tree
->left);
postorderTree(tree
->right);
printf(
"%d\t",tree
->data);
}
}
void inorderTree(linkTree tree){
if(tree
!= NULL){
inorderTree(tree
->left);
printf(
"%d\t",tree
->data);
inorderTree(tree
->right);
}
}
实验结果显示