#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node
{
struct node * lchild;
struct node * rchild;
int data;
}node;
typedef struct node * BTREE;
/*BTREE CreateBT(int v,BTREE ltree,BTREE rtree)
{
BTREE root;
root=(BTREE)malloc(sizeof(node));
root->data=v;
root->lchild=ltree;
root->rchild=rtree;
return root;
}*/
void CreateBT(BTREE &T)//创建二叉树
{
char ch;
scanf("%c",&ch);
if(ch=='#')
{
T=NULL;
}
else
{
T=(BTREE)malloc(sizeof(node));
if(T==NULL)
{
return;
}
T->data=ch;
CreateBT(T->lchild);
CreateBT(T->rchild);
}
return;
}
void PreOrder(BTREE T)//先序遍历
{
if(T==NULL)
{
return;
}
printf("%c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
void InOrder(BTREE T)//中序遍历
{
if(T==NULL)
{
return;
}
InOrder(T->lchild);
printf("%c",T->data);
InOrder(T->rchild);
}
void PostOrder(BTREE T)//后序遍历
{
if(T==NULL)
{
return;
}
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c",T->data);
}
int main()
{
BTREE A;
printf("按先序序列输入结点序列,'#'代表空:");
CreateBT(A);
printf("先序遍历节点的结果为:");
PreOrder(A);
printf("中序遍历节点的结果为:");
InOrder(A);
printf("后序遍历节点的结果为:");
PostOrder(A);
return 0;
}
二叉树的递归遍历
最新推荐文章于 2023-03-24 12:21:29 发布
3958

被折叠的 条评论
为什么被折叠?



