int Path(Btree* b, Btree* s) { Btree *p; int i, top = -1; bool flag; do { while (b) { top++; St[top] = b; b = b->1child; } p = NULL; flag =true; while (top != -1 && flag) b = st[top]; if (b->rchild == p) { if (b == s) { printf("\n >>所有祖先:\n"); for (i = 0; i < top; i++) printf("%s ", st[i]->name); printf("\n"); return 1; } else { top--; p = b; } else { b = b = > rchild: flag = false; } } while (top != -1); return 0; } Btree* CreateBTree(char* root) { int i = 0, j; BTree* b, * p; b = (BTree*)malloc(sizeof(BTree)); strcpy(b->name, root); b->lchild = b->rchild = NULL; while (i < n && strcmp(fam[i].father, root) != 0) i++; if (i < n) { p = (BTree*)malloc(sizeof(BTree)); p->lchild = p->rchild = NULL; strcpy(p->name, fam[i].wife); b->lchild = p; for (j = 0; j < n; j++) if (strcmp(fam[j].father, root) == 0) { p->rchild = CreateBTree(fam[j].son); p = p->rchild; } } return (b); }改错
时间: 2024-02-10 09:14:42 浏览: 34
该程序存在一些错误,我将逐一列出并进行修正:
1. 程序首先使用了变量`St`,但是该变量并没有被定义或声明,应该先声明一个合适的栈结构或数组。
2. `while (top != -1 && flag)`语句内的循环体没有包含花括号,导致程序逻辑错误。
3. 在第二个`else`语句中,`b = b = > rchild:`的箭头符号存在错误,应该改为`b = b->rchild;`。
修改后的代码如下所示:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct BTree {
char name[20];
struct BTree* lchild;
struct BTree* rchild;
} BTree;
typedef struct Family {
char father[20];
char wife[20];
char son[20];
} Family;
Family fam[] = {
{"刘备", "甘夫人", "刘禅"},
{"刘备", "糜夫人", "糜竺"},
{"刘备", "孙尚香", "刘福、刘谌"}
};
int n = 3;
BTree* St[MAXSIZE]; // 声明一个栈结构
int Path(BTree* b, BTree* s) {
BTree *p;
int i, top = -1;
bool flag;
do {
while (b) {
top++;
St[top] = b;
b = b->lchild;
}
p = NULL;
flag = true;
while (top != -1 && flag) { // 加入花括号
b = St[top];
if (b->rchild == p) {
if (b == s) {
printf("\n >>所有祖先:\n");
for (i = 0; i < top; i++)
printf("%s ", St[i]->name);
printf("\n");
return 1;
}
else {
top--;
p = b;
}
} // 加入花括号
else {
b = b->rchild; // 修改箭头符号
flag = false;
}
}
} while (top != -1);
return 0;
}
BTree* CreateBTree(char* root) {
int i = 0, j;
BTree* b, * p;
b = (BTree*)malloc(sizeof(BTree));
strcpy(b->name, root);
b->lchild = b->rchild = NULL;
while (i < n && strcmp(fam[i].father, root) != 0)
i++;
if (i < n) {
p = (BTree*)malloc(sizeof(BTree));
p->lchild = p->rchild = NULL;
strcpy(p->name, fam[i].wife);
b->lchild = p;
for (j = 0; j < n; j++)
if (strcmp(fam[j].father, root) == 0) {
p->rchild = CreateBTree(fam[j].son);
p = p->rchild;
}
}
return (b);
}
int main() {
BTree* root = CreateBTree("刘备");
Path(root, root->rchild->rchild);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)