有无佬看看是哪里写错了,为什么我尝试输出二叉树输出不了呢
#ifndef COM_H
#define COM_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxSize 50
typedef char elemtype;
typedef struct node
{
elemtype data;
struct node *left;
struct node *right;
}BTNode;
void CreateBTree(BTNode * b,char * str)
{
BTNode * St[MaxSize],*p;
int top = -1,k,j=0;
char ch;
b = NULL;
ch = str[j];
while(ch!='\0')
{
switch (ch)
{
case '(':
top++;
St[top] = p;
k=1;
break;
case ')':
top--;
break;
case ',':
k=2;
break;
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->left=p->right=NULL;
if(b==NULL)
b=p;
else
{
switch (k) {
case 1:
St[top]->left=p;
break;
case 2:
St[top]->right=p;
break;
}
}
}
j++;
ch=str[j];
}
}
void DestroyBTree(BTNode*b)
{
if(b!=NULL)
{
DestroyBTree(b->left);
DestroyBTree(b->right);
free(b);
}
}
BTNode *FindNode(BTNode*b,elemtype x)
{
BTNode * p;
if(b==NULL)
return NULL;
else if(b->data==x)
return b;
else
{
p=FindNode(b->left,x);
if(p!=NULL)
return p;
else
return FindNode(b->right,x);
}
}
BTNode *LchildNode(BTNode * p)
{
return p->left;
}
BTNode *RchildNode(BTNode * p)
{
return p->right;
}
int BTHeight(BTNode *b)
{
int left,right;
if(b==NULL)
return(0);
else
{
left = BTHeight(b->left);
right = BTHeight(b->right);
return (left>right)?(left+1):(right+1);
}
}
void DispBTree(BTNode *b)
{
if(b!=NULL)
{
printf("%c",b->data);
if(b->left!=NULL||b->right!=NULL)
{
printf("(");
DispBTree(b->left);
if(b->right!=NULL)
printf(",");
DispBTree(b->right);
printf(")");
}
}
}
void PreOrder(BTNode *b)
{
if(b!=NULL)
{
printf("%c",b->data);
PreOrder(b->left);
PreOrder(b->right);
}
}
void InOreder(BTNode *b)
{
if(b!=NULL)
{
InOreder(b->left);
printf("%c",b->data);
InOreder(b->right);
}
}
void PostOrder(BTNode *b)
{
if(b!=NULL)
{
PostOrder(b->left);
PostOrder(b->right);
printf("%c",b->data);
}
}
#endif
根据提供的代码和问题描述,可以看出问题可能在于 CreateBTree() 函数,函数的参数为指向根节点的指针,但是在函数中将指针重新赋值为NULL,导致函数结束后根节点指针并没有被正确修改。因此,需要将函数参数改为指向指针的指针,以确保函数中对指针的修改能够正确传出。
修改后的代码如下:
#ifndef COM_H
#define COM_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxSize 50
typedef char elemtype;
typedef struct node
{
elemtype data;
struct node *left;
struct node *right;
}BTNode;
void CreateBTree(BTNode ** b,char * str)
{
BTNode * St[MaxSize],*p;
int top = -1,k,j=0;
char ch;
(*b) = NULL; // 修改根节点指针的值,需要使用指针的指针
ch = str[j];
while(ch!='\0')
{
switch (ch)
{
case '(':
top++;
St[top] = p;
k=1;
break;
case ')':
top--;
break;
case ',':
k=2;
break;
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->left=p->right=NULL;
if((*b)==NULL)
(*b)=p; // 修改根节点指针的值,需要使用指针的指针
else
{
switch (k) {
case 1:
St[top]->left=p;
break;
case 2:
St[top]->right=p;
break;
}
}
}
j++;
ch=str[j];
}
}
void DestroyBTree(BTNode*b)
{
if(b!=NULL)
{
DestroyBTree(b->left);
DestroyBTree(b->right);
free(b);
}
}
BTNode *FindNode(BTNode*b,elemtype x)
{
BTNode * p;
if(b==NULL)
return NULL;
else if(b->data==x)
return b;
else
{
p=FindNode(b->left,x);
if(p!=NULL)
return p;
else
return FindNode(b->right,x);
}
}
BTNode *LchildNode(BTNode * p)
{
return p->left;
}
BTNode *RchildNode(BTNode * p)
{
return p->right;
}
int BTHeight(BTNode *b)
{
int left,right;
if(b==NULL)
return(0);
else
{
left = BTHeight(b->left);
right = BTHeight(b->right);
return (left>right)?(left+1):(right+1);
}
}
void DispBTree(BTNode *b)
{
if(b!=NULL)
{
printf("%c",b->data);
if(b->left!=NULL||b->right!=NULL)
{
printf("(");
DispBTree(b->left);
if(b->right!=NULL)
printf(",");
DispBTree(b->right);
printf(")");
}
}
}
void PreOrder(BTNode *b)
{
if(b!=NULL)
{
printf("%c",b->data);
PreOrder(b->left);
PreOrder(b->right);
}
}
void InOreder(BTNode *b)
{
if(b!=NULL)
{
InOreder(b->left);
printf("%c",b->data);
InOreder(b->right);
}
}
void PostOrder(BTNode *b)
{
if(b!=NULL)
{
PostOrder(b->left);
PostOrder(b->right);
printf("%c",b->data);
}
}
#endif
不知道你这个问题是否已经解决, 如果还没有解决的话:#define MAXSIZE 100 /* 存储空间初始分配量 */
#define MAX_TREE_SIZE 100 /* 二叉树的最大结点数 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int CElemType; /* 树结点的数据类型,目前暂定为整型 */
typedef CElemType SqBiTree[MAX_TREE_SIZE]; /* 0号单元存储根结点 */
CElemType Nil = 0; /*设整型以0为空 或者以 INT_MAX(65535)*/
typedef struct {
int level; //结点层
int order; //本层的序号(按照满二叉树给定序号规则)
}Position;