#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
void Preorder(BiTree T);
void Inorder(BiTree T);
void Postorder(BiTree T);
BiTree Create();
void Preorder(BiTree T)
{
if (T)
{
printf("%c ", T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
else return;
}
void Inorder(BiTree T)
{
if (T)
{
Inorder(T->lchild);
printf("%c ", T->data);
Inorder(T->rchild);
}
else return;
}
void Postorder(BiTree T)
{
if (T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c ", T->data);
}
else return;
}
int main()
{
BiTree T = Create();
printf("Preorder:"); Preorder(T); printf("\n");
printf("Inorder:"); Inorder(T); printf("\n");
printf("Postorder:"); Postorder(T); printf("\n");
return 0;
}
BiTree Create()
{
ElemType ch;
BiTree T;
scanf("%c", &ch);
if (ch == '#') return NULL;
T = (BiTree)malloc(sizeof(struct BiTNode));
T->data = ch;
T->lchild = Create();
T->rchild = Create();
return T;
}
二叉树的先序遍历、中序遍历和后后序遍历
最新推荐文章于 2021-11-18 20:03:09 发布