c语言实现:二叉搜索树

本文详细介绍了如何使用C语言实现二叉搜索树,包括树的插入、删除和查找操作。通过实例展示了二叉搜索树在数据组织和排序上的高效特性,帮助读者深入理解数据结构的应用。

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

#pragma once

#include<stdio.h>
#include<malloc.h>
#include<assert.h>

#define T int
#define FALST 0
#define TRUE 1
#define BOOL int

typedef struct BSTreeNode
{
	T data;
	BSTreeNode *leftChild;
	BSTreeNode *rightChild;
}BSTreeNode;

typedef struct BSTree
{
	BSTreeNode *root;
}BSTree;

void InitBSTree(BSTree *bst);
BOOL InsertBSTree(BSTree *bst, T x);
//BOOL InsertBSTree(BSTreeNode **t, T x);
BOOL InsertBSTree(BSTreeNode *&t, T x);
//求最大最小值
T Min(BSTree *bst);
T Min(BSTreeNode *t);
T Max(BSTree *bst);
T Max(BSTreeNode *t);
//排序,即进行中序遍历
void Sort(BSTree *bst);
void Sort(BSTreeNode *t);
//搜索值为key的节点,找不到则返回空
BSTreeNode* SearchBST(BSTree *bst,T key);
BSTreeNode* SearchBST(BSTreeNode *t,T key);
//制空二叉树
void MakeEmptyBSTree(BSTree *bst);
void MakeEmptyBSTree(BSTreeNode **t);
//删除值为key的节点
BOOL RemoveBSTree(BSTree *bst,T key);
BOOL RemoveBSTree(BSTreeNode *&t,T key);

#include"BSTree.h"

void InitBSTree(BSTree *bst)
{
	bst->root = NULL;
}

//利用二级指针完成插入
//BOOL InsertBSTree(BSTree *bst, T x)
//{
//	return InsertBSTree(&bst->root, x);
//}
//BOOL InsertBSTree(BSTreeNode **t, T x)
//{
//	if ((*t) == NULL)
//	{
//		(*t) = (BSTreeNode*)malloc(sizeof(BSTreeNode));
//		assert((*t) != NULL);
//		(*t)->data = x;
//		(*t)->leftChild = NULL;
//		(*t)->rightChild = NULL;
//	}
//	else if ((*t)->data > x)
//		return InsertBSTree(&(*t)->leftChild, x);
//	else if ((*t)->data < x)
//		return InsertBSTree(&(*t)->rightChild, x);
//    return TRUE;
//}

//利用c++的引用完成插入操作
BOOL InsertBSTree(BSTree *bst, T x)
{
	return InsertBSTree(bst->root, x);
}
BOOL InsertBSTree(BSTreeNode *&t, T x)
{
	if (t == NULL)
	{
		t = (BSTreeNode*)malloc(sizeof(BSTreeNode));
		assert(t != NULL);
		t->data = x;
		t->leftChild = NULL;
		t->rightChild = NULL;
	}
	else if (t->data > x)
		return InsertBSTree(t->leftChild, x);
	else if (t->data < x)
		return InsertBSTree(t->rightChild, x);
	return TRUE;
}

//求最大值,即二叉搜索树的最右节点的值,求最下值,即二叉搜索树的最左节点的值
T Min(BSTree *bst)
{
	assert(bst->root != NULL);
	return Min(bst->root);
}
T Min(BSTreeNode *t)
{
		BSTreeNode *p = t->leftChild;
		while (p->leftChild != NULL)
		{
			p = p->leftChild;
		}
		return p->data;	
}
T Max(BSTree *bst)
{
	assert(bst->root != NULL);
	return Max(bst->root);
}
T Max(BSTreeNode *t)
{
		BSTreeNode *p = t->rightChild;
		while (p->rightChild != NULL)
			p = p->rightChild;
		return p->data;	
}

void Sort(BSTree *bst)
{
	Sort(bst->root);
	printf("\n");
}
void Sort(BSTreeNode *t)
{
	if (t != NULL)
	{
		Sort(t->leftChild);
		printf("%d ", t->data);
		Sort(t->rightChild);
	}
}

BSTreeNode* SearchBST(BSTree *bst,T key)
{
	return SearchBST(bst->root,key);
}
BSTreeNode* SearchBST(BSTreeNode *t,T key)
{
	if (t == NULL)
		return NULL;
	if (t->data == key)
		return t;
	//比key大,在左子树早,比key小在右子数找
	if (t->data > key)
	{
		return SearchBST(t->leftChild, key);
	}
	else
	{
		return SearchBST(t->rightChild, key);
	}
}

void MakeEmptyBSTree(BSTree *bst)
{
	MakeEmptyBSTree(&bst->root);
}
void MakeEmptyBSTree(BSTreeNode **t)
{
	if ((*t) != NULL)
	{
		MakeEmptyBSTree(&(*t)->leftChild);
		MakeEmptyBSTree(&(*t)->rightChild);
		free((*t));
		(*t) = NULL;
	}
}

BOOL RemoveBSTree(BSTree *bst,T key)
{
	return RemoveBSTree(bst->root, key);
}
BOOL RemoveBSTree(BSTreeNode *&t, T key)
{
	if (t == NULL)
		return FALST;
	if (t->data > key)
		return RemoveBSTree(t->leftChild, key);
	else if (t->data < key)
		return RemoveBSTree(t->rightChild, key);
	else
	{
		BSTreeNode *p;
		//如果删除节点有左右子树,则删除该节点,下右子数中找最小节点代替
		if (t->leftChild != NULL && t->rightChild != NULL)
		{
			p = t->rightChild;
			while (p->leftChild != NULL)
				p = p->leftChild;
			t->data = p->data;
			//在这个子树中执行递归,知道转换为第二种情况h
			RemoveBSTree(t->rightChild, p->data);
		}
		else
		{
			p = t;
			if (t->leftChild == NULL)
				//这一句使得t的右子数指向t的父节点
				t = t->rightChild;
			else if (t->rightChild == NULL)
				t = t->leftChild;
			free(p);
			p = NULL;
		}
	}
	return TRUE;
}

#include"BSTree.h"

void main()
{
	BSTree bst;
	InitBSTree(&bst);
	T ar[] = { 45, 12, 53, 3, 37, 100, 24, 61, 90, 78 };
	int n = sizeof(ar) / sizeof(int);

	for (int i = 0; i < n; ++i)
	{
		InsertBSTree(&bst, ar[i]);
	}

	T min = Min(&bst);
	T max = Max(&bst);
	printf("min=%d,max=%d\n", min, max);
	Sort(&bst);
	RemoveBSTree(&bst, 37);
	printf("hello");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值