二叉搜索树的查找与删除一些基本操作

二叉搜索树:它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。

二叉搜索树有个特点,最左的是最小的节点,最右的是最大的节点。 
当我们把二叉搜索树进行中序遍历的时候,它是进行排序后的结果,所以我们也把二叉搜索树叫做排序二叉树。


接下来我们介绍二叉排序树的所有的算法:

1.1搜索二叉树的结构

首先我们来看它的结构,他的结构和二叉树类似,所以会有一个左孩子节点的指针,一个右孩子节点的指针,还有一个key值(主键)和一个value(值),二叉树中key值和value值是相等的,我们用这四个属性来构建搜索二叉树,

template<typename K,typename V>
struct BSTNode
{
	BSTNode(const K & key,const V & value):_key(key),_value(value),_LChild(NULL),_RChild(NULL)
	{   }

	K _key;
	V _value;
	BSTNode<K,V>* _LChild;
	BSTNode<K,V>* _RChild;
};

1.2二叉树的构造函数

构建搜索二叉树,我们这里采用先构建一棵空树,利用Insert插入方法来插入元素。

public:
	//默认构造函数
	BinarySearchTree():pRoot(NULL) { }
	//拷贝构造函数
	BinarySearchTree(const BinarySearchTree<K,V> & Bst)
	{
		pRoot = _Copy(Bst.pRoot);
	}
private:
	BSTNode<K,V>* _Copy(BSTNode<K,V> *Root)
	{
		if (Root == NULL)
		{
			return NULL;
		}
		BSTNode<K,V> *ptemp = new BSTNode<K,V>(Root->_key,Root->_value);
		ptemp->_LChild = _Copy(Root->_LChild);
		ptemp->_RChild = _Copy(Root->_RChild);
		return ptemp;
	}

1.3二叉树的插入函数 


其搜索二叉树关键的算法之一,如果为空树,那么可以直接进行插入。他的用根节点的键值和key进行比较,如果key大于根节点键值,就说明在右子树如果小,那么就说明在右子树,如果相等,就说明已经出现了,不需要再次进行插入。插入的时候要看与父亲节点的key进行比较,如果小插入右边,如果大,插入左边。


插入非递归代码:

//非递归版本
	bool Insert(const K & key,const V & value)
	{
		if (pRoot == NULL)
		{
			pRoot = new BSTNode<K,V>(key,value);
			return true;
		}

		BSTNode<K,V> *pcur = pRoot;
		BSTNode<K,V> *pParent = NULL;
		//找到要插入的位置,发现其实插入的位置都是树的叶子节点。
		while (pcur)
		{
			if (key < pcur->_key)
			{
				pParent = pcur;
				pcur = pcur->_LChild;
			}
			else if(key > pcur->_key)
			{
				pParent = pcur;
				pcur = pcur->_RChild;
			}
			else
				return false;   //这个值在树里有,插入失败。
		}

		BSTNode<K,V> *pM = new BSTNode<K,V>(key,value);
		if (key < pParent->_key)  //插在左子树上。
		{
			pParent->_LChild = pM;
		}
		else
		{
			pParent->_RChild = pM;
		}
		return true;
	}
插入递归代码:

//递归的版本
	bool Insert(const K & key,const V & value)
	{	
		return _Insert(pRoot,key,value);
	}
bool _Insert(BSTNode<K,V> *&Root,const K & key, const V & value)
	{
		if (Root == NULL)
		{
			Root = new BSTNode<K,V>(key,value);
			return true;
		}

		if (key < Root->_key)
		{
			return _Insert(Root->_LChild,key,value);
		}
		else if(key > Root->_key)
			return _Insert(Root->_RChild,key,value);
		else
			return false;
	}

1.4搜索二叉树的查找

搜索二叉树的查找,下面进行简单的介绍一下,你要搜索的,那么就是和根的key比较,如果大于根的key,那么就找右树,如果小于根的key,那么就找左树。相等,就代表找到了。
二叉搜索树的查找非递归代码:
//非递归版本
	bool Find(const K & key)
	{
		if (pRoot == NULL)
		{
			return false;
		}
		BSTNode<K,V> *pcur = pRoot;

		while (pcur)
		{
			if (pcur->_key == key)
			{
				return true;
			}
			else if (key < pcur->_key)
			{
				pcur = pcur->_LChild;
			}
			else
			{
				pcur = pcur->_RChild;
			}
		}
		return false;
	}
二叉树的递归查找代码:
bool Find(const K & key)
	{
		return _Find(pRoot,key);
	}
bool _Find(BSTNode<K,V>*Root,const K & key)
	{
		if (Root == NULL)
		{
			return false;
		}

		if (Root->_key == key)
		{
			return true;
		}
		else if (key < Root->_key)
		{
			return _Find(Root->_LChild,key);
		}
		else 
			return _Find(Root->_RChild,key);
		
	}

