C++进阶----二叉搜索树

1、二叉搜索树概念

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

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树

                        二叉搜索树又称二叉排序树或者二叉查找树。

2、二叉搜索树操作

1. 二叉搜索树的查找
        a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
        b、最多查找高度次,走到到空,还没找到,这个值不存在。

2. 二叉搜索树的插入 插入的具体过程如下:
        a. 树为空,则直接新增节点,赋值给root指针
        b. 树不空,按二叉搜索树性质查找插入位置,插入新节点

3. 二叉搜索树的删除

首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情 况:

  • a. 要删除的结点无孩子结点
  • b. 要删除的结点只有左孩子结点
  • c. 要删除的结点只有右孩子结点
  • d. 要删除的结点有左、右孩子结点

看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程 如下:

  • 情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
  • 情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
  • 情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点 中,再来处理该结点的删除问题--替换法删除

3、二叉搜索树的实现

#pragma once
#include<iostream>
using namespace std;
template<class T>
struct BSTNode
{
	BSTNode(const T& data = T()) 
		:_pLeft(nullptr)
		,_pRight(nullptr)
		,_data(data)
	{

	}
	BSTNode<T>* _pLeft;
	BSTNode<T>* _pRight;
	T _data;
};

template<class T>
class BSTree 
{
	typedef BSTNode<T> Node;
	typedef Node* PNode;

public:
	BSTree() 
	:_proot(nullptr)
	{}
	~BSTree() {};
	PNode Find(const T& data) {
		PNode cur = _proot;
		while (cur) {
			if (cur->_data < data) {
				cur = cur->_pRight;
			}
			else if (cur->_data > data) {
				cur = cur->_pLeft;
			}
			else {
				return cur;
			}
		}
		return nullptr;
	}
	bool Insert(const T& data) {
		if (_proot == nullptr) {
			_proot = new Node(data);
			return true;
		}
		PNode cur = _proot;
		PNode parent = nullptr;
		while (cur) {
			parent = cur;
			if (data > cur->_data) {
				cur = cur->_pRight;
			}
			else if (data < cur->_data) {
				cur = cur->_pLeft;

			}
			else {
				return false;
			}
		}
		cur = new Node(data);
		if (parent->_data < data) {
			parent->_pRight = cur;
		}
		else {
			parent->_pLeft = cur;
		}
		return true;
	}
	bool Erase(const T& data) {
		PNode cur = nullptr;
		PNode parent = _proot;
		while (cur) {
			
			if (cur->_data < data) {
				parent = cur;
				cur = cur->_pRight;
			}
			else if (cur->_data > data) {
				parent = cur;
				cur = cur->_pLeft;
			}
			else {
				if (cur->_pLeft == nullptr) {
					if (cur == _proot) {
						_proot = cur->_pRight;
					}
					else {
						if (parent->_pLeft == cur) {
							parent->_pLeft = cur->_pRight;

						}
						else {
							parent->_pRight = cur->_pRight;
						}
					}
					
					delete cur;
				}
				else if (cur->_pRight == nullptr) {
					if (cur == _proot) {
						_proot = cur->_pRight;
					}
					else {
						if (parent->_pLeft == cur) {
							parent->_pLeft = cur->_pLeft;

						}
						else {
							parent->_pRight = cur->_pLeft;
						}
					}
					
					delete cur;
				}
				else {
					PNode minParent = cur;
					PNode minRight = cur->_pRight;
					while (minRight->_pLeft) {
						minParent = minRight;
						minRight = minRight->_pLeft;
					}
					swap(minRight->_data, cur->_data);
					if (minParent->_pLeft == minRight)
					{
						minParent->_pLeft = minRight->_pRight;
					}
					else
					{
						minParent->_pRight = minRight->_pRight;
					}

					delete minRight;
				}
				return true;
			}
		}
		return false;
	}
	void InOrder() {
		_InOrder(_proot);
		cout << endl;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_pLeft);
		cout << root->_data << " ";
		_InOrder(root->_pRight);
	}
	bool FindR(const T& data)
	{
		return _FindR(_proot, data);
	}

	bool InsertR(const T& data)
	{
		return _InsertR(_proot, data);
	}

	bool EraseR(const T& data)
	{
		return _EraseR(_proot, data);
	}

	private:
		bool _EraseR(Node*& root, const T& data)
		{
			if (root == nullptr)
				return false;

			if (root->_data < data)
			{
				return _EraseR(root->_pRight, data);
			}
			else if (root->_data > data)
			{
				return _EraseR(root->_pLeft, data);
			}
			else
			{
				Node* del = root;
				// 删除
				if (root->_pLeft == nullptr)
				{
					root = root->_pRight;
				}
				else if (root->_pRight == nullptr)
				{
					root = root->_pLeft;
				}
				else
				{
					Node* minRight = root->_pRight;
					while (minRight->_pLeft)
					{
						minRight = minRight->_pLeft;
					}

					swap(root->_data, minRight->_data);

					return _EraseR(root->_pRight, data);
				}

				delete del;
				return true;
			}
		}

		bool _InsertR(Node*& root, const T& data)
		{
			if (root == nullptr)
			{
				root = new Node(data);
				return true;
			}

			if (root->_data < data)
				return _InsertR(root->_pRight, data);
			else if (root->_data > data)
				return _InsertR(root->_pLeft, data);
			else
				return false;
		}

		bool _FindR(Node* root, const T& data)
		{
			if (root == nullptr)
				return false;

			if (root->_data < data)
			{
				return _FindR(root->_pRight, data);
			}
			else if (root->_data > data)
			{
				return _FindR(root->_pLeft, data);
			}
			else
			{
				return true;
			}
		}

