|
发表于 2023-11-9 10:32:47
|
显示全部楼层
问题出在初始化二叉树的函数 initbitree 上。在初始化时,应该将 t->date 赋值为一个有效的值,而不是 NULL 。因为 telemtype 被定义为 char 类型,所以 t->date 应该赋值为一个字符,而不是 NULL 。
以下是修正后的代码:
- #include<stdio.h>
- #include<stdlib.h>
- #define OK 1
- #define ERROR 0
- typedef int status;
- typedef char telemtype;
- typedef struct BitNode{
- telemtype date;
- struct BitNode *lchild,*rchild;
- }BitNode,*BiTree;
- void initbitree(BiTree &t)
- {
- t=(BitNode*)malloc(sizeof(BitNode));
- t->date='\0'; // 将t->date赋值为一个有效的字符
- t->lchild=NULL;
- t->rchild=NULL;
- }
- status createbitree(BiTree &t)
- {
- char e;
- scanf("%c",&e);
- if(e=='#') t->date='\0'; // 将t->date赋值为一个有效的字符
- else{
- t=(BitNode*)malloc(sizeof(BitNode));
- t->date=e;
- createbitree(t->lchild);
- createbitree(t->rchild);
- }
- return OK;
- }
- void PreOrder(BiTree t)
- {
- if(t){
- printf("%c",t->date);
- PreOrder(t->lchild);
- PreOrder(t->rchild);
- }
- }
- int main()
- {
- BiTree t;
- int m=0;
- initbitree(t);
- while(m!=6){
- printf("1.建立一个二叉树 2.复制二叉树 3.判断相等 4.求叶子结点个数 5.求二叉树深度 6.退出\n");
- printf("请输入你想进行的操作:");
- scanf("%d",&m);
- switch(m)
- {
- case 1: createbitree(t);
- printf("该二叉树为:");
- PreOrder(t);
- break;
- }
- }
- }
复制代码
这样修正后的代码应该可以正常运行了。 |
|