【C++】B树

概念

在这里插入图片描述

实现

二叉搜索树的插入(非递归)

二叉搜索树的中序遍历

二叉搜索树的查找(非递归)

二叉搜索树的删除(非递归)

二叉搜索树的查找(递归)

拷贝构造函数

赋值运算符重载

三、二叉搜索树的实现完整代码

namespace Key
{
	//结点
	template<class K>
	struct BSTreeNode
	{
		BSTreeNode(const K& val = K())
			:_key(val)
			, _left(nullptr)
			, _right(nullptr)
		{}
		K _key;
		BSTreeNode<K>* _left;
		BSTreeNode<K>* _right;
	};

	//树结构
	//class BinarySearchTree
	template<class K>
	class BSTree
	{
		typedef BSTreeNode<K> Node;
		Node* _root;
	public:
		BSTree()
			:_root(nullptr)
		{}

		//C++11强制编译器生成默认构造
		//BSTree() = default;

		BSTree(const BSTree<K>& x)
		{
			_root = _Copy(x._root);
		}
		BSTree<K> operator=(BSTree<K> x)
		{
			swap(_root, x._root);
			return *this;
		}
		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}
			else
			{
				Node* parent = _root;
				Node* cur = _root;
				while (cur != nullptr)
				{
					if (cur->_key == key)
					{
						return false;
					}
					else if (cur->_key > key)
					{
						parent = cur;
						cur = cur->_left;
					}
					else
					{
						parent = cur;
						cur = cur->_right;
					}
				}
				cur = new Node(key);
				if (parent->_key > key)
				{
					parent->_left = cur;
				}
				else if (parent->_key < key)
				{
					parent->_right = cur;
				}
				return true;
			}
		}
		bool InsertR(const K& key)
		{
			return _InsertR(_root, key);
		}

		void InOrder()//中序遍历
		{
			_InOrder(_root);
			cout << endl;
		}

		//非递归
		//先考虑parent与cur之间的关系
		bool Erase1(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//找到了
				{
					//目标值为根节点
					if (parent == nullptr)
					{
						if (cur->_left == nullptr)
						{
							_root = cur->_right;
							delete cur;
							return true;
						}
						else if (cur->_right == nullptr)
						{
							_root = cur->_left;
							delete cur;
							return true;
						}
						else
						{
							Node* leftMaxParent = cur;
							Node* leftMax = cur->_left;
							if (leftMax->_right == nullptr)
							{
								leftMax->_right = cur->_right;
								delete cur;
								_root = leftMax;
								return true;
							}
							while (leftMax->_right)
							{
								leftMaxParent = leftMax;
								leftMax = leftMax->_right;
							}
							std::swap(leftMax->_key, cur->_key);
							leftMaxParent->_right = leftMax->_left;
							delete leftMax;
							leftMax = nullptr;
							return true;
						}
					}
					//目标值在父节点的左子树
					if (parent->_left == cur)
					{
						//如果目标值的左子树为空
						if (cur->_left == nullptr)
						{
							parent->_left = cur->_right;
							delete cur;
							return true;
						}
						//如果目标值的右子树为空
						else if (cur->_right == nullptr)
						{
							parent->_left = cur->_left;
							delete cur;
							return true;
						}
						//如果目标值的左右子树都不为空
						else
						{
							Node* leftMaxParent = cur;
							Node* leftMax = cur->_left;
							if (leftMax->_right == nullptr)
							{
								leftMax->_right = cur->_right;
								delete cur;
								parent->_left = leftMax;
								return true;
							}
							while (leftMax->_right)
							{
								leftMaxParent = leftMax;
								leftMax = leftMax->_right;
							}
							std::swap(leftMax->_key, cur->_key);
							leftMaxParent->_right = leftMax->_left;
							delete leftMax;
							leftMax = nullptr;
							return true;
						}
					}
					//目标值在父节点的右子树
					else
					{
						if (cur->_left == nullptr)
						{
							parent->_right = cur->_right;
							delete cur;
							return true;
						}
						else if (cur->_right == nullptr)
						{
							parent->_right = cur->_left;
							delete cur;
							return true;
						}
						else
						{
							Node* leftMaxParent = cur;
							Node* leftMax = cur->_left;
							if (leftMax->_right == nullptr)
							{
								leftMax->_right = cur->_right;
								delete cur;
								parent->_right = leftMax;
								return true;
							}
							while (leftMax->_right)
							{
								leftMaxParent = leftMax;
								leftMax = leftMax->_right;
							}
							swap(leftMax->_key, cur->_key);
							leftMaxParent->_right = leftMax->_left;
							delete leftMax;
							leftMax = nullptr;
							return true;
						}
					}
				}
			}
			return false;
		}
		//先考虑cur与其孩子之间的关系
		bool Erase2(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
				{
					if (cur->_left == nullptr)
					{
						if (parent == nullptr)
						{
							_root = cur->_right;
						}
						else if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
						else if (parent->_right == cur)
						{
							parent->_right = cur->_right;
						}
					}
					else if (cur->_right == nullptr)
					{
						if (parent == nullptr)
						{
							_root = cur->_left;
						}
						else if (parent->_left == cur)
						{
							parent->_left = cur->_left;
						}
						else if (parent->_right = cur)
						{
							parent->_right = cur->_left;
						}
					}
					else
					{
						Node* leftMax = cur->_left;
						Node* leftMaxParent = cur;
						while (leftMax->_right)
						{
							leftMaxParent = leftMax;
							leftMax = leftMax->_right;
						}
						std::swap(cur->_key, leftMax->_key);
						if (leftMaxParent->_left == leftMax)
						{
							leftMaxParent->_left = leftMax->_left;
						}
						else
						{
							leftMaxParent->_right = leftMax->_left;
						}

						cur = leftMax;
					}
					delete cur;
					return true;
				}
			}
			return false;
		}

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

		//非递归
		bool Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key == key)
				{
					return true;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					cur = cur->_right;
				}
			}
			return false;
		}
		//递归
		bool FindR(const K& key)
		{
			return _FindR(_root, key);
		}
		~BSTree()
		{
			_Destory(_root);
		}
	private:
		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}
		bool _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
				return false;

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

			if (root->_key > key)
			{
				return _EraseR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _EraseR(root->_right, key);
			}
			else//找到了
			{
				Node* del = root;
				if (root->_left == nullptr)
				{
					root = root->_right;
				}
				else if (root->_right == nullptr)
				{
					root = root->_left;
				}
				else//左右子树均不为空
				{
					//找右树最左节点
					Node* min = root->_right;
					while (min->_left)
					{
						min = min->_left;
					}
					swap(root->_key, min->_key);
					return _EraseR(root->_right, key);
				}
				delete del;
				return true;
			}
		}
		void _Destory(Node*& root)
		{
			if (root == nullptr)
			{
				return;
			}
			_Destory(root->_left);
			_Destory(root->_right);
			delete root;
			root = nullptr;
		}
		Node* _Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}
			Node* copy = new Node(root->_key);
			copy->_left = _Copy(root->_left);
			copy->_right = _Copy(root->_right);
			return copy;
		}
	};
	void test1()
	{
		BSTree<int>root;
		int arr[] = { 1,5,4,8,15,-1,4,3,7,89,5,6,8,14,97,45,9 };
		for (auto it : arr)
		{
			root.Insert(it);
		}

		root.InOrder();

		root.Erase2(8);
		root.InOrder();

		BSTree<int>root1;
		root1.Insert(0);
		root1.Erase2(0);
		root1.InOrder();

		for (auto it : arr)
		{
			root.EraseR(it);
			root.InOrder();
		}
	}

	void test2()
	{
		BSTree<int>root;
		int arr[] = { 1,5,4,8,15,-1,4,3,7,89,5,6,8,14,97,45,9 };
		for (auto it : arr)
		{
			root.Insert(it);
		}
		BSTree<int>root1 = root;
		root1.InOrder();

		BSTree<int>root2;
		root2 = root;
		root2.InOrder();
	}
}

