69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#define Max 500
typedef struct BiTNode//树结点结构
{
char date; //数据
struct BiTNode *plchild,*prchild;//左右孩子指针
}BiTNode, *BiTree;
void CreateBiTree(BiTree T)
{
char c;
scanf("%c",&c);//输入节点数据
if(c == '#')//如果输入‘#’表示结点为空
T=NULL;
else{
if(!(T = (BiTree)malloc(sizeof(BiTNode))))//申请结点空间
exit(-1);
T->date = c;
//printf("%c",T->date);
CreateBiTree(T->plchild);//由建立左子树
CreateBiTree(T->prchild);//建立右子树
}
}
void Display(BiTree root)//层序输出树
{
BiTree queue[Max],p;
int front=0,rear=0;
queue[rear++]=root;
if(NULL==root)return ;
while(rear!=front)
{
p=queue[front++];
printf("%c",p->date);
if(p->plchild)queue[rear++]=p->plchild;
if(p->prchild)queue[rear++]=p->prchild;
}
}
int main()
{
BiTree p;//建立树节点
CreateBiTree(p);
Display(p);
}
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define Max 500
typedef struct BiTNode//树结点结构
{
int date; //数据
struct BiTNode *plchild,*prchild;//左右孩子指针
}BiTNode, *BiTree;
BiTree CreateBiTree(BiTree T)
{
char c ;
scanf(" %c", &c); //输入节点数据,%c前面加一个空格,否则回车符也被读取了
if(c =='#') //如果输入#表示结点为空
{
return NULL ;
}
else{
if(!(T = (BiTree)malloc(sizeof(BiTNode))))//申请结点空间
{
printf("Error code 0x01:fail to malloc memory!") ;
exit(-1);
}
T->date = c;
//printf("%c",T->date);
T->plchild=CreateBiTree(T->plchild);//由建立左子树
T->prchild=CreateBiTree(T->prchild);//建立右子树
return T ;
}
}
void Display(BiTree root)//层序输出树
{
BiTree queue[Max],p;
int front=0,rear=0;
queue[rear++]=root;
if(NULL==root)return ;
while(rear!=front)
{
p=queue[front++];
printf("%c",p->date);
if(p->plchild) queue[rear++]=p->plchild;
if(p->prchild) queue[rear++]=p->prchild;
}
}
int main()
{
BiTree p=NULL;//建立树节点
p=CreateBiTree(p);
Display(p);
}
#include <stdio.h>
#include <stdlib.h>
#define Max 500
typedef struct BiTNode//树结点结构
{
char date; //数据
struct BiTNode *plchild,*prchild;//左右孩子指针
}BiTNode, *BiTree;
void CreateBiTree(BiTree* T)
{
char c;
scanf("%c",&c);//输入节点数据
getchar();
if(c == '#')//如果输入‘#’表示结点为空
*T=NULL;
else{
if(!(*T = (BiTree)malloc(sizeof(BiTNode))))//申请结点空间
exit(-1);
(*T)->date = c;
//printf("%c",T->date);
CreateBiTree(&((*T)->plchild));//由建立左子树
CreateBiTree(&((*T)->prchild));//建立右子树
}
}
void Display(BiTree root)//层序输出树
{
BiTree queue[Max],p;
int front=0,rear=0;
queue[rear++]=root;
if(NULL==root)return ;
while(rear!=front)
{
p=queue[front++];
printf("%c",p->date);
if(p->plchild)queue[rear++]=p->plchild;
if(p->prchild)queue[rear++]=p->prchild;
}
}
int main()
{
BiTree p;//建立树节点
CreateBiTree(&p);
Display(p);
}