#include<stdio.h>
#include<malloc.h>
typedef struct node//二叉树界定类型的定义
{
char data;
struct node *lchild;//定义节点的左孩子指针
struct node *rchild;//定义节点的右孩子指针
}BinTNode;
BinTNode *CreateBinTree()//输入二叉树的线序遍历序列,创建二叉链表
{
BinTNode *t;
char ch;
ch=getchar();
if(ch=='0')
t=NULL;
else
{
t=(BinTNode *)malloc(sizeof(BinTNode));//申请根节点*t空间
t->data=ch;
t->lchild=CreateBinTree();
t->rchild=CreateBinTree();
}
return t;
}
void ListBinTree(BinTNode *t)//用广义表表示二叉树
{
if(t!=NULL)
{
printf("%c",t->data);
if(t->lchild!=NULL||t->rchild!=NULL)
{
printf("(");
ListBinTree(t->lchild);
if(t->rchild!=NULL)
printf(",");
ListBinTree(t->rchild);
printf(")");
}
}
}
void preorder(BinTNode *t)//对二叉树进行先序遍历
{
if(t!=NULL)
{
printf("%3c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(BinTNode *t)//对二叉树进行中序遍历
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%3c",t->data);
inorder(t->rchild);
}
}
void postorder(BinTNode *t)
{
if(t!=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%3c",t->data);
}
}
void main()
{
BinTNode *t=NULL;
printf("请输入先序序列:\n");
t=CreateBinTree();
printf("广义表表示的二叉树的输出:\n");
ListBinTree(t);
printf("\n二叉树的前序遍历结果为:\n");
preorder(t);
printf("\n二叉树的中序遍历结果为:\n");
inorder(t);
printf("\n二叉树的后序便利结果为:\n");
postorder(t);
printf("\n");
}
#include<malloc.h>
typedef struct node//二叉树界定类型的定义
{
char data;
struct node *lchild;//定义节点的左孩子指针
struct node *rchild;//定义节点的右孩子指针
}BinTNode;
BinTNode *CreateBinTree()//输入二叉树的线序遍历序列,创建二叉链表
{
BinTNode *t;
char ch;
ch=getchar();
if(ch=='0')
t=NULL;
else
{
t=(BinTNode *)malloc(sizeof(BinTNode));//申请根节点*t空间
t->data=ch;
t->lchild=CreateBinTree();
t->rchild=CreateBinTree();
}
return t;
}
void ListBinTree(BinTNode *t)//用广义表表示二叉树
{
if(t!=NULL)
{
printf("%c",t->data);
if(t->lchild!=NULL||t->rchild!=NULL)
{
printf("(");
ListBinTree(t->lchild);
if(t->rchild!=NULL)
printf(",");
ListBinTree(t->rchild);
printf(")");
}
}
}
void preorder(BinTNode *t)//对二叉树进行先序遍历
{
if(t!=NULL)
{
printf("%3c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(BinTNode *t)//对二叉树进行中序遍历
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%3c",t->data);
inorder(t->rchild);
}
}
void postorder(BinTNode *t)
{
if(t!=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%3c",t->data);
}
}
void main()
{
BinTNode *t=NULL;
printf("请输入先序序列:\n");
t=CreateBinTree();
printf("广义表表示的二叉树的输出:\n");
ListBinTree(t);
printf("\n二叉树的前序遍历结果为:\n");
preorder(t);
printf("\n二叉树的中序遍历结果为:\n");
inorder(t);
printf("\n二叉树的后序便利结果为:\n");
postorder(t);
printf("\n");
}