数据结构之---C语言实现二叉排序树(BinarySortTree)

400 篇文章 ¥9.90 ¥99.00
133 篇文章 ¥9.90 ¥99.00
本文介绍了如何使用C语言实现二叉排序树(Binary Sort Tree),详细讲解了二叉搜索树的概念,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里又叫做二叉搜索树

代码:

//二叉排序树(二叉搜索树Binary Search Tree)
//杨鑫
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
	int key;
	struct node *lchild, *rchild;
}BSTNode, *BSTree;

//插入
int InsertBST(BSTree *bst, int k)
{
	BSTree r, s, pre;
	r = (BSTree)malloc(sizeof(BSTNode));
	r->key = k;
	r->lchild = NULL;
	r->rchild = NULL;
	if(*bst == NULL)
	{
		*bst = r;
		return 1;
	}
	pre = NULL;
	s = *bst;
	while(s)
	{
		if(k == s->key)
				return 0;
		else if(k < s->key)
		{
			pre = s;
			s = s->lchild;
		}
		else
		{
			pre = s;
			s = s->rchild;
		}
	}
	if(k < pre->key)
			pre->lchild = r;
	else
			pre->rchild = r;
	return 1;
}	


void CreateBST(BSTree *bst)
{
	int key;
	*bst = NULL;
	scanf("%d", 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨鑫newlfe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值