1.5搜索二叉树的删除

搜索二叉树的删除算法算是最难的了。在这里我们需要考虑几种思路。
1.根为空
2.只有根节点且根节点就是我们要删除的节点。
3.只有左单支
4.只有右单支
5.要删除的节点既有左孩子又有右孩子

1和2好理解,就不多说了,重点说说3,4,5三种情况。

只有左单支(删除的三种情况)

只有右单支(删除的三种情况)




删除的节点既有左单支又有右单支




删除节点代码:

bool Remove(const K & key)
	{
		if (pRoot == NULL)
		{
			return false;
		}

		if (pRoot->_LChild == NULL && pRoot->_RChild == NULL && pRoot->_key == key)
		{
			delete pRoot;
			pRoot = NULL;
		}

		BSTNode<K,V> *pcur = pRoot;
		BSTNode<K,V> *pParent = NULL;
		//在树中找这个节点
		while (pcur)
		{
			if (key < pcur->_key)
			{
				pParent = pcur;
				pcur = pcur->_LChild;
			}
			else if (key > pcur->_key)
			{
				pParent = pcur;
				pcur = pcur->_RChild;
			}
			else 
				break;
		}

		if (pcur)  //找到了。
		{
			if (pcur->_LChild == NULL)  //右单支
			{
				if (pParent != NULL)  //删除的不是根节点
				{
					if (pcur == pParent->_RChild)
					{
						pParent->_RChild = pcur->_RChild;
					}
					else
						pParent->_LChild = pcur->_RChild;
				}
				else  //删除的是根节点,此时pParent为空,所以这里要单独的拿出来写,要不然程序直接崩溃。
				{
					pRoot = pcur->_LChild;
				}

			}
			else if (pcur->_RChild == NULL) //左单支
			{
				if (pParent != NULL)
				{
					if (pcur == pParent->_LChild)
					{
						pParent->_LChild = pcur->_LChild;
					}
					else
						pParent->_RChild = pcur->_LChild;
				}
				else
					pRoot = pcur->_RChild;

			}
			else  //要删除的节点左右子树都有  将右链的最左链的节点代替当前的节点,删除最左链的那个节点。
			{
				BSTNode<K,V> *firstInOrder = pcur->_RChild;

				while (firstInOrder->_LChild)
				{
					pParent = firstInOrder;
					firstInOrder = firstInOrder->_LChild;
				}

				pcur->_key = firstInOrder->_key;
				pcur->_value = firstInOrder->_value;

				if (pcur->_RChild != firstInOrder)  //右子树的左链不为空
				{
					pParent->_LChild = firstInOrder->_RChild;
				}
				else   //右子树的左链为空。
				{
					pcur->_RChild = firstInOrder->_RChild;
				}

				pcur = firstInOrder;  //pcur指向要删除的节点

			}

			delete pcur;
			return true;
		}


		return false;  //没找到直接返回false.
	}

整个代码如下:

#pragma  once
#include<iostream>
using namespace std;

template<typename K,typename V>
struct BSTNode
{
	BSTNode(const K & key,const V & value):_key(key),_value(value),_LChild(NULL),_RChild(NULL)
	{   }

	K _key;
	V _value;
	BSTNode<K,V>* _LChild;
	BSTNode<K,V>* _RChild;
};

template<typename K,typename V>
class BinarySearchTree
{
public:
	//默认构造函数
	BinarySearchTree():pRoot(NULL) { }
	//拷贝构造函数
	BinarySearchTree(const BinarySearchTree<K,V> & Bst)
	{
		pRoot = _Copy(Bst.pRoot);
	}

	BinarySearchTree& operator=(const BinarySearchTree<K,V> & Bst)
	{
		if (this != &Bst)
		{
			BinarySearchTree<K,V> ptemp(Bst);
			swap(pRoot,ptemp.pRoot);
		}
		return *this;
	}

	~BinarySearchTree()
	{
		_Destroy(pRoot);
	}



	非递归版本
	//bool Insert(const K & key,const V & value)
	//{
	//	if (pRoot == NULL)
	//	{
	//		pRoot = new BSTNode<K,V>(key,value);
	//		return true;
	//	}

	//	BSTNode<K,V> *pcur = pRoot;
	//	BSTNode<K,V> *pParent = NULL;
	//	//找到要插入的位置,发现其实插入的位置都是树的叶子节点。
	//	while (pcur)
	//	{
	//		if (key < pcur->_key)
	//		{
	//			pParent = pcur;
	//			pcur = pcur->_LChild;
	//		}
	//		else if(key > pcur->_key)
	//		{
	//			pParent = pcur;
	//			pcur = pcur->_RChild;
	//		}
	//		else
	//			return false;   //这个值在树里有,插入失败。
	//	}

