二叉搜索树

二叉搜索树概念

二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树
    在这里插入图片描述
    由图可以看出:二叉搜索树最左边的数一定是最小的,最右边的数,一定是最大的。
二叉搜索树操作
2. 1 二叉搜索树的查找

在这里插入图片描述
若根节点不为空,
1.若根节点的data == 要查找的树data 则反回true;
2.若根节点的data > 要找的data, 则在其左子树查找;
3.若根节点的data < 要找的data, 则在其右子树查找;
否则:要找的数不在二叉搜索树中,返回false。

2.2 二叉搜索树的插入

插入的具体过程如下:
a. 树为空,则直接插入。
在这里插入图片描述
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点。
在这里插入图片描述

2.3 二叉搜索树的删除

在删除前,我们要查看一下,要删除的元素在二叉搜索树中有没有,如果没有,则直接返回false,如果存在,则分为以下四种情况:
a. 要删除的结点无孩子结点;
b. 要删除的结点只有左孩子结点;
c. 要删除的结点只有右孩子结点;
d. 要删除的结点既有左孩子,又有右孩子。

以上的情况我们可以将a与b,c 情况结合一下,则删除方法:
b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点。
c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点
d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题。(相当于交换要删除的节点,换到右子树中最左边,就与情况b,c相同,可以直接删了)。

2.4 二叉树性能分析

插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
在这里插入图片描述
最优情况下,二叉搜索树为完全二叉树,时间复杂度为O(log N);
最差情况下,二叉搜索树退化为单支树,时间复杂度为O(N);
如果给的数据趋近于有序或该数据本就有序,二叉搜索树的性能就失去了。

#include<iostream>
using namespace std;

template<class T>
struct BSTreeNode
{
	BSTreeNode(const T& data = T())
		:_pLeft(nullptr)
		,_pRight(nullptr)
		,_data(data)
	{}
	BSTreeNode<T>* _pLeft;
	BSTreeNode<T>* _pRight;
	T _data;
};
template<class T>
class BSTree
{
	typedef BSTreeNode<T> Node;
public:
	BSTree()
		:_pRoot(nullptr)
	{}
	bool Insert(const T& data)
	{
		if (nullptr == _pRoot)
		{
			_pRoot = new Node(data);
		}
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur)
		{
			//保存母亲节点,最后会插入母亲节点的左右。
			pParent = pCur;
			if (data < pCur->_data)
				pCur = pCur->_pLeft;
			else if (data > pCur->_data)
				pCur = pCur->_pRight;
			else
				return false;
		}
		pCur = new Node(data);
		if (data < pParent->_data)
			pParent->_pLeft = pCur;
		else
			pParent->_pRight = pCur;

		return true;
	}
	bool Delete(const T& data)
	{
		if (nullptr == _pRoot)
			return false;
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur)
		{
			if (data == pCur->_data)
				break;
			else if (data < pCur->_data)
			{
				pParent = pCur;
				pCur = pCur->_pLeft;
			}
			else
			{
				pParent = pCur;
				pCur = pCur->_pRight;
			}
		}
		//判断是否找到节点
		if (nullptr == pCur)
			return false;
		//找到要删除的节点
		//要删除的节点只有右孩子
		if (nullptr == pCur->_pLeft)
		{
			//删除节点为根节点
			if (nullptr == pParent)
				_pRoot = pCur->_pRight;
			else
			{
				if (pCur == pParent->_pLeft)
					pParent->_pLeft = pCur->_pRight;
				else
					pParent->_pRight = pCur->_pRight;
			}
		}
		//删除的节点只有左孩子
		else if (nullptr == pCur->_pRight)
		{//删除节点为根节点
			if (nullptr == pParent)
				_pRoot = pCur->_pLeft;
			else
			{
				if (pCur == pParent->_pLeft)
					pParent->_pLeft = pCur->_pLeft;
				else
					pParent->_pRight = pCur->_pLeft;
			}
		}
		else
			//要删除的节点左右孩子都有
		{
			//找替代节点
			Node* pDelNode = pCur->_pRight;
			pParent = pCur;
			//在右子树中找最小的(即右子树中最小的)
			while (pDelNode->_pLeft)
			{
				pParent = pDelNode;
				pDelNode = pDelNode->_pLeft;
			}
			pCur->_data = pDelNode->_data;
			if (pDelNode == pParent->_pLeft)
				pParent->_pLeft = pDelNode->_pRight;
			else
				pParent->_pRight = pDelNode->_pRight;

			pCur = pDelNode;	
		}
		delete pCur;
		return true;
	}
	Node* Find(const T& data)
	{
		Node* pCur = _pRoot;
		while (pCur)
		{
			if (data == pCur->_data)
				return pCur;
			else if (data > pCur->_data)
				pCur = pCur->_pRight;
			else
				pCur = pCur->_pLeft;
		}
		return nullptr;
	}
	Node* LeftMost()
	{
		if (nullptr == _pRoot)
			return nullptr;
		Node* pCur = _pRoot;
		while (pCur->_pLeft)
			pCur = pCur->_pLeft;

		return pCur;
	}
	Node* RightMost()
	{
		if (nullptr == _pRoot)
			return nullptr;
		Node* pCur = _pRoot;
		while (pCur->_pRight)
			pCur = pCur->_pRight;

		return pCur;
	}
	void InOrder()
	{
		_InOrder(_pRoot);
	}
private:
	void _InOrder(Node* pRoot)
	{
		if (pRoot)
		{
			_InOrder(pRoot->_pLeft);
			cout << pRoot->_data << " ";
			_InOrder(pRoot->_pRight);
		}
	}
private:
	Node* _pRoot;
};

void TestBSTree()
{
	int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
	BSTree<int> bst;
	for (auto e : a)
	{
		bst.Insert(e);
	}
	cout << bst.LeftMost()->_data << endl;
	cout << bst.RightMost()->_data << endl;
	bst.InOrder();
	cout << endl;
	cout << bst.Find(5)->_data << endl;
}
int main()
{
	TestBSTree();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值