void Creat(Node* T) { char c=getchar(); if (c == '#') T = NULL; else { T->data = c; T->Lchild = (Node*)malloc(sizeof(Node)); T->Rchild = (Node*)malloc(sizeof(Node)); } Creat(T->Lchild); Creat(T->Rchild); }这段代码是创建二叉树的函数,编译失败,修改并对比找出原因
时间: 2024-02-12 22:05:36 浏览: 40
这段代码的问题在于,在递归函数 `Creat` 中传递的参数 `Node* T` 是一个指针,而函数内部对于这个指针的修改并不会影响到调用函数时传递的实参指针。因此,递归函数内部对于 `T` 指针的修改相当于是在函数内部新建了一个指针,而实参指针并没有被修改。
要解决这个问题,可以将 `Creat` 函数的参数修改为 `Node** T`,即传递一个指向指针的指针,这样递归函数内部对于 `*T` 的修改就能够影响到调用函数时传递的实参指针。
修改后的代码如下:
```
void Creat(Node** T) {
char c = getchar();
if (c == '#')
*T = NULL;
else {
(*T) = (Node*)malloc(sizeof(Node));
(*T)->data = c;
Creat(&((*T)->Lchild));
Creat(&((*T)->Rchild)));
}
}
```
值得注意的是,为了防止内存泄漏,还需要在程序结束时对二叉树进行销毁。
相关问题
#include <stdlib.h>#include <stdio.h>#define N 10typedef struct list{ int data; struct list *next;} NODE;NODE *creat_list(int *a, int n){ NODE *head, *p, *q; head = (NODE *)malloc(sizeof(NODE)); head->next = NULL; q = head; for (int i = 0; i < n; i++) { p = (NODE *)malloc(sizeof(NODE)); p->data = a[i]; q->next = p; q = p; } q->next = NULL; return head;}void insNode(NODE *head, int x){ NODE *p, *q, *t; p = head; q = head->next; while (q != NULL && q->data < x) { p = q; q = q->next; } t = (NODE *)malloc(sizeof(NODE)); t->data = x; p->next = t; t->next = q;}void print(NODE *h){ NODE *p; p = h->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n");}int main(){ int i, j, x, a[N] = {1, 13, 25, 37, 9, 911, 133, 415, 147, 19}; NODE *head; head = creat_list(a, 10); scanf("%d", &x); insNode(head, x); print(head); return 0;}这个没问题吗?
这个代码的主要功能是实现将一个无序数组转化为有序链表,并能够在有序链表中插入一个新的元素,最后输出整个有序链表。代码的主要逻辑和实现方式都没有问题,但是有两个小问题需要注意:
1. 在创建链表的时候,首先要创建一个头节点,但是头节点不应该存储任何数据,因此应该将头节点的data域设为0或者其他一个无意义的值,而不是随便malloc一个节点。
2. 在插入新节点的时候,如果插入的节点比链表中所有节点的值都要小,那么新节点应该作为头节点的后继节点,而不是作为头节点本身。因此应该在while循环结束后,判断一下是否需要将新节点作为头节点的后继节点。
下面是修改后的代码:
```
#include <stdlib.h>
#include <stdio.h>
#define N 10
typedef struct list{
int data;
struct list *next;
} NODE;
NODE *creat_list(int *a, int n){
NODE *head, *p, *q;
head = (NODE *)malloc(sizeof(NODE));
head->data = 0; // 头节点不存储任何数据
head->next = NULL;
q = head;
for (int i = 0; i < n; i++)
{
p = (NODE *)malloc(sizeof(NODE));
p->data = a[i];
q->next = p;
q = p;
}
q->next = NULL;
return head;
}
void insNode(NODE *head, int x){
NODE *p, *q, *t;
p = head;
q = head->next;
while (q != NULL && q->data < x)
{
p = q;
q = q->next;
}
t = (NODE *)malloc(sizeof(NODE));
t->data = x;
p->next = t;
t->next = q;
if (head->next == NULL || x < head->next->data) {
head->next = t;
}
}
void print(NODE *h){
NODE *p;
p = h->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main(){
int i, j, x, a[N] = {1, 13, 25, 37, 9, 911, 133, 415, 147, 19};
NODE *head;
head = creat_list(a, 10);
scanf("%d", &x);
insNode(head, x);
print(head);
return 0;
}
```
希望能对你有所帮助!
请按照下面的核心代码进行更改#include #include struct node{ char info; struct node *llink,*rlink; }; typedef struct node NODE; NODE *creat(){ char x; NODE *p; scanf("%c",&x); printf("%c",x); if(x!='.'){ p=(NODE *)malloc(sizeof(NODE)); p->info=x; p->llink=creat(); p->rlink=creat(); } else p=NULL; return p; } void run(NODE *t){ if(t){ run(t->llink); run(t->rlink); printf("%c",t- >info); } } main() { NODE *T; printf("PLease input a tree:\n"); T=creat(); printf("\n"); if(!T) printf("This is a empty binary tree."); else { printf("The result of post travese is:\n "); run(T); } printf("\n"); }
好的,根据你提供的代码,我们可以将其修改为按照题目要求进行输入和输出的程序。具体的实现可以参考下面的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char info;
struct node *llink, *rlink;
} NODE;
NODE* creat() {
char x;
NODE* p;
scanf("%c", &x);
if (x == '.') {
printf(".");
p = NULL;
} else {
p = (NODE*)malloc(sizeof(NODE));
p->info = x;
p->llink = creat();
p->rlink = creat();
}
return p;
}
void run(NODE* t) {
if (t) {
run(t->llink);
run(t->rlink);
printf("%c", t->info);
}
}
int main() {
NODE* T;
printf("Please input a tree:\n");
T = creat();
printf("\n");
if (!T) {
printf("THIS IS A EMPTY BINARY TREE\n");
} else {
printf("The result of post travese is:\n");
run(T);
printf("\n");
}
return 0;
}
```
输入样例:ABD..EH...CF.I..G..
输出样例:DHEBIFGCA
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)