	//	BSTNode<K,V> *pM = new BSTNode<K,V>(key,value);
	//	if (key < pParent->_key)  //插在左子树上。
	//	{
	//		pParent->_LChild = pM;
	//	}
	//	else
	//	{
	//		pParent->_RChild = pM;
	//	}
	//	return true;
	//}

	//递归的版本
	bool Insert(const K & key,const V & value)
	{	
		return _Insert(pRoot,key,value);
	}

	非递归版本
	//bool Find(const K & key)
	//{
	//	if (pRoot == NULL)
	//	{
	//		return false;
	//	}
	//	BSTNode<K,V> *pcur = pRoot;

	//	while (pcur)
	//	{
	//		if (pcur->_key == key)
	//		{
	//			return true;
	//		}
	//		else if (key < pcur->_key)
	//		{
	//			pcur = pcur->_LChild;
	//		}
	//		else
	//		{
	//			pcur = pcur->_RChild;
	//		}
	//	}
	//	return false;
	//}

	bool Find(const K & key)
	{
		return _Find(pRoot,key);
	}


	bool Remove(const K & key)
	{
		if (pRoot == NULL)
		{
			return false;
		}

		if (pRoot->_LChild == NULL && pRoot->_RChild == NULL && pRoot->_key == key)
		{
			delete pRoot;
			pRoot = NULL;
		}

		BSTNode<K,V> *pcur = pRoot;
		BSTNode<K,V> *pParent = NULL;
		//在树中找这个节点
		while (pcur)
		{
			if (key < pcur->_key)
			{
				pParent = pcur;
				pcur = pcur->_LChild;
			}
			else if (key > pcur->_key)
			{
				pParent = pcur;
				pcur = pcur->_RChild;
			}
			else 
				break;
		}

		if (pcur)  //找到了。
		{
			if (pcur->_LChild == NULL)  //右单支
			{
				if (pParent != NULL)  //删除的不是根节点
				{
					if (pcur == pParent->_RChild)
					{
						pParent->_RChild = pcur->_RChild;
					}
					else
						pParent->_LChild = pcur->_RChild;
				}
				else  //删除的是根节点,此时pParent为空,所以这里要单独的拿出来写,要不然程序直接崩溃。
				{
					pRoot = pcur->_LChild;
				}

			}
			else if (pcur->_RChild == NULL) //左单支
			{
				if (pParent != NULL)
				{
					if (pcur == pParent->_LChild)
					{
						pParent->_LChild = pcur->_LChild;
					}
					else
						pParent->_RChild = pcur->_LChild;
				}
				else
					pRoot = pcur->_RChild;

			}
			else  //要删除的节点左右子树都有  将右链的最左链的节点代替当前的节点,删除最左链的那个节点。
			{
				BSTNode<K,V> *firstInOrder = pcur->_RChild;

				while (firstInOrder->_LChild)
				{
					pParent = firstInOrder;
					firstInOrder = firstInOrder->_LChild;
				}

				pcur->_key = firstInOrder->_key;
				pcur->_value = firstInOrder->_value;

				if (pcur->_RChild != firstInOrder)  //右子树的左链不为空
				{
					pParent->_LChild = firstInOrder->_RChild;
				}
				else   //右子树的左链为空。
				{
					pcur->_RChild = firstInOrder->_RChild;
				}

				pcur = firstInOrder;  //pcur指向要删除的节点

			}

			delete pcur;
			return true;
		}


		return false;  //没找到直接返回false.
	}
private:
	BSTNode<K,V>* _Copy(BSTNode<K,V> *Root)
	{
		if (Root == NULL)
		{
			return NULL;
		}
		BSTNode<K,V> *ptemp = new BSTNode<K,V>(Root->_key,Root->_value);
		ptemp->_LChild = _Copy(Root->_LChild);
		ptemp->_RChild = _Copy(Root->_RChild);
		return ptemp;
	}

	void _Destroy(BSTNode<K,V> *Root)
	{
		if (Root == NULL)
		{
			return;
		}
		_Destroy(Root->_LChild);
		_Destroy(Root->_RChild);
		delete Root;
		Root = NULL;
	}

	bool _Insert(BSTNode<K,V> *&Root,const K & key, const V & value)
	{
		if (Root == NULL)
		{
			Root = new BSTNode<K,V>(key,value);
			return true;
		}

		if (key < Root->_key)
		{
			return _Insert(Root->_LChild,key,value);
		}
		else if(key > Root->_key)
			return _Insert(Root->_RChild,key,value);
		else
			return false;
	}

	bool _Find(BSTNode<K,V>*Root,const K & key)
	{
		if (Root == NULL)
		{
			return false;
		}

		if (Root->_key == key)
		{
			return true;
		}
		else if (key < Root->_key)
		{
			return _Find(Root->_LChild,key);
		}
		else 
			return _Find(Root->_RChild,key);
		
	}


private:
	BSTNode<K,V> *pRoot;
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值