#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef char data_t; typedef struct BiTNode { data_t data;//存放本节点数据 struct tree *l_child;//存放左孩子节点地址 struct tree *r_child;//存放右孩子节点地址 }BiTNode,*BiTree; void CreateBiTree(BiTree &T) { char ch,cin; cin>>ch; if(ch=='#') T=NULL; else{ T= new BiTNode; T->data=ch; CreateBiTree(T->l_child); CreateBiTree(T->r_child); } }
时间: 2024-02-26 13:53:46 浏览: 43
这段代码是二叉树的创建函数 CreateBiTree,它使用递归的方式创建一棵二叉树。函数的参数是一个指向 BiTree 类型的指针 T,用于存储二叉树的根节点地址。
在函数中,首先读入一个字符 ch,如果 ch 是 '#',说明当前节点为空节点,将 T 的值设为 NULL,并结束递归。否则,创建一个新的 BiTNode 节点,并将 ch 存储到节点的 data 成员中。然后,递归调用 CreateBiTree 函数,将返回的地址分别存储到当前节点的左孩子和右孩子中。
值得注意的是,结构体中的成员变量类型应该是 BiTree*,而不是 tree*,因为 BiTree 是一个指向 BiTNode 的指针类型,而不是结构体类型。
相关问题
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef int DataType; void print(BiTree tree); BiTree CreatBiTree(); typedef struct Node{ DataType data; struct Node *LChild; struct Node *RChild; }BiTNode,* BiTree; void print(BiTree tree) { if(tree){ printf(tree->LChild); printf("%d",tree->data); printf(tree->RChild); } } BiTree CreatBiTree() { char ch; if(ch=='#') return NULL; else{ BiTree Node=((Node *)malloc(Node)); Node->LChild=CreatBiTree(); Node->data=ch; Node->RChild=CreatBiTree(); } } int main() { BiTree tree=NULL; tree=CreatBiTree(); print(tree); return 0; }找错误
这段代码中存在以下几个错误:
1. 缺少头文件定义,应该添加头文件:`#include "stdlib.h"`和`#include "string.h"`。
2. `BiTree`的定义应该在函数之前。
3. `CreatBiTree`函数中缺少输入,应该在函数内部添加一个读取输入字符的语句,例如:`scanf("%c",&ch);`。
4. 在`CreatBiTree`函数中,分配内存的语句应该是`Node=((BiTree)malloc(sizeof(BiTNode)));`,而不是`Node=((Node *)malloc(Node));`。
5. `print`函数中应该打印`tree->LChild`和`tree->RChild`的数据,而不是直接打印指针地址。应该改成:`print(tree->LChild); printf("%d",tree->data); print(tree->RChild);`。
下面是修改后的代码:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int DataType;
typedef struct Node{
DataType data;
struct Node *LChild;
struct Node *RChild;
} BiTNode,* BiTree;
BiTree CreatBiTree();
void print(BiTree tree);
void print(BiTree tree) {
if(tree){
print(tree->LChild);
printf("%d",tree->data);
print(tree->RChild);
}
}
BiTree CreatBiTree() {
char ch;
scanf("%c",&ch);
if(ch=='#')
return NULL;
else{
BiTree Node=((BiTree)malloc(sizeof(BiTNode)));
Node->LChild=CreatBiTree();
Node->data=ch;
Node->RChild=CreatBiTree();
return Node;
}
}
int main() {
BiTree tree=NULL;
tree=CreatBiTree();
print(tree);
return 0;
}
```
#include<stdio.h> #include<stdlib.h> typedef struct BiTNode{ char data; struct BiTNode *lchild; struct BiTNode *rchild; }BiTNode,*Bintree; void CreateBinaryTree(Bintree *T){ char ch; scanf("%c",&ch); if(ch=='#') *T==NULL; else{ *T = (Bintree)malloc(sizeof(BiTNode)); (*T)->data = ch; CreateBinaryTree(&(*T)->lchild); CreateBinaryTree(&(*T)->rchild); } } void midOrder(Bintree T){ if(T){ midOrder(T->lchild); printf("%c",T->data); midOrder(T->rchild); } } int main() { Bintree T; CreateBinaryTree(&T); midOrder(T); return 0; }修改这段代码
代码中存在一个明显的错误,即第11行应该是赋值操作而不是等于操作。此外,函数名`midOrder`应该改为`inOrder`,以符合中序遍历的命名规范。修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
} BiTNode, *Bintree;
void CreateBinaryTree(Bintree *T){
char ch;
scanf("%c", &ch);
if (ch == '#')
*T = NULL;
else{
*T = (Bintree)malloc(sizeof(BiTNode));
(*T)->data = ch;
CreateBinaryTree(&((*T)->lchild));
CreateBinaryTree(&((*T)->rchild));
}
}
void inOrder(Bintree T){
if (T){
inOrder(T->lchild);
printf("%c", T->data);
inOrder(T->rchild);
}
}
int main(){
Bintree T;
CreateBinaryTree(&T);
inOrder(T);
return 0;
}
```