二叉搜索树的建立
- 树的结构
struct stree{
int data;//数据
struct stree* left;//左子树
struct stree *right;//右子树
};
我做的二叉搜索树先要一个指针,然后再指针后面做一个树。
//创建一个二叉搜索树
struct stree* Creat_Bin(int x,struct stree *bin)
{
if(!bin){
//在创建第一个的时候当然是空的,所以先判断是不是第一个结点
bin=(struct stree*)malloc(sizeof(struct stree));
bin->data = x;
bin->left =bin->right =NULL;
return bin;
}else{
struct stree *tmp=bin;
while(bin){
if(x< bin->data ){
//如果我们放的这个数比原来这个结点数据小,就放左边。
if(bin->left==NULL){
如果他后面没有结点,那就放在这个后面
bin->left =(struct stree*)malloc(sizeof(struct stree));
bin->left->data=x;
bin->left->left=bin->left->right=NULL</

本文介绍了如何使用C语言创建二叉搜索树。通过树的结构,文章提供了完整的代码示例,该代码已在dev c++5.11环境下运行并通过了测试。作者还展示了两种不同的打印方式,供读者验证其正确性。
最低0.47元/天 解锁文章
6710

被折叠的 条评论
为什么被折叠?



