(二叉树)3. 二叉排序树结点的删除

一、引言

上一篇文章:
(二叉树)2. 查找二叉排序树的最大、最小值及查找指定结点前驱、后继结点

由于避免篇幅过长,将二叉排序树建立、[先序、中序、后序]递归与非递归遍历、查找最大、最小值以及查找指定结点的前驱、后继结点、删除指定结点所有代码新放一个页面,下面是地址:
二叉排序树完整代码

本文实现二叉排序树的删除,并且不会“缩略代码”秀技术。
另外因为二叉排序树结点的删除要考虑的情况比较多,所以在最后设计了一个测试程序,输入规模n,插入[0, n)中所有整数,然后测试删除每一个结点的正确性,为了保证数据较充分,会对这n个数进行全排列然后再进行测试。

例如:
输入2


测试用例1:
插入:0,1
测试删除0、1的正确性


测试用例2:
插入1,0
测试删除1,0的正确性


如果n*n!个数据全部通过测试则程序没有问题

二、内容

1. 简述

流程图:
删除结点流程图

2. 代码

因为流程图+代码中的注释已经解释的够详细了,这里就不赘述了。

2.1 主代码

//删除结点
BiTree* deleteNode(BiTree* root, int value) {
	BiTree* node = findNode(root, value);																						//首先查找到要删除的结点
	//第一种情况:如果要删除的结点是叶子结点
	if (node->left == NULL && node->right == NULL) {
		if (!node->parent) {																										//要删除的是根
			delete node;
			return NULL;
		}
		if (node == node->parent->left)																						//不是根
			node->parent->left = NULL;
		else
			node->parent->right = NULL;
		delete node;
		return root;
	}
	//第二种情况:如果要删除的结点左右子树有一个为空
	if (node->left == NULL || node->right == NULL) {
		if (!node->parent) {																										//如果是根
			BiTree* temp = NULL;
			if (node->left) 
				temp = node->left;
			else 
				temp = node->right;
			temp->parent = NULL;
			delete node;
			return temp;
		}																																//如果不是根
		if (node->left == NULL) {																								//1. 如果要删除结点左子树为空
			node->right->parent = node->parent;
			if (node->parent->left == node) 																					//	a. 要删除结点是父结点的左子树
				node->parent->left = node->right;
			else 																														//	b. 要删除结点是父结点的右子树
				node->parent->right = node->right;
			delete node;
		}
		else {																															//2. 要删除结点右子树为空
			node->left->parent = node->parent;
			if (node->parent->left == node) 																					//	a. 要删除结点是父结点的左子树
				node->parent->left = node->left;
			else 																														//	b. 要删除结点是父结点的右子树
				node->parent->right = node->left;
			delete node;
		}
		return root;
	}
	//第三种情况:左右子树都不为空
	/*
	方法:
	1. 找出要删除结点的后继结点
	2. 处理后继结点的问题
	3. 用后继结点替换本身
	*/
	BiTree* successor = successorNode(root, value);																			//1. 查找后继结点
	/*																																	//2. 处理后继结点
	说明:
	1. 其后继结点一定只有右子树或者没有子树。
	2. node的后继结点只有两种情况:
		a. node后继结点为他的右子树根结点(右子树根结点没有向左分叉)
		b. 其余情况
			对于其他情况来说,node的后继结点一定没有左子树且node一定是它的parent的左子树
	*/
	if (successor->right) {																											//后继结点有右子树
		successor->right->parent = successor->parent;
		if (successor == successor->parent->right)																			//后继结点情况a
			successor->parent->right = successor->right;
		else																															//后继结点情况b
			successor->parent->left = successor->right;
	}
	else{																																//后继结点无右子树
		if (successor == successor->parent->right)																			//后继结点情况a
			successor->parent->right = NULL;
		else																															//后继结点情况b
			successor->parent->left = NULL;
	}
	successor->parent = node->parent;																						//3. 下面进行替换操作
	successor->left = node->left;
	successor->right = node->right;
	node->left->parent = successor;
	if (node->right)																													//如果是后继结点情况a,那么在上面处理后继节点的时候就已经使node->right = NULL 所以这里需要判断一下
		node->right->parent = successor;
	if (node->parent) {																											//如果node不是根
		if (node->parent->left == node)
			 node->parent->left = successor;
		else
			node->parent->right = successor;
	}
	else
		root = successor;																											//如果node是根
	return root;
}

2.2 测试工具代码:

2.2.1 工具函数名:
//测试工具,Perm:生成全排列,createBiNodeTest:有参数形式创建结点函数
void swap(int& a, int& b);
void Perm(vector<int> vec, int b, int e);
BiTree* createBiNodeTest(int value);
int factorial(int n);
void testDelete();	
2.2.2 测试工具实现:
//测试工具函数
void swap(int& c1, int& c2)
{
	char temp;
	temp = c1;
	c1 = c2;
	c2 = temp;
}
BiTree* createBiNodeTest(int value) {
	BiTree* node = new BiTree;
	node->left = node->right = node->parent = NULL;
	node->key = value;
	return node;
}
void Perm(vector<int> p, int b, int e)
{
	static int num = 1;
	if (b == e) {
		BiTree* testRoot = NULL;
		for (int i = 0; i <= e; i++) {
			BiTree* temp = createBiNodeTest(p[i]);
			Insert(testRoot, temp);
		}
		cout << "--- 测试组" << num++ << "---" << endl;
		cout << "插入顺序:";
		for (int i = 0; i <= e; i++)
			cout << p[i] << " ";
		cout << endl << endl;
		for (int i = 0; i <= e; i++) {
			testRoot = deleteNode(testRoot, p[i]);
			cout << "删除" << p[i] << "后的数据";
			midTrav(testRoot);
			cout << endl;
			
			testRoot = NULL;
			for (int j = 0; j <= e; j++) {
				BiTree* temp = createBiNodeTest(p[j]);
				Insert(testRoot, temp);
			}
		}
		cout << "--- 测试组 ---" << endl << endl;
		//Sleep(100);
	}
	else
	{
		for (int i = b; i <= e; i++)
		{
			swap(p[b], p[i]);
			Perm(p, b + 1, e);
			swap(p[b], p[i]);
		}
	}
}
int factorial(int n) {
	if (n == 1)
		return 1;
	return n * factorial(n - 1);
}
void testDelete() {
	//测试删除:
	int n;
	cout << "接下来生成由[0, n - 1)中整数构成的测试用例,将这n个数的全排列中每一种排列数都作为生成二叉排序树的新方式,并测试删除这一种排列方式中的每一个数据。总测试数应为n * n!。" << endl << endl;
	cout << "输入n:";
	cin >> n;
	cout << "--------------------------------------------------------------" << endl;
	cout << "\t\t\t" << "下面测试删除结点正确性" << endl;
	vector<int> p;
	for (int i = 0; i < n; i++)
		p.push_back(i);
	Perm(p, 0, n - 1);
	cout << "共有 " << n <<"! = " << factorial(n) << " 组测试数据进行测试,总测试通过组数为 " << n << " * " << n << "! = " <<  factorial(n) * n << "组。" << endl;
	cout << "--- 测试通过!---" << endl;
	cout << "--------------------------------------------------------------" << endl;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值