发布网友 发布时间:2022-04-24 05:54
共2个回答
热心网友 时间:2022-04-18 21:39
#include<stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef enum {Link,Thread} PointerTag;
typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BiThrNode;
typedef BiThrNode *BiThrTree;
BiTree CreateBiTree(BiTree T) //先序遍历构造二叉树
{
char ch;
scanf("%c",&ch);
if(ch=='#') //#代表空指针
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode)); //申请结点
if(!T)
exit(OVERFLOW);
T->data=ch; //生成根结点
T->lchild=CreateBiTree(T->lchild); //构造左子树
T->rchild=CreateBiTree(T->rchild); //构造右子树
}
return T;
}
Status InOrderTraverse(BiTree T,Status(*visit)(TElemType))
{//中序遍历二叉树
if(T)
{
if(InOrderTraverse(T->lchild,visit))
if(visit(T->data))
if(InOrderTraverse(T->rchild,visit))
return OK;
return ERROR;
}
else
return OK;
}
Status PrintElement(TElemType e)
{
printf("%-2c",e);
return OK;
}
void main()
{
BiTree T=NULL;
printf("请输入构造二叉树的字符序列:");
T=CreateBiTree(T);
if(T)
printf("二叉树建立成功!\n");
else
printf("二叉树构造失败!!!\n");
printf("中序遍历二叉树:");
InOrderTraverse(T,PrintElement);
printf("\n");
}
热心网友 时间:2022-04-18 22:57
#include <iostream>