相关推荐

  1. 线段CF 练习题

    2024-06-09 09:48:03       46 阅读
  2. <span style='color:red;'>CI</span>/<span style='color:red;'>CD</span>

    CI/CD

    2024-06-09 09:48:03      49 阅读
  3. CI/CD总结

    2024-06-09 09:48:03       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-09 09:48:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-09 09:48:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-09 09:48:03       87 阅读
  4. Python语言-面向对象

    2024-06-09 09:48:03       96 阅读

热门阅读

  1. VSCode 1.90版本 升级需谨慎~(Python)

    2024-06-09 09:48:03       27 阅读
  2. React Native 快速Demo(2)

    2024-06-09 09:48:03       23 阅读
  3. PostgreSQL教程

    2024-06-09 09:48:03       27 阅读
  4. Docker面试整理-Docker Swarm是什么?

    2024-06-09 09:48:03       31 阅读
  5. Python——用新字符替换字符串中的旧字符

    2024-06-09 09:48:03       37 阅读
  6. 基本通信设备

    2024-06-09 09:48:03       28 阅读
  7. LIO-SAM报错记录

    2024-06-09 09:48:03       33 阅读
  8. GlusterFS分布式文件系统

    2024-06-09 09:48:03       36 阅读
  9. 821. 字符的最短距离

    2024-06-09 09:48:03       31 阅读
  10. 【力扣】 两个字符串的最小ASCII删除和

    2024-06-09 09:48:03       36 阅读
  11. git checkout file 撤销对该文件的所有修改

    2024-06-09 09:48:03       33 阅读
  12. echarts-for-react

    2024-06-09 09:48:03       37 阅读
  13. C# WPF入门学习主线篇(十一)—— 布局管理

    2024-06-09 09:48:03       30 阅读
  14. scss是什么安装使⽤的步骤

    2024-06-09 09:48:03       31 阅读