// 先序创建二叉树 int creatBTTree(btNode** tree, char* str, int n) { char ch = str[n]; printf("%d %c\n", n, ch); n += 1; if (ch != '\0') { if (ch == '#') { *tree = NULL; } else { (*tree) = (btNode*) malloc(sizeof(btNode)); if (*tree = NULL) { printf("创建失败\n"); exit(0); } (*tree)->data = ch; n = creatBTTree(&((*tree)->rChild), str, n); n = creatBTTree(&((*tree)->lChild), str, n); } } return n; } void printTree(btNode* tree) { if (tree == NULL) { printf("#"); } else { printf("%c", tree->data); printTree(tree->lChild); printTree(tree->rChild); } } int main(void) { char* str = "ABC##DE#G##F###"; btNode* tree = NULL; creatBTTree(&tree, str, 0); printTree(tree); return 0; }这段代码有什么问题?
时间: 2024-01-18 17:03:45 浏览: 32
在代码中存在一个错误,即在判断节点是否创建成功时,使用了单等号(=)而不是双等号(==)。具体来说,在以下代码中:
```
(*tree) = (btNode*) malloc(sizeof(btNode));
if (*tree = NULL)
```
应该将其中的 `*tree = NULL` 改为 `*tree == NULL`,即改为双等号。这是因为单等号是赋值操作符,会将 NULL 赋值给 `*tree`,而双等号才是相等判断操作符。这个错误会导致节点的创建失败,进而导致程序出现异常。
相关问题
生成先序遍历为-+a*b-cd/ef的先序二叉树
根据先序遍历构建二叉树的方法,我们可以通过递归的方式来构建该二叉树。
先序遍历的顺序是:根节点 -> 左子树 -> 右子树。
对于当前的先序遍历序列-+a*b-cd/ef,根据顺序,我们可以得到根节点为-,左子树为+,右子树为/。
接下来,我们递归的构建左子树和右子树。
左子树的先序遍历序列是+ab*cd,我们可以得到根节点为+,左子树为a,右子树为*b-cd。继续递归构建右子树,得到先序遍历序列为/ef,根节点为/,左子树为e,右子树为f。
最终,我们得到如下的二叉树:
```
-
/ \
+ /
/ \ / \
a * - f
/ \
b c/d
```
c语言CreateBTree(BTNode *&bt,char *str)代码
下面是 C 语言实现的 CreateBTree 函数代码,可以根据一个字符串 str 创建一棵二叉树:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct BTNode {
char data;
struct BTNode *lchild, *rchild;
} BTNode;
void CreateBTree(BTNode *&bt, char *str) {
BTNode *st[100], *p;
int top = -1, k, j = 0;
char ch;
bt = 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->lchild = p->rchild = NULL;
if (bt == NULL) {
bt = p;
} else {
switch(k) {
case 1:
st[top]->lchild = p;
break;
case 2:
st[top]->rchild = p;
break;
}
}
}
j++;
ch = str[j];
}
}
```
函数的参数包括一棵二叉树 BTNode *&bt 和一个字符串 str。函数中使用了堆栈 st 来保存节点,堆栈中存储的是 BTNode 类型的指针。代码中使用了 switch 语句来处理字符串中的每个字符,根据不同的字符进行不同的操作。函数会根据字符串中的括号和逗号来构建二叉树的结构,最终返回构建好的二叉树。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)