/*
.二叉树的遍历
前序遍历
根左右
中序遍历
左根右
后序遍历
左右根
*/
#include<stdio.h>
#include<stdlib.h>
//描述一个体
typedef struct treeNode
{
char data;//数据域字符表示
struct treeNode*LChild;
struct treeNode*RChild;
}TREE,*LPTREE;
//创建节点
LPTREE createNode(char data)
{
LPTREE newNode = (LPTREE)malloc(sizeof(TREE));
newNode->data = data;
newNode->LChild = NULL;
newNode->RChild = NULL;
return newNode;
}
void insertNode(LPTREE parentNode, LPTREE LChild, LPTREE RChild)
{
parentNode->LChild = LChild;
parentNode->RChild = RChild;
}
void printCurNodeData(LPTREE curData)
{
printf("%c\t",curData->data);
}
//递归法
//先序
void preOrder(LPTREE root)
{
if (root != NULL)
{
printCurNodeData(root);
preOrder(root->LChild);
preOrder(root->RChild);
}
}
void midOrder(LPTREE root)
{
if (root != NULL)
{
midOrder(root->LChild);
printCurNodeData(root);
midOrder(root->RChild);
}
}
void lastOrder(LPTREE root)
{
if (root != NULL)
{
lastOrder(root->LChild);
lastOrder(root->RChild);
printCurNodeData(root);
}
}
int main()
{
LPTREE A = createNode('A');
LPTREE B = createNode('B');
LPTREE C = createNode('C');
LPTREE D = createNode('D');
LPTREE E = createNode('E');
LPTREE F = createNode('F');
LPTREE G = createNode('G');
insertNode(A, B, C);
insertNode(B, D, E);
insertNode(C, F, G);
printf("先序遍历:\n");
preOrder(A);
printf("\n");
printf("\n中序遍历:\n");
midOrder(A);
printf("\n");
printf("\n后序遍历:\n");
lastOrder(A)
printf("\n");
system("pause");
return 0;
}