二叉搜索树(C语言实现)超级简洁版

数据结构设计

#define BST_NODE_CHILD 0
#define BST_NODE_PARENT 1

//bst的节点
typedef struct bst_node
{
   
	//关键字
	int key;
	//左孩子
	bst_node* left;
	//右孩子
	bst_node* right;
}bst_node;

//bst树
typedef struct bst
{
   
	//树的节点
	struct bst_node* root;
	//记录树的节点数目
	int size;
}bst;

接口设计

//创建一个空的二叉搜索树
bst* bst_create();
//创建一个bst的节点
bst_node* bst_create_node(int key);
//查找节点(type为选择查找目标的父节点还是本身);
bst_node* bst_search_node(bst_node* node, int type, int key);
//搜索子树中最大节点或其父节点
bst_node* bst_find_max_node(bst_node* node, int type);
//插入节点
bst* bst_insert_node(bst* bst, int key);
//删除节点
void bst_delete_node(bst* bst, int key);
//中序遍历
void bst_inorder_traversal(bst_node* node);

测试

int main()
{
   
	bst* bst = bst_create();
	int arr[] = {
    21,3,5,26,29,50,18,53,8,67,1,78,6 };
	int length = sizeof(arr) / sizeof(int);

	for (int i = 0; i < length; i++)
	{
   
		bst_insert_node(bst, arr[i]);
		printf("size:%d ", bst->size
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值