c++二插搜索树

1二插搜索树的概念

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

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

image-20220513124614728

2二插搜索树的实现

#pragma once
#include <iostream>
using std::cout;
template <class K>
struct BSTreeNode
{
	K _key;
	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;

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

template <class K>
class BSTree
{
public:
	typedef BSTreeNode<K> Node;
	BSTree()
		:_root(nullptr)
	{}
	~BSTree()
	{
		Destroy(_root);
		_root = nullptr;
	}
	BSTree(const BSTree<K>& t)
	{
		_root = CopyTree(t._root);
	}
	BSTree<K>& operator=(BSTree<K> t)
	{
		std::swap(_root, t._root);
		return *this;
	}

	bool Insert(const K& key) 
	{
		//今天我们实现的二叉搜索树不允许插入同一个值
		if (_root == nullptr)
		{
			//第一次插入数据
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			parent = cur;
			if (cur->_key > key) 
			{
				cur = cur->_left;
			}
			else if (cur->_key < key) 
			{
				cur = cur->_right;
			}
			else//树中存在相同的值
				return false;
		}
		cur = new Node(key);
		//判断我们插入的节点是在parent的左还是右
		if (parent->_key > key)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		return true;
	}
	bool Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	bool Erase(const K& key)
	{
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				//找到了 处理删除的细节
				//被删除的节点右四种情况
				//1.左右孩子都为空(叶子节点) 2. 左为空右不为空  3.右为空左不为空  4.左右都不为空
				if (cur->_left == nullptr)
				{
					//这里我们可以处理情况1和2,让父亲指向cur的右
					if (parent->_left == cur)
					{
						parent->_left = cur->_right;
					}
					else
					{
						parent->_right = cur->_right;
					}
					delete cur;
					return true;
				}
				else if (cur->_right == nullptr)
				{
					//这里我们可以处理情况3,让父亲指向cur的左
					if (parent->_left == cur)
					{
						parent->_left = cur->_left;
					}
					else
					{
						parent->_right = cur->_left;
					}
					delete cur;
					return true;
				}
				else
				{
					//情况4,左右都不为空,这种情况我们可以将被删节点和其右子树的最左节点(或左子树的最右节点互换位置)
					//互换之后删除节点,不会破坏搜索树的结构
					//这里我们以右子树的最左节点互换为例
					Node* minRight = cur->_right;
					Node* minParent = cur;
					while (minRight->_left)
					{
						minParent = minRight;
						minRight = minRight->_left;
					}
					std::swap(cur->_key, minRight->_key);

					//当然我们也可以选择直接覆盖被删节点
					//cur->_key = minRight->_key;

					//然后删除minRight节点即可
					//注意这里一定不能再次递归调用Erase(key),因为互换位置之后搜索树结构已经被破坏
					if (minParent->_left == minRight)
					{
						minParent->_left = minRight->_right;
					}
					else
					{
						minParent->_right = minRight->_right;
					}
					delete minRight;
					return true;
				}
			}

		}
		//没找到
		return false;
	}

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

	void InOrder()
	{
		_InOrder(_root);
	}
private:
	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}
	Node* CopyTree(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* copyNode = new Node(root->_key);
		copyNode->_left = CopyTree(root->_left);
		copyNode->_right = CopyTree(root->_right);
		return copyNode;
	}
	bool _EraseR(Node*& root, const K& key)
	{
		if (root == nullptr)//不存在
			return false;
		if (root->_key > key)
		{
			_EraseR(root->_left, key);
		}
		else if (root->_key < key)
		{
			_EraseR(root->_right, key);
		}
		else
		{
			//找到了该被删除的值
			Node* del = root;
			if (root->_left == nullptr)
			{
				//我们传参的时候传的是Node*& root,因此root不仅指向被删除的值
				//root还恰好是root的父亲节点指向root的那个指针
				//我们只需改变root的指向即可
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* minRight = root->_right;
				while (minRight->_left)
				{
					minRight = minRight->_left;
				}
				std::swap(minRight->_key, root->_key);
				return _EraseR(root->_right, key);//这次递归调用最终一定会走到左为空的情况
			}
			delete del;
			return true;
		}

	}
	bool _FindR(Node* root, const K& key)
	{
		if (root == nullptr)
			return false;
		if (root->_key > key)
		{
			_FindR(root->_left, key);
		}
		else if (root->_key < key)
		{
			_FindR(root->_right, key);
		}
		else
			return true;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}


private:
	Node* _root;
};

3.二插搜索树的应用

  1. K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。
    比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:
    以单词集合中的每个单词作为key,构建一棵二叉搜索树
    在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
  2. KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对;再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word,count>就构成一种键值对。
    比如:实现一个简单的英汉词典dict,可以通过英文找到与其对应的中文,具体实现方式如下:
    <单词,中文含义>为键值对构造二叉搜索树,注意:二叉搜索树需要比较,键值对比较时只比较Key查询英文单词时,只需给出英文单词,就可快速找到与其对应的key

现在我们将上面的搜索二叉树改成kv模型

