#include <stdio.h>#include <stdlib.h>typedef int TElemType;typedef int Status;#define OK 1#define ERROR -1typedef struct BSTNode{ TElemType data; struct BSTNode *leftchild,*rightchild;}BSTNode,*BSTree;Status CreateBST(BSTree &T,int x) { if(T==NULL) { T = (BSTree)malloc(sizeof(BSTNode)); T->data=x; T->leftchild=T->rightchild=NULL; return OK; } if (x < T->data) { CreateBST(T->leftchild, x); } else { CreateBST(T->rightchild, x); } return OK;}Status print(BSTree T) { if(T) { print(T->leftchild); printf("%d ",T->data); print(T->rightchild); } return OK;}int main() { int a[100]; int n,i; BSTree T=NULL; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { CreateBST(T,a[i]); } print(T); printf("\n"); return 0;}
时间: 2024-04-02 10:35:57 浏览: 17
这是一段使用C语言实现的二叉搜索树的创建与遍历代码。在该代码中,定义了二叉搜索树节点的结构体BSTNode,包含数据域和左右孩子指针。使用CreateBST函数来创建二叉搜索树,print函数来进行中序遍历输出二叉搜索树的节点值。在main函数中,先输入节点个数n和n个节点的值,然后通过CreateBST函数创建二叉搜索树,并用print函数输出二叉搜索树的节点值。
相关问题
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef int ElemType; typedef struct
这段代码是C语言中的头文件引用和一些定义的示例。其中,`#include <stdio.h>`和`#include <stdlib.h>`是用来引入标准库函数的头文件。`stdio.h`包含了输入输出相关的函数,如`printf`和`scanf`;`stdlib.h`包含了一些常用的函数,如内存分配函数`malloc`和随机数生成函数`rand`。
`#define MAXSIZE 100`是一个宏定义,用来定义一个常量`MAXSIZE`,它的值为100。
`typedef int ElemType;`是用来给`int`类型起一个别名`ElemType`,方便在代码中使用。
至于代码中的结构体部分,由于你没有提供完整的代码,所以无法给出具体的解释。但一般来说,结构体用于定义一种自定义的数据类型,可以包含多个不同类型的成员变量。
include <stdio.h>#include <stdlib.h>
这是一个C语言的标准头文件的引用,其中<stdio.h>包含了C语言的输入输出函数,而<stdlib.h>包含了一些通用的工具函数和内存分配函数。这些头文件在C语言中非常常见,几乎每个C程序都会用到它们。下面是一个简单的例子,展示了如何使用这两个头文件。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
char str[100];
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered %d.\n", num);
printf("Enter a string: ");
scanf("%s", str);
printf("You entered %s.\n", str);
return 0;
}
```
上面的程序使用了<stdio.h>中的printf和scanf函数,以及<stdlib.h>中的exit函数。输入一个整数和一个字符串,然后将它们输出到控制台上。