#include<stdio.h>
#include <stdlib.h>
typedef struct _Tree
{
int nValue;
struct _Tree* pLeft;
struct _Tree* pRight;
}Tree;
void CreateTree(Tree* *tree)
{
(*tree) = (Tree*)malloc(sizeof(Tree));
(*tree)->nValue = 1;
(*tree)->pLeft = (Tree*)malloc(sizeof(Tree));
(*tree)->pLeft->nValue = 2;
(*tree)->pLeft->pLeft = (Tree*)malloc(sizeof(Tree));
(*tree)->pLeft->pLeft->nValue = 4;
(*tree)->pLeft->pLeft->pLeft = NULL;
(*tree)->pLeft->pLeft->pRight = NULL;
(*tree)->pLeft->pRight = (Tree*)malloc(sizeof(Tree));
(*tree)->pLeft->pRight->nValue = 5;
(*tree)->pLeft->pRight->pLeft = NULL;
(*tree)->pLeft->pRight->pRight = NULL;
(*tree)->pRight = (Tree*)malloc(sizeof(Tree));
(*tree)->pRight->nValue = 3;
(*tree)->pRight->pLeft = (Tree*)malloc(sizeof(Tree));
(*tree)->pRight->pLeft->nValue = 6;
(*tree)->pRight->pLeft->pLeft = NULL;
(*tree)->pRight->pLeft->pRight = NULL;
(*tree)->pRight->pRight = (Tree*)malloc(sizeof(Tree));
(*tree)->pRight->pRight->nValue = 7;
(*tree)->pRight->pRight->pLeft = NULL;
(*tree)->pRight->pRight->pRight = NULL;
}
void FrontPrint(Tree* tree)
{
if (tree == NULL)
{
return;
}
// 先访问根节点
printf("%d ",tree->nValue);
// 遍历左子树
FrontPrint(tree->pLeft);
// 遍历右子树
FrontPrint(tree->pRight);
}
void MidPrint(Tree* tree)
{
if (tree == NULL)
{
return;
}
// 遍历左子树
MidPrint(tree->pLeft);
// 先访问根节点
printf("%d ",tree->nValue);
// 遍历右子树
MidPrint(tree->pRight);
}
void EndPrint(Tree* tree)
{
if (tree == NULL)
{
return;
}
// 遍历左子树
EndPrint(tree->pLeft);
// 遍历右子树
EndPrint(tree->pRight);
// 先访问根节点
printf("%d ",tree->nValue);
}
int main()
{
Tree* root = NULL;
CreateTree(&root);
FrontPrint(root);
printf("\n");
MidPrint(root);
printf("\n");
EndPrint(root);
printf("\n");
system("pause");
return 0;
}
#include <stdlib.h>
typedef struct _Tree
{
int nValue;
struct _Tree* pLeft;
struct _Tree* pRight;
}Tree;
void CreateTree(Tree* *tree)
{
(*tree) = (Tree*)malloc(sizeof(Tree));
(*tree)->nValue = 1;
(*tree)->pLeft = (Tree*)malloc(sizeof(Tree));
(*tree)->pLeft->nValue = 2;
(*tree)->pLeft->pLeft = (Tree*)malloc(sizeof(Tree));
(*tree)->pLeft->pLeft->nValue = 4;
(*tree)->pLeft->pLeft->pLeft = NULL;
(*tree)->pLeft->pLeft->pRight = NULL;
(*tree)->pLeft->pRight = (Tree*)malloc(sizeof(Tree));
(*tree)->pLeft->pRight->nValue = 5;
(*tree)->pLeft->pRight->pLeft = NULL;
(*tree)->pLeft->pRight->pRight = NULL;
(*tree)->pRight = (Tree*)malloc(sizeof(Tree));
(*tree)->pRight->nValue = 3;
(*tree)->pRight->pLeft = (Tree*)malloc(sizeof(Tree));
(*tree)->pRight->pLeft->nValue = 6;
(*tree)->pRight->pLeft->pLeft = NULL;
(*tree)->pRight->pLeft->pRight = NULL;
(*tree)->pRight->pRight = (Tree*)malloc(sizeof(Tree));
(*tree)->pRight->pRight->nValue = 7;
(*tree)->pRight->pRight->pLeft = NULL;
(*tree)->pRight->pRight->pRight = NULL;
}
void FrontPrint(Tree* tree)
{
if (tree == NULL)
{
return;
}
// 先访问根节点
printf("%d ",tree->nValue);
// 遍历左子树
FrontPrint(tree->pLeft);
// 遍历右子树
FrontPrint(tree->pRight);
}
void MidPrint(Tree* tree)
{
if (tree == NULL)
{
return;
}
// 遍历左子树
MidPrint(tree->pLeft);
// 先访问根节点
printf("%d ",tree->nValue);
// 遍历右子树
MidPrint(tree->pRight);
}
void EndPrint(Tree* tree)
{
if (tree == NULL)
{
return;
}
// 遍历左子树
EndPrint(tree->pLeft);
// 遍历右子树
EndPrint(tree->pRight);
// 先访问根节点
printf("%d ",tree->nValue);
}
int main()
{
Tree* root = NULL;
CreateTree(&root);
FrontPrint(root);
printf("\n");
MidPrint(root);
printf("\n");
EndPrint(root);
printf("\n");
system("pause");
return 0;
}