c语言二叉树的建立与遍历,,程序总停留在输入环节,下不去!!急
#include<stdio.h>#include<malloc.h>#defineM10typedefstructbnode{chardata;structbnode*...
#include <stdio.h>
#include <malloc.h>
#define M 10
typedef struct bnode
{
char data;
struct bnode *lchild;
struct bnode *rchild;
}Bnode, *BTree;
/*建立二叉树*/
void creat_BTree(BTree &T)
{
char n;
n=getchar();
if(n=='#') T=NULL;
else
{
T=(Bnode *)malloc(sizeof(Bnode));
T->data=n;
creat_BTree(T->lchild);
creat_BTree(T->rchild);
}
}
/*前序遍历*/
void r_preorder(BTree T)
{
if(!T)
{
printf("%c",T->data);
r_preorder(T->lchild);
r_preorder(T->rchild);
}
}
/*后序遍历*/
void r_posorder(BTree T)
{
if(!T)
{
r_posorder(T->lchild);
r_posorder(T->rchild);
printf("%c",T->data);
}
}
/*层次遍历*/
void r_levelorder(BTree T)
{ BTree q[M];
int front=0,rear=0;
BTree p;
if(T)
{
q[rear]=T;
rear=(rear+1)%M;
}
while(front!=rear)
{
p=q[front];
printf("%c ",p->data);
if(p->lchild)
{
q[rear]=p->lchild;
rear=(rear+1)%M;
}
if(p->rchild)
{
q[rear]=p->rchild;
rear=(rear+1)%M;
}
}
}
int main()
{
BTree T;
T=NULL;
int select;
while(1)
{
printf("\n请选择要进行的操作:\n");
printf(" 1 创建二叉树\n");
printf(" 2 前序遍历\n");
printf(" 3 后序遍历\n");
printf(" 4 层次遍历\n");
printf(" 5 结束程序\n");
printf("=========================\n");
scanf("%d",&select);
switch (select)
{
case 1:
{
printf("请按前序次序输入各结点的值,以#表示空树(输入时可连续输入)\n");
creat_BTree(T);
break;
}
case 2:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
r_preorder(T);
break;
}
case 3:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
r_posorder(T);
break;
}
case 4:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
r_levelorder(T);
break;
}
default:;
}
}
} 展开
#include <malloc.h>
#define M 10
typedef struct bnode
{
char data;
struct bnode *lchild;
struct bnode *rchild;
}Bnode, *BTree;
/*建立二叉树*/
void creat_BTree(BTree &T)
{
char n;
n=getchar();
if(n=='#') T=NULL;
else
{
T=(Bnode *)malloc(sizeof(Bnode));
T->data=n;
creat_BTree(T->lchild);
creat_BTree(T->rchild);
}
}
/*前序遍历*/
void r_preorder(BTree T)
{
if(!T)
{
printf("%c",T->data);
r_preorder(T->lchild);
r_preorder(T->rchild);
}
}
/*后序遍历*/
void r_posorder(BTree T)
{
if(!T)
{
r_posorder(T->lchild);
r_posorder(T->rchild);
printf("%c",T->data);
}
}
/*层次遍历*/
void r_levelorder(BTree T)
{ BTree q[M];
int front=0,rear=0;
BTree p;
if(T)
{
q[rear]=T;
rear=(rear+1)%M;
}
while(front!=rear)
{
p=q[front];
printf("%c ",p->data);
if(p->lchild)
{
q[rear]=p->lchild;
rear=(rear+1)%M;
}
if(p->rchild)
{
q[rear]=p->rchild;
rear=(rear+1)%M;
}
}
}
int main()
{
BTree T;
T=NULL;
int select;
while(1)
{
printf("\n请选择要进行的操作:\n");
printf(" 1 创建二叉树\n");
printf(" 2 前序遍历\n");
printf(" 3 后序遍历\n");
printf(" 4 层次遍历\n");
printf(" 5 结束程序\n");
printf("=========================\n");
scanf("%d",&select);
switch (select)
{
case 1:
{
printf("请按前序次序输入各结点的值,以#表示空树(输入时可连续输入)\n");
creat_BTree(T);
break;
}
case 2:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
r_preorder(T);
break;
}
case 3:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
r_posorder(T);
break;
}
case 4:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
r_levelorder(T);
break;
}
default:;
}
}
} 展开
1个回答
展开全部
#include <stdio.h>
#include <malloc.h>
#define M 10
typedef struct bnode
{
char data;
struct bnode *lchild;
struct bnode *rchild;
}Bnode, *BTree;
/*建立二叉树*/
void creat_BTree(BTree* T)
{
char n;
n=getchar();
if(n=='#') *T=NULL;
else if(n=='\n') return;
else
{
(*T)=(BTree)malloc(sizeof(Bnode));
(*T)->data=n;
creat_BTree(&(*T)->lchild);
creat_BTree(&(*T)->rchild);
}
}
/*前序遍历*/
void r_preorder(BTree T)
{
if(T)
{
printf("%c",T->data);
r_preorder(T->lchild);
r_preorder(T->rchild);
}
}
/*后序遍历*/
void r_posorder(BTree T)
{
if(T)
{
r_posorder(T->lchild);
r_posorder(T->rchild);
printf("%c",T->data);
}
}
/*层次遍历*/
void r_levelorder(BTree T)
{
BTree q[1024];
BTree p;
int front=0,rear=0;
if(T)
{
q[rear]=T;
rear=(rear+1)%M;
}
while(front!=rear)
{
p=q[front];
printf("%c ",p->data);
if(p->lchild)
{
q[rear]=p->lchild;
rear=(rear+1)%M;
}
if(p->rchild)
{
q[rear]=p->rchild;
rear=(rear+1)%M;
}
front = (front+1)%M;
}
}
int main()
{
BTree T;
T=NULL;
int select;
while(1)
{
printf("\n请选择要进行的操作:\n");
printf(" 1 创建二叉树\n");
printf(" 2 前序遍历\n");
printf(" 3 后序遍历\n");
printf(" 4 层次遍历\n");
printf(" 5 结束程序\n");
printf("=========================\n");
scanf("%d",&select);
getchar();
switch (select)
{
case 1:
{
printf("请按前序次序输入各结点的值,以#表示空树(输入时可连续输入)\n");
creat_BTree(&T);
break;
}
case 2:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
else r_preorder(T);
break;
}
case 3:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
else r_posorder(T);
break;
}
case 4:
{
if(!T) printf("未建树,请先执行1操作建树!\n");
else r_levelorder(T);
break;
}
default:;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询