二叉搜索树的基本操作

定义

typedef int BSDataType;
typedef struct BSTreeNode
{
	struct BSTreeNode* pLeft;
	struct BSTreeNode* pRight;
	BSDataType data;
}BSTreeNode, *pBSTreeNode;

创建新结点

pBSTreeNode BSBuyNode(BSDataType d)
{
	pBSTreeNode newNode = (pBSTreeNode)malloc(sizeof(BSTreeNode));
	if (NULL == newNode)
	{
		assert(0);
	}
	newNode->data = d;
	newNode->pLeft = NULL;
	newNode->pRight = NULL;
	return newNode;
}

初始化

void InitBSTree(pBSTreeNode* bstree)
{
	assert(bstree);
	*bstree = NULL;
}

查找

非递归

pBSTreeNode FindBSTree(pBSTreeNode bstree, BSDataType d) 
{
	assert(bstree);
	pBSTreeNode cur = bstree;
	while (NULL != cur)
	{
		if (cur->data == d)
			return cur;
		else if (cur->data > d) //向左走
			cur = cur->pLeft;
		else//向右走
			cur = cur->pRight;
	}
	return NULL;
}

递归

pBSTreeNode FindBSTreeNor(pBSTreeNode bstree, BSDataType d) 
{
	if (NULL == bstree)
	{
		return NULL;
	}
	else if (bstree->data == d) //相等
	{
		return bstree;
	}
	else if (bstree->data > d) //左子树
	{
		return FindBSTreeNor(bstree->pLeft, d); //查找
	}
	else//右子树
	{
		return FindBSTreeNor(bstree->pRight, d); //查找
	}
}

插入

非递归

void InsertBSTree(pBSTreeNode* bstree, BSDataType d) 
{
	if (NULL == (*bstree)) //树为空,直接插入
	{
		*bstree = BSBuyNode(d);
	}
	else //树不为空
	{
		//查找插入的位置
		pBSTreeNode cur = *bstree;
		pBSTreeNode parent = NULL;
		while (NULL != cur)
		{
			parent = cur;//记录父母的位置,方便之后的插入
			if (cur->data > d) //向左走
				cur = cur->pLeft;
			else if (cur->data < d) //向右走
				cur = cur->pRight;
			else //相等则不需要插入
				return;
		}
		//插入结点
		cur = BSBuyNode(d);
		if (parent->data > d) //插入到左孩子
			parent->pLeft = cur;
		else//插入到右孩子
			parent->pRight = cur;
	}
}

递归

void InsertBSTreeNor(pBSTreeNode* bstree, BSDataType d) 
{
	if (NULL == (*bstree)) //树为空,直接插入
	{
		*bstree = BSBuyNode(d);
		return;
	}
	else if ((*bstree)->data > d) //插入到左子树
	{ 
		InsertBSTreeNor(&((*bstree)->pLeft), d);
	}
	else if ((*bstree)->data < d) //插入到右子树
	{
		InsertBSTreeNor(&((*bstree)->pRight), d);
	}
	else//相等
	{
		return;
	}
}

删除

非递归

void DeleteBSTree(pBSTreeNode* bstree, BSDataType d)
{
	if (NULL == (*bstree)) //树为空,直接返回
	{
		return;
	}

	//查找删除的位置
	pBSTreeNode cur = *bstree;
	pBSTreeNode parent = cur;
	while (d != cur->data)
	{
		parent = cur;//记录父母的位置,方便之后的插入
		if (cur->data > d) //向左走
			cur = cur->pLeft;
		else
			cur = cur->pRight;
	}
	if (NULL == cur) //找不到
		return;

	//删除结点cur
	//pBSTreeNode del = cur; //释放空间
	if ((NULL == cur->pLeft && NULL != cur->pRight) || 
		(NULL == cur->pLeft && NULL == cur->pRight))//只有右孩子或左右孩子不存在
	{
		if (cur == *bstree)//cur为根结点
		{
			*bstree = cur->pRight;//把cur的右子树作为根
		}
		else//cur不是根结点
		{
			if (cur == parent->pLeft) //cur为双亲的左子树
				parent->pLeft = cur->pRight;//把cur的右子树作为双亲的左子树
			else //cur为双亲的右子树
			parent->pRight = cur->pRight; //把cur的右子树作为双亲的右子树
		}
			
	}
	else if (NULL != cur->pLeft && NULL == cur->pRight)//只有左孩子
	{
		if (cur == *bstree)//cur为根结点
		{
			*bstree = cur->pLeft;//把cur的左子树作为根
		}
		else//cur不是根结点
		{
			if (cur == parent->pLeft) //cur为双亲的左子树
				parent->pLeft = cur->pLeft;//把cur的左子树作为双亲的左子树
			else //cur为双亲的右子树
				parent->pRight = cur->pLeft; //把cur的左子树作为双亲的右子树
		}
	}
	else if (NULL != cur->pLeft && NULL != cur->pRight)//左右孩子存在
	{
		//找到左子树中最大的结点或右子树中最小的结点
		pBSTreeNode str = cur->pLeft;
		//找左子树中最大的结点,即左子树中最右端的结点
		parent = cur;
		while (str->pRight)
		{
			parent = str;
			str = str->pRight;
		}
		//交换cur与str的值
		cur->data = str->data;
		//删除替代结点
		if (str == parent->pLeft) //str为双亲的左子树
			parent->pLeft = str->pLeft;//把str的左子树作为双亲的左子树
		else //cur为双亲的右子树
			parent->pRight = str->pRight; //把str的左子树作为双亲的右子树
		cur = str;
	}
	free(cur);
	cur = NULL;
}

递归

int DeleteBSTreeNor(pBSTreeNode* bstree, BSDataType d)
{
	if (NULL == (*bstree)) //树为空,直接返回
	{
		return -1;
	}
	if (d < (*bstree)->data)
		return  DeleteBSTreeNor(&((*bstree)->pLeft), d);  //进入左子树
	else if (d >(*bstree)->data)
		return DeleteBSTreeNor(&((*bstree)->pRight), d);  //进入右子树
	else
	{
		pBSTreeNode del = *bstree;
		if (NULL == del->pLeft)  //左子树为空   只有右孩子或左右孩子均不存在
		{
			*bstree = del->pRight;
			free(del);
		}
		else if (NULL == del->pRight)  //只有左孩子
		{
			*bstree = del->pLeft;
			free(del);
			return 1;  //需要有返回值
		}
		else//左右孩子均存在
		{
			pBSTreeNode pcur = del->pRight;
			while (pcur->pLeft)//找到左子树中最大的结点或右子树中最小的结点
			{
				pcur = pcur->pLeft;
			}
			del->data = pcur->data;  //替换
			return DeleteBSTreeNor(&((*bstree)->pRight), del->data);  //删除该节点右子树中的最左端的结点
		}
	}
}

销毁

void DestroyBSTree(pBSTreeNode* bstree)
{
	assert(bstree);
	if (*bstree)
	{
		DestroyBSTree(&((*bstree)->pLeft));
		DestroyBSTree(&((*bstree)->pRight));
		free(*bstree);
		*bstree = NULL;
	}
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值