• [C++随想录] 二叉搜索树


    二叉搜索树的使用

    二叉搜索树 相较于 普通的二叉树来说:

    1. 根节点的左子树的所有键值都 小于 根节点, 根节点的右子树的所有键值 大于 根节点
    2. 根节点的 左右子树 都是 二叉搜索树
    3. 中序遍历升序的 ⇒ 二叉搜素树 又叫作 二叉排序树
    • 子树 && 节点
    1. 查找
      假如查找 key, 有如下 四种情况:
      1. 如果 key > 根节点的值, 那么就去根节点的右子树去查找
      2. 如果 key <根节点的值, 那么就去根节点的左子树去查找
      3. 如果 key = 根节点的值, 那么就找到了
      4. 如果找到 , 那就不存在
    • 查找的时间复杂度是 O(高度次), 而不是 O(logN)
      如果是 完全二叉树, 那么就是 O(logN); 如果 退化到极限情况, 类似于链表, 那么就是 O(N)

      所以, 总结下来, 时间复杂度就是 O(高度次)
      那么如何解决这种 退化问题呢? ⇒ AVL树 和 红黑树 就是针对这种情况做了特殊处理 --> 旋转
    1. 插入
      总体思想: 找到非空节点去插入

    2. 删除key

      1. 先找到key的位置, 有两种情况:
        1. 没找到, 那就直接返回
        2. 找到了key的位置, 记作cur. 找到了也有三种情况:
          1. cur的左子树为空
          2. cur的右子树为空
          3. cur的左右子树都不为空

    由于 cur要进行删除, 要把cur后面的内容链接到parent的后面. && cur也有两种可能 parent的左子树 or 右子树我们要cur后面的内容链接到 cur处于parent的位置
    删除具体如下👇👇👇

    1. cur的右子树为空
      (1) cur是parent的左子树

      (2) cur是parent的右子树
    2. cur的左子树为空
      (1) cur是parent的左子树

      (2) cur是parent的右子树
    3. cur的左右子树都不为空
      🗨️删除掉cur, 那么我们如何链接cur的左右子树呢?
      • 可以找一个节点来替换掉cur, 然后我们来处理这个节点的链接关系就好了
        🗨️替换过去, 也不改变二叉搜索树的结构, 那么节点是什么好呢? 后面集中处理这个节点, 那么这个节点应该容易处理才对, 那么这个节点是叶子节点吗?
      • 替换过去, 不改变二叉树的结构 — — 替换节点应该为 cur的左子树的最大节点 或者 cur的右子树的最小节点中序遍历, cur旁边的两个数; 中序是 左跟右, ⇒ 那么就应该是左子树的最大节点, 或者右子树的最小节点
        左子树的最大节点, 或者右子树的最小节点; 正好是叶子节点 ⇒ 那么我们处理这个替换节点也比较 容易 ⇒ 思想同上 替换节点的左子树为空, 或 替换节点的右子树为空

    二叉搜索树的模拟实现(K)

    整体结构


    Node类

    	template<class T>
    	struct BSTreeNode
    	{
    	public:
    		BSTreeNode(const T& key)
    			:_left(nullptr)
    			,_right(nullptr)
    			,_key(key)
    		{}
    
    	public:
    		BSTreeNode<T>* _left;
    		BSTreeNode<T>* _right;
    		T _key;
    	};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    BSTree类

    template<class T>
    class BSTree
    {
    	typedef BSTreeNode<T> Node;
    public:
    	BSTree()
    		:_root(nullptr)
    	{}
    	
    	// 析构函数
    	~BSTree()
    	{
    		_BSTree(_root);
    	}
    	
    private:
    		// 析构函数
    		void _BSTree(Node* root)
    		{
    			if (root == nullptr)
    				return;
    
    			// 后序遍历进行删除
    			_BSTree(root->_left);
    			_BSTree(root->_right);
    			delete root;
    		}
    		
    	// 成员函数
    	Node* _root;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    循环版本

    1. find
    Node* find(const K& key)
    {
    	return _find(_root, key);
    }
    
    private:
    Node* _find(Node* root, const T& 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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    1. insert
    bool insert(const T& key)
    {
    	Node* newnode = new Node(key);
    
    	if (_root == nullptr)
    	{
    		_root = newnode;
    		return true;
    	}
    
    	Node* parent = nullptr;
    	Node* cur = _root;
    	// 寻找插入的位置
    	while (cur)
    	{
    		if (key > cur->_key)
    		{
    			parent = cur;
    			cur = cur->_right;
    		}
    		else if (key < cur->_key)
    		{
    			parent = cur;
    			cur = cur->_left;
    		}
    		else
    		{
    			break;
    		}
    	}
    
    	// 链接
    	if (key > parent->_key)
    	{
    		parent->_right = newnode;
    	}
    	else if (key < parent->_key)
    	{
    		parent->_left = newnode;
    	}
    	else
    	{
    		return false;
    	}
    
    	return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    1. inorder
    void Inorder()
    {
    	_Inorder(_root);
    }
    
    private:
    void _Inorder(Node* root)
    {
    	if (root == nullptr)
    		return;
    
    	_Inorder(root->_left);
    	std::cout << root->_key << " ";
    	_Inorder(root->_right);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. erase

    bool erase(const T& key)
    {
    	return _erase(_root, key);
    }
    
    private:
    bool _erase(Node* root, const T& key)
    {
    	// 先找到位置
    	Node* parent = root;
    	Node* cur = root;
    
    	while (cur)
    	{
    		if (key > cur->_key)
    		{
    			parent = cur;
    			cur = cur->_right;
    
    		}	
    		else if (key < cur->_key)
    		{
    			parent = cur;
    			cur = cur->_left;
    		}
    		// 找到了
    		else
    		{
    			// 左为空
    			if (cur->_left == nullptr)
    			{
    				
    				if (cur == _root)
    				{
    					_root = cur->_right;
    				}
    				else
    				{
    					// 判读cur是parent的位置
    					if (cur == parent->_left)
    					{
    						parent->_left = cur->_right;
    					}
    					else if (cur == parent->_right)
    					{
    						parent->_right = cur->_right;
    					}
    				}
    			}
    			// 右为空
    			else if (cur->_right == nullptr)
    			{
    				if (cur == _root)
    				{
    					_root = cur->_left;
    				}
    				else
    				{
    					// 判读cur是parent的位置
    					if (cur == parent->_left)
    					{
    						parent->_left = cur->_left;
    					}
    					else if (cur == parent->_right)
    					{
    						parent->_right = cur->_left;
    					}
    				}
    			}
    			// 左右都不为空
    			else
    			{
    				// 先找到cur的左子树的最大值 或 右子树的最小值
    				// parent必须初始化为cur -- 以防删除的就是头节点
    				Node* parent = cur;
    				Node* LeftMax = cur->_left;
    				while (LeftMax->_right)
    				{
    					parent = LeftMax;
    					LeftMax = LeftMax->_right;
    				}
    
    				// 交换cur 和 LeftMax的值
    				std::swap(cur->_key, LeftMax->_key);
    
    				// 改变链接关系
    				if (parent->_left == LeftMax)
    				{
    					parent->_left = LeftMax->_left;
    				}
    				else if (parent->_right == LeftMax)
    				{
    					parent->_right = LeftMax->_left;
    				}
    
    				cur = LeftMax;
    			}
    			
    			// 集中释放 cur
    			delete cur;
    			return true;
    		}
    	}
    
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    递归版本

    1. findr
      无需链接关系 — — 不用引用即可
      1. 递归退出条件 root == nullptr, 那就返回nullptr
      2. 根据二叉搜索数的特性: 大了往右边走, 小了往左边走, 相等就返回当前节点的指针;
    Node* findr(const T& key)
    {
    	return _findr(_root, key);
    }
    
    private:
    Node*_findr(Node* root, const T& key)
    {
    	if (root == nullptr)
    		return nullptr;
    
    	if (key < root->_key)
    	{
    		_findr(root->_left, key);
    	}
    	else if (key > root->_key)
    	{
    		_findr(root->_right, key);
    	}
    	else
    	{
    		return root;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. insertr
      需要重新链接 -- -- 引用的妙用
      总体思想 : 遇到空就插入
      1. 递归返回条件 : 遇到空, 插入后, 返回true
      2. 二叉树的特性: 大了往右边走, 小了往左边走, 相等返回false

    bool insertr(const T& key)
    {
    	return _insertr(_root, key);
    }
    
    private:
    bool _insertr(Node*& root, const T& key)
    {
    	if (root == nullptr)
    	{
    		root = new Node(key);
    		return true;
    	}
    
    	if (key > root->_key)
    	{
    		return _insertr(root->_right, key);
    	}
    	else if (key < root->_key)
    	{
    		return _insertr(root->_left, key);
    	}
    	else
    	{
    		return false;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    1. eraser
      需要重新链接 -- -- 引用的妙用
      1. 递归结束条件: 遇到空就返回 false
      2. 先找到位置, 记作 cur
      3. cur有三种情况 :cur的左子树为空, cur的右子树为空, cur的左右子树都不为空; 三种情况分类讨论

    这个和上面的 引用的妙用是一样的道理, 那么我就不在这里画 递归展开图

    bool eraser(const T& key)
    {
    	return _eraser(_root, key);
    }
    
    private:
    bool _eraser(Node*& root, const T& key)
    {
    	if (root == nullptr)
    	{
    		return false;
    	}
    
    	if (key > root->_key)
    	{
    		_eraser(root->_right, key);
    	}
    	else if (key < root->_key)
    	{
    		_eraser(root->_left, key);
    	}
    	else
    	{
    		// 由于是上面节点的引用 && 要删掉root节点
    		// ⇒ 找一个背锅侠来代替root节点去删除
    		Node* tem = root;
    		
    		// 左子树为空
    		if (root->_left == nullptr)
    		{
    			root = root->_right;
    		}
    		//右子树为空
    		else if (root->_right == nullptr)
    		{
    			root = root->_left;
    		}
    		// 左右子树都不为空
    		else
    		{
    			// 找到左树的最大节点
    			Node* maxleft = root->_left;
    			while (maxleft->_right)
    			{
    				maxleft = maxleft->_right;
    			}
    			
    			// 交换root 和 maxleft的值
    			std::swap(maxleft->_key, root->_key);
    			
    			// 重新链接
    			root = maxleft->_left;
    			
    			// 背锅侠就位
    			tem = maxleft;
    		}
    		
    		// 统一删除
    		delete tem;
    		return true;
    	}
    
    	return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    二叉搜索树的应用

    二叉搜索树主要有两个版本 K版本 和 KV版本
    KV版本 相较于 K版本 就多了个 value

    template<class K, class V>
    	struct BSTreeNode
    	{
    	public:
    		BSTreeNode(const K& key, const V& value)
    			:_left(nullptr)
    			,_right(nullptr)
    			,_key(key)
    			,_value(value)
    		{}
    
    	public:
    		BSTreeNode<K,V>* _left;
    		BSTreeNode<K,V>* _right;
    		K _key;
    		V _value;
    	};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    template<class K, class V>
    class BSTree
    {
    	typedef BSTreeNode<K, V> Node;
    public:
    	BSTree()
    		:_root(nullptr)
    	{}
    private:
    	Node* _root;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    由于 还是对 K 进行操作 ⇒ 我们这里就不写 KV的代码了. 后面源码会附上 KV的完整代码


    二叉搜索树主要应用于两种模型: K模型 和 KV模型

    1. K模型 — — 根据关键码Key去解决 在不在的问题
      比如 : 判断单词是否拼写错误 (将词库导入二叉搜索树, 然后判断在不在)
    void test1()
    {
    	// 模拟导入词库
    	muyu::BSTree<string, string> World;
    	World.insert("insert", "插入");
    	World.insert("input", "输入");
    	World.insert("output", "输出");
    	World.insert("love", "爱情");
    
    	string str;
    	while (cin >> str)
    	{
    		// 查找是否在词库中出现
    		auto ret = World.find(str);
    		if (ret)
    		{
    			cout << "输入正确" << endl;
    		}
    		else
    		{
    			cout << "查无单词, 请重新输入" << endl;
    		}
    	}
    }
    
    int main()
    {
    	test1();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    运行结果:

    1. KV模型 — — 每一个关键码Key, 都有一个与之对应的 Value, 存在 键值对
      比如: 统计水果出现的次数
    void test2()
    {
    	muyu::BSTree<string, int> cnt;
    	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };
    
    	for (const auto& e : arr)
    	{
    		auto res = cnt.find(e);
    		// 第一次插入, 次数就给个1
    		if (!res)
    		{
    			cnt.insert(e, 1);
    		}
    		// 不是第一次插入, 就在key对应的value进行++
    		else
    		{
    			res->_value++;
    		}
    
    	}
    
    	cnt.Inorder();
    }
    
    int main()
    {
    	test2();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    运行结果:

    苹果 6
    西瓜 3
    香蕉 2
    
    • 1
    • 2
    • 3

    源码(kv)

    #pragma once
    
    namespace muyu
    {
    	template<class K, class V>
    	struct BSTreeNode
    	{
    	public:
    		BSTreeNode(const K& key = K(), const V& value = V())
    			:_left(nullptr)
    			,_right(nullptr)
    			,_key(key)
    			,_value(value)
    		{}
    
    	public:
    		BSTreeNode<K,V>* _left;
    		BSTreeNode<K,V>* _right;
    		K _key;
    		V _value;
    	};
    
    	template<class K, class V>
    	class BSTree
    	{
    		typedef BSTreeNode<K, V> Node;
    	public:
    		BSTree()
    			:_root(nullptr)
    		{}
    
    		~BSTree()
    		{
    			_BSTree(_root);
    		}
    
    		bool insert(const K& key, const V& value)
    		{
    			Node* newnode = new Node(key, value);
    
    			if (_root == nullptr)
    			{
    				_root = newnode;
    				return true;
    			}
    
    			Node* parent = nullptr;
    			Node* cur = _root;
    			// 寻找插入的位置
    			while (cur)
    			{
    				if (key > cur->_key)
    				{
    					parent = cur;
    					cur = cur->_right;
    				}
    				else if (key < cur->_key)
    				{
    					parent = cur;
    					cur = cur->_left;
    				}
    				else
    				{
    					break;
    				}
    			}
    
    			// 链接
    			if (key > parent->_key)
    			{
    				parent->_right = newnode;
    			}
    			else if (key < parent->_key)
    			{
    				parent->_left = newnode;
    			}
    			else
    			{
    				return false;
    			}
    
    			return true;
    		}
    
    		bool insertr(const K& key)
    		{
    			return _insertr(_root, key);
    		}
    
    		void Inorder()
    		{
    			_Inorder(_root);
    		}
    
    		Node* find(const K& key)
    		{
    			return _find(_root, key);
    		}
    
    		Node* findr(const K& key)
    		{
    			return _findr(_root, key);
    		}
    
    		bool erase(const K& key)
    		{
    			return _erase(_root, key);
    		}
    
    		bool eraser(const K& key)
    		{
    			return _eraser(_root, key);
    		}
    
    	private:
    
    		void _BSTree(Node* root)
    		{
    			if (root == nullptr)
    				return;
    
    			// 后序遍历进行删除
    			_BSTree(root->_left);
    			_BSTree(root->_right);
    			delete root;
    		}
    
    		void _Inorder(Node* root)
    		{
    			if (root == nullptr)
    				return;
    
    			_Inorder(root->_left);
    			std::cout << root->_key << " " << root->_value << std::endl;
    			_Inorder(root->_right);
    
    		}
    
    		Node* _insertr(Node*& root, const K& key, const V& value)
    		{
    			if (root == nullptr)
    			{
    				root = new Node(key, value);
    				return root;
    			}
    
    			if (key > root->_key)
    			{
    				return _insertr(root->_right, key);
    			}
    			else if (key < root->_key)
    			{
    				return _insertr(root->_left, key);
    			}
    			else
    			{
    				return nullptr;
    			}
    		}
    
    		Node* _find(Node* root, 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;
    		}
    
    		Node* _findr(Node* root, const K& key)
    		{
    			if (root == nullptr)
    				return nullptr;
    
    			if (key < root->_key)
    			{
    				_findr(root->_left, key);
    			}
    			else if (key > root->_key)
    			{
    				_findr(root->_right, key);
    			}
    			else
    			{
    				return root;
    			}
    		}
    
    		bool _erase(Node* root, const K& key)
    		{
    			// 先找到位置
    			Node* parent = root;
    			Node* cur = root;
    
    			while (cur)
    			{
    				if (key > cur->_key)
    				{
    					parent = cur;
    					cur = cur->_right;
    
    				}	
    				else if (key < cur->_key)
    				{
    					parent = cur;
    					cur = cur->_left;
    				}
    				// 找到了
    				else
    				{
    					// 左为空
    					if (cur->_left == nullptr)
    					{
    						
    						if (cur == _root)
    						{
    							_root = cur->_right;
    						}
    						else
    						{
    							// 判读cur是parent的位置
    							if (cur == parent->_left)
    							{
    								parent->_left = cur->_right;
    							}
    							else if (cur == parent->_right)
    							{
    								parent->_right = cur->_right;
    							}
    						}
    					}
    					// 右为空
    					else if (cur->_right == nullptr)
    					{
    						if (cur == _root)
    						{
    							_root = cur->_left;
    						}
    						else
    						{
    							// 判读cur是parent的位置
    							if (cur == parent->_left)
    							{
    								parent->_left = cur->_left;
    							}
    							else if (cur == parent->_right)
    							{
    								parent->_right = cur->_left;
    							}
    						}
    					}
    					// 左右都不为空
    					else
    					{
    						// 先找到cur的左子树的最大值 或 右子树的最小值
    						Node* parent = cur;
    						Node* LeftMax = cur->_left;
    						while (LeftMax->_right)
    						{
    							parent = LeftMax;
    							LeftMax = LeftMax->_right;
    						}
    
    						// 交换cur 和 LeftMax的值
    						std::swap(cur->_key, LeftMax->_key);
    
    						// 改变链接关系
    						if (parent->_left == LeftMax)
    						{
    							parent->_left = LeftMax->_left;
    						}
    						else if (parent->_right == LeftMax)
    						{
    							parent->_right = LeftMax->_left;
    						}
    
    						cur = LeftMax;
    					}
    
    					delete cur;
    					return true;
    				}
    			}
    
    			return false;
    		}
    
    		bool _eraser(Node*& root, const K& key)
    		{
    			if (root == nullptr)
    			{
    				return false;
    			}
    
    			if (key > root->_key)
    			{
    				_eraser(root->_right, key);
    			}
    			else if (key < root->_key)
    			{
    				_eraser(root->_left, key);
    			}
    			else
    			{
    				Node* tem = root;
    
    				if (root->_left == nullptr)
    				{
    					root = root->_right;
    				}
    				else if (root->_right == nullptr)
    				{
    					root = root->_left;
    				}
    				else
    				{
    					Node* maxleft = root->_left;
    					while (maxleft->_right)
    					{
    						maxleft = maxleft->_right;
    					}
    
    					std::swap(maxleft->_key, root->_key);
    
    					root = maxleft->_left;
    
    					tem = maxleft;
    				}
    
    				delete tem;
    				return true;
    			}
    
    			return false;
    		}
    
    		Node* _root;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353

    晚日寒鸦一片愁。柳塘新绿却温柔。若教眼底无离恨,不信人间有白头。
    肠已断,泪难收。相思重上小红楼。情知已被山遮断,频倚阑干不自由。
    — — 辛弃疾· 《鹧鸪天》

  • 相关阅读:
    ES6的模块化管理、立即执行函数(IIFE):在函数声明后面立即调用、函数劫持
    C/C++数1的个数 2019年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
    Docker 搭建 apache2 + php8 + MySQL8 环境
    Bitcoin+STARK: ZeroSync & Khepri
    前端最强面试宝典 - JS 篇之 ES6
    Linux资源限制命令—ulimit
    Git仓库(github/gialab)进行fork后如何与原仓库同步
    几道ctf 命令注入题目的解法
    华为云如何购买并登录Windows弹性云服务器?
    v73.结构
  • 原文地址:https://blog.csdn.net/qq_67549203/article/details/133686939