|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
建立好像没问题,遍历有错看不出来,求助
#include <stdio.h>
#include <stdlib.h>
typedef struct tree{
int data;
struct tree *left;
struct tree *right;
}BiTree;
void pushTree(BiTree *);
void ptree2(BiTree *);
int main(void)
{
BiTree *p;
pushTree(p);
printf("%d",p->data);
return 0;
}
void pushTree(BiTree *root)
{
int i;
printf("shuru\n");
scanf("%d",&i);
if(i==9999)
root=NULL;
else{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=i;
printf("%d左指数\n",root->data);
pushTree(root->left);
printf("%d右指数\n",root->data);
pushTree(root->right);
}
}
void ptree2(BiTree *root)
{
if(root)
{
printf("%d ",root->data);
ptree2(root->left);
ptree2(root->right);
}
}
本帖最后由 Stubborn 于 2021-5-15 18:54 编辑
我学C没有学多久,不过看你的这个函数
void pushTree(BiTree *root)
root=(struct tree*)malloc(sizeof(struct tree));
你申请下来的root确定不会覆盖传进来的root指针吗?
你确定你申请下来的root节点,"挂“在之前节点的左或者右侧了吗?
- #include <stdio.h>
- #include <stdlib.h>
- #define TElemType int
- typedef struct BitNode{
- TElemType data;
- struct BitNode *left, *right;
- }BitNode, *BitTree;
- void CreateBiTree(BitTree *);
- void DisplayBiTree(BitTree *);
- int main(void)
- {
- BitTree tree;
- CreateBiTree(&tree);
- DisplayBiTree(&tree);
- return 0;
- }
- void CreateBiTree(BitTree *root)
- {
- int value = 999;
- printf("input data value>>[ ] \b\b");
- scanf("%d", &value);
- if (value == 999)
- {
- *root = NULL;
- } else
- {
- *root = (BitTree) malloc(sizeof(BitNode));
- (*root)->data = value;
- CreateBiTree(&(*root)->left);
- CreateBiTree(&(*root)->right);
- }
- }
- void DisplayBiTree(BitTree *root)
- {
- if (root)
- {
- printf(" value is > %d", (*root)->data);
- DisplayBiTree(&(*root)->left);
- DisplayBiTree(&(*root)->right);
- }
- }
复制代码
|
|