#pragma once
#include <iostream>
using std::cout;
template <class K, class V>
struct BSTreeNode
{
	K _key;
	V _val;
	BSTreeNode<K, V>* _left;
	BSTreeNode<K, V>* _right;

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

template <class K, class V>
class BSTree
{
public:
	typedef BSTreeNode<K, V> Node;
	BSTree()
		:_root(nullptr)
	{}
	~BSTree()
	{
		Destroy(_root);
		_root = nullptr;
	}
	BSTree(const BSTree<K, V>& t)
	{
		_root = CopyTree(t._root);
	}
	BSTree<K, V>& operator=(BSTree<K, V> t)
	{
		std::swap(_root, t._root);
		return *this;
	}

	bool Insert(const K& key, const V& val)
	{
		//今天我们实现的二叉搜索树不允许插入同一个值
		if (_root == nullptr)
		{
			//第一次插入数据
			_root = new Node(key, val);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			parent = cur;
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else//树中存在相同的值
				return false;
		}
		cur = new Node(key, val);
		//判断我们插入的节点是在parent的左还是右
		if (parent->_key > key)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		return true;
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

	bool Erase(const K& key)
	{
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{

			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				//找到了 处理删除的细节
				//被删除的节点右四种情况
				//1.左右孩子都为空(叶子节点) 2. 左为空右不为空  3.右为空左不为空  4.左右都不为空
				if (cur->_left == nullptr)
				{
					//这里我们可以处理情况1和2,让父亲指向cur的右
					if (parent->_left == cur)
					{
						parent->_left = cur->_right;
					}
					else
					{
						parent->_right = cur->_right;
					}
					delete cur;
					return true;
				}
				else if (cur->_right == nullptr)
				{
					//这里我们可以处理情况3,让父亲指向cur的左
					if (parent->_left == cur)
					{
						parent->_left = cur->_left;
					}
					else
					{
						parent->_right = cur->_left;
					}
					delete cur;
					return true;
				}
				else
				{
					//情况4,左右都不为空,这种情况我们可以将被删节点和其右子树的最左节点(或左子树的最右节点互换位置)
					//互换之后删除节点,不会破坏搜索树的结构
					//这里我们以右子树的最左节点互换为例
					Node* minRight = cur->_right;
					Node* minParent = cur;
					while (minRight->_left)
					{
						minParent = minRight;
						minRight = minRight->_left;
					}
					std::swap(cur->_key, minRight->_key);

					//当然我们也可以选择直接覆盖被删节点
					//cur->_key = minRight->_key;

					//然后删除minRight节点即可
					//注意这里一定不能再次递归调用Erase(key),因为互换位置之后搜索树结构已经被破坏
					if (minParent->_left == minRight)
					{
						minParent->_left = minRight->_right;
					}
					else
					{
						minParent->_right = minRight->_right;
					}
					delete minRight;
					return true;
				}
			}

		}
		//没找到
		return false;
	}

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

	void InOrder()
	{
		_InOrder(_root);
	}
private:
	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}
	Node* CopyTree(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* copyNode = new Node(root->_key, root->_val);
		copyNode->_left = CopyTree(root->_left);
		copyNode->_right = CopyTree(root->_right);
		return copyNode;
	}
	bool _EraseR(Node*& root, const K& key)
	{
		if (root == nullptr)//不存在
			return false;
		if (root->_key > key)
		{
			_EraseR(root->_left, key);
		}
		else if (root->_key < key)
		{
			_EraseR(root->_right, key);
		}
		else
		{
			//找到了该被删除的值
			Node* del = root;
			if (root->_left == nullptr)
			{
				//我们传参的时候传的是Node*& root,因此root不仅指向被删除的值
				//root还恰好是root的父亲节点指向root的那个指针
				//我们只需改变root的指向即可
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* minRight = root->_right;
				while (minRight->_left)
				{
					minRight = minRight->_left;
				}
				std::swap(minRight->_key, root->_key);
				return _EraseR(root->_right, key);//这次递归调用最终一定会走到左为空的情况
			}
			delete del;
			return true;
		}

	}
	Node* _FindR(Node* root, const K& key)
	{
		if (root == nullptr)
			return nullptr;
		if (root->_key > key)
		{
			_FindR(root->_left, key);
		}
		else if (root->_key < key)
		{
			_FindR(root->_right, key);
		}
		else
			return root;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}


private:
	Node* _root;
};

现在我们实现一个简易字典试试

#include "BinarySearchTree of KV.hpp"
#include <string>
using namespace std;
int main()
{
	BSTree<string, string> dict;
	dict.Insert("hello", "你好");
	dict.Insert("apple", "苹果");
	dict.Insert("Chinese", "中文");
	dict.Insert("insert", "插入");
	
	string str;
	while (cin >> str)
	{
		auto ret = dict.FindR(str);
		if (ret)
		{
			cout << "对应的中文" << ":" << ret->_val << endl;
		}
		else
			cout << "不存在,请重新输入" << endl;
	}

	return 0;
}

下面我们在利用kv模型实现一下统计字符串出现的次数

#include "KVBinarySearchTree.h"

int main()
{
	string arr[] = { "苹果", "香蕉", "橘子", "苹果", "香蕉", "苹果" };
	BSTree<string, int> cnt;
	for (const auto& str : arr)
	{

		auto ret = cnt.Find(str);
		if (ret == nullptr)
		{
			cnt.Insert(str, 1);
		}
		else
		{
			ret->_val++;
		}
	}
	cnt.InOrder();
	return 0;
}

image-20220518005632616

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逃跑的机械工

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

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

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

打赏作者

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

抵扣说明:

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

余额充值