C++进阶之搜索二叉树

目录

一、搜素二叉树的基本概念

二、搜索二叉树的基本性质

三、搜索二叉树的应用

四、模拟实现

4.1 插入操作

4.2 删除操作

4.3 查找操作

4.4 遍历操作

4.5 整体框架


一、搜素二叉树的基本概念

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

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

下面这张图就是一个搜索二叉树:

二、搜索二叉树的基本性质

  1. 有序性质
    每个节点都包含一个键值。
    对于每个节点,其左子树中的所有节点的键值小于该节点的键值。
    对于每个节点,其右子树中的所有节点的键值大于该节点的键值。
    因此,中序遍历搜索二叉树会得到一个有序的序列。
  2. 唯一性
    在搜索二叉树中,不存在两个节点具有相同的键值。
  3. 平衡性
    在极端情况下,搜索二叉树可能会退化为链表,导致查找、插入和删除操作的时间复杂度为 O(n)。为了保持树的平衡性,可以使用平衡二叉搜索树(如 AVL 树、红黑树等)。

三、搜索二叉树的应用

1.K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值

2.KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:
       比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对。

四、模拟实现

4.1 插入操作

插入的具体过程如下:

  1. 树为空,则直接新增节点,赋值给root指针
  2. 树不空,按二叉搜索树性质查找插入位置,插入新节点
bool Insert(const K& key, const V& value)
	{
		Node* pre = nullptr;
		Node* cur = _root;
		Node* tmp = new Node(key, value);
		if (cur == nullptr)
		{
			_root = tmp;
			return true;
		}
		while (cur)
		{
			if (key > cur->_key)
			{
				pre = cur;
				cur = cur->right;
			}
			else
			{
				pre = cur;
				cur = cur->left;
			}
		}
		if (key > pre->_key)
		{
			pre->right = tmp;
		}
		else
			pre->left = tmp;
		return true;
	}

这是非递归版本的,递归版本的大家可以自己尝试一下。

4.2 删除操作

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

  • 要删除的结点无孩子结点
  • 要删除的结点只有左孩子结点
  • 要删除的结点只有右孩子结点
  • 要删除的结点有左、右孩子结点

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

  1. 情况1:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
  2. 情况2:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
  3. 情况3:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题--替换法删除
bool Erase(const K& key)
	{
		Node* pre = nullptr;
		Node* cur = _root;
		while (cur->_key != key)
		{
			if (key > cur->_key)
			{
				pre = cur;
				cur = cur->right;
			}
			else if (key < cur->_key)
			{
				pre = cur;
				cur = cur->left;
			}
		}
		if (cur->left==nullptr)//左孩子为空
		{
			if (pre == nullptr)
			{
				_root = cur->right;
			}
			else if (pre->_key > key)
			{
				pre->left = cur->right;
			}
			else
			{
				pre->right = cur->right;
			}
			delete cur;
		}
		else if(cur->right==nullptr)//右孩子为空
		{
			if (pre == nullptr)
			{
				_root = cur->left;
			}
			else if (pre->_key > key)
			{
				pre->left = cur->left;
			}
			else
			{
				pre->right = cur->left;
			}
			delete cur;
		}
		else if (cur->left && cur->right)//左右孩子都不为空
		{
			pre = cur;
			Node* tmp = cur->right;
			while (tmp->left)
			{
				pre = tmp;
				tmp = tmp->left;
			}
			swap(cur->_key, tmp->_key);
			swap(cur->_value, tmp->_value);
			if (pre != cur)
				pre->left = tmp->right;
			else
				pre->right = tmp->right;
			delete tmp;
		}
		return true;
	}

4.3 查找操作

从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
最多查找高度次,走到到空,还没找到,这个值不存在。

Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key > cur->_key)
				cur = cur->right;
			else if (key < cur->_key)
				cur = cur->left;
			else
				return cur;
				
		}
		return nullptr;
	}

4.4 遍历操作

void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->left);
		cout << root->_key << " " << root->_value << endl;
		_InOrder(root->right);
	}

4.5 整体框架

#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class K, class V>
struct BSTreeNode
{
	K _key;
	V _value;
	BSTreeNode<K,V>* left;
	BSTreeNode<K,V>* right;
	BSTreeNode(K key,V value)
		:_key(key)
		,_value(value)
		,left(nullptr)
		,right(nullptr)
	{}
};

template<class K, class V>
class BSTree
{
	typedef BSTreeNode<K, V> Node;
public:
	bool Insert(const K& key, const V& value)
	{
		return Insert(key, value, _root);
		/*Node* pre = nullptr;
		Node* cur = _root;
		Node* tmp = new Node(key, value);
		if (cur == nullptr)
		{
			_root = tmp;
			return true;
		}
		while (cur)
		{
			if (key > cur->_key)
			{
				pre = cur;
				cur = cur->right;
			}
			else
			{
				pre = cur;
				cur = cur->left;
			}
		}
		if (key > pre->_key)
		{
			pre->right = tmp;
		}
		else
			pre->left = tmp;
		return true;*/
	}
	bool Insert(const K& key, const V& value, Node*& root)
	{
		if (root == nullptr)
		{
			root = new Node(key, value);
			return true;
		}
		if (key > root->_key)
			return Insert(key, value, root->right);
		else
			return Insert(key, value, root->left);
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key > cur->_key)
				cur = cur->right;
			else if (key < cur->_key)
				cur = cur->left;
			else
				return cur;
				
		}
		return nullptr;
	}
	bool Erase(const K& key)
	{
		Node* pre = nullptr;
		Node* cur = _root;
		while (cur->_key != key)
		{
			if (key > cur->_key)
			{
				pre = cur;
				cur = cur->right;
			}
			else if (key < cur->_key)
			{
				pre = cur;
				cur = cur->left;
			}
		}
		if (cur->left==nullptr)//左孩子为空
		{
			if (pre == nullptr)
			{
				_root = cur->right;
			}
			else if (pre->_key > key)
			{
				pre->left = cur->right;
			}
			else
			{
				pre->right = cur->right;
			}
			delete cur;
		}
		else if(cur->right==nullptr)//右孩子为空
		{
			if (pre == nullptr)
			{
				_root = cur->left;
			}
			else if (pre->_key > key)
			{
				pre->left = cur->left;
			}
			else
			{
				pre->right = cur->left;
			}
			delete cur;
		}
		else if (cur->left && cur->right)//左右孩子都不为空
		{
			pre = cur;
			Node* tmp = cur->right;
			while (tmp->left)
			{
				pre = tmp;
				tmp = tmp->left;
			}
			swap(cur->_key, tmp->_key);
			swap(cur->_value, tmp->_value);
			if (pre != cur)
				pre->left = tmp->right;
			else
				pre->right = tmp->right;
			delete tmp;
		}
		return true;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->left);
		cout << root->_key << " " << root->_value << endl;
		_InOrder(root->right);
	}
	void InOrder()
	{
		_InOrder(_root);
	}
private:
	Node* _root = nullptr;
};


 

  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值