一.二叉树遍历
转载请附上链接:https://blog.csdn.net/qq_37978862/article/details/104661419
#include<stdio.h>
typedef char data_t;
typedef struct node
{
data_t data;
struct node* lchild,*rchild;
}node_t;
//前序遍历
void pre_printf(node_t *root)
{
if(root == NULL)
return;
printf("%c\n",root->data);
pre_printf(root->lchild);
pre_printf(root->rchild);
}
//中序遍历
void mid_printf(node_t *root)
{
if(root == NULL)
return ;
mid_printf(root->lchild);
printf("%c\n",root->data);
mid_printf(root->rchild);
}
//后序遍历
void beh_printf(node_t *root)
{
if(root == NULL)
return;
beh_printf(root->lchild);
beh_printf(root->rchild);
printf("%c\n",root->data);
}
int main()
{
node_t a = {'a',NULL,NULL};
node_t b = {'b',NULL,NULL};
node_t c = {'c',NULL,NULL};
node_t d = {'d',NULL,NULL};
node_t f = {'f',NULL,NULL};
node_t e = {'e',NULL,NULL};
a.lchild = &b;
b.lchild = &c;
c.rchild = &e;
e.lchild = &f;
e.rchild = &d;
pre_printf(&a);
printf("*****************\n");
mid_printf(&a);
printf("*****************\n");
beh_printf(&a);
}