请高手帮忙修改一下下面数据结构的代码(C语言版)解决思路
请高手帮忙修改一下下面数据结构的代码(C语言版)
输入p127二叉链表的定义
录入调试p131算法6.4,实现二叉树的构造函数
编写二叉树打印函数,可以通过递归算法将二叉树输出为广义表的形式,以方便观察树的结构。
#include<stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
char data;
struct BiTNdoe *lchild,*rchild;
}BiTNode,*BiTree;
int CreateBiTree(BiTNode* T){
char ch;
scanf(&ch);
if(ch==' ') T=NULL;
else{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
return 0;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
void DispBiTNode(BiTNode* T)
{
if (T!=NULL)
{
printf("%c",T->data);
if (T->lchild!=NULL || T->rchild!=NULL)
{
printf("(");
DispBiTNode(T->lchild); //递归处理左子树
if (T->rchild!=NULL) printf(",");
DispBiTNode(T->rchild); //递归处理右子树
printf(")");
}
}
}
void main()
{
BiTNdoe *T;
printf(" 构造一棵二叉树T:\n");
for(int i=0;i<15;i++)
CreateBiTree(T);
printf(" 广义表表示法:");
DispBiTNode(T);
printf("\n");
}
运行时出现了下列错误:
树以广义表的形式输出.cpp
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(17) : error C2664: 'CreateBiTree' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(18) : error C2664: 'CreateBiTree' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(30) : error C2664: 'DispBiTNode' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(32) : error C2664: 'DispBiTNode' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(42) : error C2664: 'CreateBiTree' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(44) : error C2664: 'DispBiTNode' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.
树以广义表的形式输出.obj - 1 error(s), 0 warning(s)
求解,谢谢
------解决方案--------------------
输入p127二叉链表的定义
录入调试p131算法6.4,实现二叉树的构造函数
编写二叉树打印函数,可以通过递归算法将二叉树输出为广义表的形式,以方便观察树的结构。
#include<stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
char data;
struct BiTNdoe *lchild,*rchild;
}BiTNode,*BiTree;
int CreateBiTree(BiTNode* T){
char ch;
scanf(&ch);
if(ch==' ') T=NULL;
else{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
return 0;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return 1;
}
void DispBiTNode(BiTNode* T)
{
if (T!=NULL)
{
printf("%c",T->data);
if (T->lchild!=NULL || T->rchild!=NULL)
{
printf("(");
DispBiTNode(T->lchild); //递归处理左子树
if (T->rchild!=NULL) printf(",");
DispBiTNode(T->rchild); //递归处理右子树
printf(")");
}
}
}
void main()
{
BiTNdoe *T;
printf(" 构造一棵二叉树T:\n");
for(int i=0;i<15;i++)
CreateBiTree(T);
printf(" 广义表表示法:");
DispBiTNode(T);
printf("\n");
}
运行时出现了下列错误:
树以广义表的形式输出.cpp
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(17) : error C2664: 'CreateBiTree' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(18) : error C2664: 'CreateBiTree' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(30) : error C2664: 'DispBiTNode' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(32) : error C2664: 'DispBiTNode' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(42) : error C2664: 'CreateBiTree' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\users\hp\desktop\新建文件夹\树以广义表的形式输出.cpp(44) : error C2664: 'DispBiTNode' : cannot convert parameter 1 from 'struct BiTNdoe *' to 'struct BiTNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.
树以广义表的形式输出.obj - 1 error(s), 0 warning(s)
求解,谢谢
------解决方案--------------------
- C/C++ code
#include<stdio.h> #include <malloc.h> typedef struct BiTNode { char data; BiTNode *lchild,*rchild;// }BiTNode,*BiTree; int CreateBiTree(BiTNode* T){ char ch; scanf(&ch); if(ch==' ') T=NULL; else{ if(!(T=(BiTNode*)malloc(sizeof(BiTNode)))) return 0; T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return 1; } void DispBiTNode(BiTNode* T) { if (T!=NULL) { printf("%c",T->data); if (T->lchild!=NULL || T->rchild!=NULL) { printf("("); DispBiTNode(T->lchild); //递归处理左子树 if (T->rchild!=NULL) printf(","); DispBiTNode(T->rchild); //递归处理右子树 printf(")"); } } } void main() { BiTNode *T; printf(" 构造一棵二叉树T:\n"); for(int i=0;i<15;i++) CreateBiTree(T); printf(" 广义表表示法:"); DispBiTNode(T); printf("\n"); }