private:
	PNode _proot = nullptr;

};

4 二叉搜索树的应用

1. K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到 的值。
比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:

  • 以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
  • 在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。

2. KV模型:每一个关键码key,都有与之对应的值Value,即的键值对。该种方 式在现实生活中非常常见:

  • 比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英 文单词与其对应的中文就构成一种键值对;
  • 再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出 现次数就是就构成一种键值对。

//kv模型
#include<iostream>
#include<string>
using namespace std;
namespace key_value
{
#pragma once

	template<class K, class V>
	struct BSTreeNode
	{
		BSTreeNode<K, V>* _left;
		BSTreeNode<K, V>* _right;

		const K _key;
		V _value;

		BSTreeNode(const K& key, const V& value)
			:_left(nullptr)
			, _right(nullptr)
			, _key(key)
			, _value(value)
		{}
	};

	template<class K, class V>
	class BSTree
	{
		typedef BSTreeNode<K, V> Node;
	public:

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

		///
		Node* FindR(const K& key)
		{
			return _FindR(_root, key);
		}

		bool InsertR(const K& key, const V& value)
		{
			return _InsertR(_root, key, value);
		}

		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}

	private:
		bool _EraseR(Node*& root, const K& key)
		{
			if (root == nullptr)
				return false;

			if (root->_key < key)
			{
				return _EraseR(root->_right, key);
			}
			else if (root->_key > key)
			{
				return _EraseR(root->_left, key);
			}
			else
			{
				Node* del = root;
				// 删除
				if (root->_left == nullptr)
				{
					root = root->_right;
				}
				else if (root->_right == nullptr)
				{
					root = root->_left;
				}
				else
				{
					Node* minRight = root->_right;
					while (minRight->_left)
					{
						minRight = minRight->_left;
					}

					swap(root->_key, minRight->_key);

					return _EraseR(root->_right, key);
				}

				delete del;
				return true;
			}
		}

		bool _InsertR(Node*& root, const K& key, const V& value)
		{
			if (root == nullptr)
			{
				root = new Node(key, value);
				return true;
			}

			if (root->_key < key)
				return _InsertR(root->_right, key, value);
			else if (root->_key > key)
				return _InsertR(root->_left, key, value);
			else
				return false;
		}

		Node* _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
				return nullptr;

			if (root->_key < key)
			{
				return _FindR(root->_right, key);
			}
			else if (root->_key > key)
			{
				return _FindR(root->_left, key);
			}
			else
			{
				return root;
			}
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;

			_InOrder(root->_left);
			cout << root->_key << ":" << root->_value << endl;
			_InOrder(root->_right);
		}
	private:
		Node* _root = nullptr;
	};
}

5 二叉搜索树的性能分析

插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。

对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二 叉搜索树的深度的函数,即结点越深,则比较次数越多。 但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:

最优情况下,二叉搜索树为完全二叉树(或者接近完全二叉树),其平均比较次数为:O(log n)

最差情况下,二叉搜索树退化为单支树(或者类似单支),其平均比较次数为:O(n)

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ღ星ღ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值