先序遍历(根 左 右) 中序遍历(左 根 右) 后续遍历(左 右 根) printf("%c",root->ch) recursion(root->lChild) recursion(root->lChild)
recursion(root->lChild) pfrintf("%c",root->ch) recursion(root->rChild)
recursion(root->rChild) recursion(root->rChild) pfrintf("%c",root->ch)
#include<stdio.h>
#include<string>
#include<stdlib.h>
struct BinaryNode
{
char ch;//数据域
struct BinaryNode* lChild;//左孩子节点
struct BinaryNode* rChild;//右孩子节点
};
//递归遍历函数
void recursion(struct BinaryNode*root)
{
if(NULL== root)
{
return;
}
//先序遍历 先根 再左 在右
printf("%c",root->ch);
recursion(root->lChild);
recursion(root->rChild);
}
void tset()
{
struct BinaryNode nodeA={'A',NULL,NULL};
struct BinaryNode nodeB={'B',NULL,NULL};
struct BinaryNode nodeC={'C',NULL,NULL};
struct BinaryNode nodeD={'D',NULL,NULL};
struct BinaryNode nodeE={'E',NULL,NULL};
struct BinaryNode nodeF={'F',NULL,NULL};
struct BinaryNode nodeG={'G',NULL,NULL};
struct BinaryNode nodeH={'H',NULL,NULL};
}
//建立节点关系
nodeA.lChild=&nodeB;
nodeA.rChild=&nodeF;
nodeB.rChild=&nodeC;
nodeC.rChild=&nodeE;
nodeC.lChild=&nodeD;
nodeF.rChild=&nodeG;
nodeG.lChild=&nodeH;
//递归遍历
recursion(nodeA);
int main()
{
tset()
systrm("puase");
return EXIT_SUCCESS;
}