二叉排序树(BST)的构建与删除

二叉排序树(BST)的构建,插入,查找与删除

#include<iostream>
using namespace std;
#include<time.h>
#include<vector>

class bithrnode {
public:
	int data;
	bithrnode *lchild;
	bithrnode *rchild;
};
void  creat(bithrnode *	&T);	//创建
void insert(bithrnode * &bst, int n);//插入
void inordertraverse(bithrnode * bst);//中序遍历输出
bithrnode* find_BST(bithrnode *bst, int data);	//非递归查找
bithrnode* find_BST2(bithrnode *bst, int data);  //递归查找

bool deleteBST(bithrnode * &bst, int data);//删除二叉树
bool Delete(bithrnode * &bst);

bool destroy(bithrnode * bst);		//摧毁二叉排序树

//插入
void insert(bithrnode * &bst, int n)
{
	if (bst == NULL)			//指向的结点为空时,new一个新结点(叶子结点)
	{
		bst = new bithrnode;
		bst->data = n;
		bst->lchild = bst->rchild = NULL;
		return;
	}
	else if (n == bst->data)	//只能插入不相等的数
		return;
	else if (n < bst->data)
		insert(bst->lchild, n);
	else
		insert(bst->rchild, n);
}
//创建二叉排序树
void  creat(bithrnode *	&T)
{
	T = NULL;
	srand(time(NULL));
	vector<int> arr;		//声明一个数组
	cout << "输入的数为" << endl;
	for (int i = 0; i < 6; i++)
	{
		arr.push_back(rand() % 100 + 1);	//将数组随机赋值1-100。构建长度为6的随机数组
		cout << arr[i] << " ";
	}
	cout << endl;
	for (int j = 0; j < 6; j++)
	{
		insert(T, arr[j]);			//将arr数组的值插入到二叉排序树中
	}
}
//中序遍历输出
void inordertraverse(bithrnode * bst)
{
	if (bst == NULL)
		return;

	inordertraverse(bst->lchild);
	cout << bst->data << " ";
	inordertraverse(bst->rchild);

}
//非递归查找
bithrnode* find_BST(bithrnode *bst, int data)	
{
	bithrnode * p = bst;
	while (p != NULL && data != p->data)
	{
		if (data < p->data)
			p = p->lchild;
		else
			p = p->rchild;
	}
	return bst;
}
//递归查找
bithrnode* find_BST2(bithrnode *bst, int data)  
{
	if (bst == NULL || data == bst->data)	
		return bst;					//找到返回该结点
	
	else if (data < bst->data)		//小于结点数,去左子树查找
		find_BST2(bst->lchild, data);
	else
		find_BST2(bst->rchild, data);
}

bool Delete(bithrnode* &bst)
{
	bithrnode* p, *s;
	if (bst->lchild == NULL)		//左孩子为空
	{
		p = bst;					//临时变量保存要删除的结点地址
		bst = bst->rchild;			//直接指向右孩子
		delete(p);
	}
	else if (bst->rchild == NULL)	//右孩子为空
	{
		p = bst;
		bst = bst->lchild;			//直接指向左孩子
		delete(p);
	}
	else							//左右子树都不为空,找结点的前驱结点
	{
		p = bst;
		s = bst->lchild;			//转左,然后到最右的结点(待删除结点的前驱)
		while (s->rchild != NULL)
		{
			p = s;
			s = s->rchild;
		}
		bst->data = s->data;		//将待删结点的前驱的数值赋值给待删结点

		if (p != bst)				//p,bst指向不同,说明待删结点的左子树有右孩子
			p->rchild = s->lchild;	//重接bst的右子树
		else
			p->lchild = s->lchild;	//p=bst,说明待删结点的左子树没有右孩子。  /*** s->lchild接在双亲的右边还是左边,取决于所删结点的左子树有没有右孩子**/

		delete s;			//将待删结点的前驱数值给待删结点,最后释放的是直接前驱,并没有直接释放待删结点。拿了前驱的数据,最后还要让前驱替它释放,过河拆桥。
	}

	//else							//左右子树都不为空,找结点的后驱结点
	//{
	//p = bst;
	//s = bst->rchild;			//转右,然后到最左的结点(待删除结点的后驱)
	//while (s->lchild != NULL)
	//{
	//	p = s;
	//	s = s->lchild;
	//}
	//bst->data = s->data;		//将待删结点的后驱的数值赋值给待删结点

	//if (p != bst)				//p,bst指向不同,说明待删结点的右子树有左孩子
	//	p->lchild = s->rchild;	//重接bst的左子树
	//else
	//	p->rchild = s->rchild;	//p=bst,说明待删结点的右子树没有左孩子。 

	//delete s;			
	//}


	return true;
}

//删除二叉树
bool deleteBST(bithrnode * &bst, int data)//找到要删除的结点
{
	if (bst == NULL)
		return false;
	else
	{
		
		if (data == bst->data)
			return Delete(bst);			//删除结点
		else if (data < bst->data)
			return deleteBST(bst->lchild, data);//若小于当前结点的数值,则搜寻左子树,继续寻找
		else
			return deleteBST(bst->rchild, data);
	}
}


//摧毁二叉树        
bool destroy(bithrnode * bst)
{
	if (bst != NULL)	
	{								//必须后序遍历,不然无法根据根节点找到子节点
		destroy(bst->lchild);
		destroy(bst->rchild);
		delete bst;				
		return true;
	}
}

int main()
{
	bithrnode * tree;
	creat(tree);			//创建
	insert(tree, 50);		//插入数字2
	inordertraverse(tree);	//中序遍历输出
	cout << endl;
	 
	deleteBST(tree, 50);		//删除数字2
	inordertraverse(tree);	//中序遍历输出
	//cout<<find_BST2(tree, 2)->data;
	destroy(tree);
	system("pause");
	return 0;
}

二叉排序树的删除结点分为三种情况
(1)叶子结点
这种情况最简单,由于删除叶子结点不会破坏整棵树的结构,只需要修改其双亲结点的指针即可
在这里插入图片描述

(2)要删除的结点只求左子树或者右子树
相对而言也比较简单,只需要删除结点后,将它的左子树或右子树整个移动到删除节点的位置即可。
在这里插入图片描述

(3)左右子树都有的节点。
用删除节点的直接前驱或者直接后继来替换当前节点,调整直接前驱或者直接后继的位置
在这里插入图片描述

时间、空间复杂度:
最好情况:二叉排序树是平衡的,则n个节点的二叉排序树的高度为log2(n)+1,其查找效率为O(logn),近似于折半查找。
最坏情况:如果二叉排序树完全不平衡,一颗斜树则其深度可达到n,查找效率为O(n),退化为顺序查找。
一般的,二叉排序树的查找性能在到O(n)之间。因此,为了获得较好的查找性能,就要构造一棵平衡的二叉排序树。
ASL:
查找成功的平均查找长度(ASL):∑(本层高度本层元素个数)/节点总数
查找不成功的平均查找长度(ASL):∑(本层高度
本层补上的叶子个数)/补上的叶子总数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值