【C/C++数据结构】搜索二叉树

目录

概述

算法

源码

BSTree.h

test.cpp


概述

搜索二叉树又称二叉排序树,它遵守二叉树的原则,每个节点都有规则:

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

二叉树的每个节点都包含数据:key值、左节点指针、右节点指针

二叉树的插入:新插入的节点一定是叶子节点,先按照key值找到需要插入节点的位置,然后新建节点在该位置插入新节点

二叉树的删除:先找到需要删除的节点,若无法找到,则删除失败;若找到了,则根据一下三种情况进行判断:1. 该节点的左节点为空;2. 该节点的右节点为空;3. 该节点的左右节点都不为空。若是前两种情况,则将不为空的子树上移即可完成删除,若是第三种情况,需要找到左子树的最大节点或者右子树的最小节点,将找到的极值节点与需要删除的节点的值交换,再删除找到的极值节点。

算法

严格遵守搜索二叉树原则:左节点一定小于根节点,右节点一定大于根节点,无重复值

分别实现了进行循环和递归两种模式的设计方案,递归方案代码更简洁,但对栈帧空间的损耗更大

搜索二叉树的平均时间复杂度是O(log n),但不稳定,存在极值为O(n),极值就是后面的值都往一边插入的情况,基于这种情况,后面通过设计AVL平衡二叉树来弥补这种缺陷

源码

BSTree.h

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <cassert>

template<class T>
struct BSTreeNode
{
	BSTreeNode<T>* _left;
	BSTreeNode<T>* _right;
	T _key;

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

template<class T>
class BSTree
{
	typedef BSTreeNode<T> Node;
public:
	// 构造
	BSTree()
		: _root(nullptr)
	{}

	BSTree(const BSTree<T>& t)
	{
		_root = _Copy(t._root);
	}

	BSTree<T>& operator=(BSTree<T> t)
	{
		std::swap(_root, t._root);
		return *this;
	}

	// 析构
	~BSTree()
	{
		_Destroy(_root);
		_root = nullptr;
	}

	// 新增节点
	bool Insert(const T& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			parent = cur;
			if (cur->_key < key)
				cur = cur->_right;
			else if (cur->_key > key)
				cur = cur->_left;
			else
				return false;
		}

		cur = new Node(key);
		if (parent->_key < key)
			parent->_right = cur;
		else
			parent->_left = cur;

		return true;
	}

	// 查找节点
	bool Find(const T& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
				cur = cur->_right;
			else if (cur->_key > key)
				cur = cur->_left;
			else
				return true;
		}
		return false;
	}

	// 删除节点
	bool Erase(const T& key)
	{
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				// 找到了需要删除的节点
				// 1. 左为空
				// 2. 右为空
				// 3. 左右都不为空
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_right;
						else
							parent->_right = cur->_right;
					}
					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;
					}
					delete cur;
				}
				else
				{
					// 找到左子树的最大值(右子树的最小值)
					Node* max_parent = cur;
					Node* max_left = cur->_left;
					while (max_left->_right)
					{
						max_parent = max_left;
						max_left = max_left->_right;
					}
					cur->_key = max_left->_key;
					if (max_left == max_parent->_left)
						max_parent->_left = max_left->_left;
					else
						max_parent->_right = max_left->_left;
					delete max_left;
				}
				return true;
			}
		}
		return false;
	}

	void In_Order()
	{
		_In_Order(_root);
		std::cout << std::endl;
	}

	bool Re_Insert(const T& key)
	{
		return _Re_Insert(_root, key);
	}

	bool Re_Find(const T& key)
	{
		return _Re_Find(_root, key);
	}

	bool Re_Erase(const T& key)
	{
		return _Re_Erase(_root, key);
	}

private:
	Node* _Copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;
		Node* new_root = new Node(root->_key);
		new_root->_left = _Copy(root->_left);
		new_root->_right = _Copy(root->_right);
		return new_root;
	}

	void _Destroy(Node* root)
	{
		if (root != nullptr)
		{
			_Destroy(root->_left);
			_Destroy(root->_right);
			delete root;
		}
	}

	void _In_Order(Node* root)
	{
		if (root != nullptr)
		{
			_In_Order(root->_left);
			std::cout << root->_key << " ";
			_In_Order(root->_right);
		}
	}

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

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

	bool _Re_Find(Node*& root, const T& key)
	{
		if (root == nullptr)
			return false;
		
		if (root->_key < key)
			_Re_Find(root->_right, key);
		else if (root->_key > key)
			_Re_Find(root->_left, key);
		else
			return true;
	}

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

		if (root->_key < key)
		{
			return _Re_Erase(root->_right, key);
		}
		else if (root->_key > key)
		{
			return _Re_Erase(root->_left, key);
		}
		else
		{
			Node* del = root;
			if (del->_left == nullptr)
			{
				root = root->_right;
			}
			else if (del->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				// 找右子树的最小值节点
				Node* min_right = root->_right;
				while (min_right->_left)
				{
					min_right = min_right->_left;
				}
				std::swap(min_right->_key, root->_key);

				// 转换成在子树中删除节点
				return _Re_Erase(root->_right, min_right->_key);
			}

			delete del;
			return true;
		}
	}

private:
	Node* _root;
};

test.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include "BSTree.hpp"

void test()
{
	BSTree<int> t1;
	t1.Insert(2);
	t1.Insert(4);
	t1.Insert(1);
	t1.Insert(3);
	t1.Re_Insert(11);
	t1.Re_Insert(21);
	t1.Re_Insert(-11);
	t1.Re_Insert(-21);
	t1.Insert(-10);
	t1.Insert(-10);		// 重复值无法插入
	t1.Re_Insert(-20);
	t1.Re_Insert(-20);
	t1.Re_Insert(0);
	t1.In_Order();		// 中序遍历,自动实现递增排序
						// -21 -20 -11 -10 0 1 2 3 4 11 21
						
	BSTree<int> t2(t1);
	t2.In_Order();		// -21 -20 -11 -10 0 1 2 3 4 11 21
	std::cout << t2.Find(0) << std::endl;			// 1
	std::cout << t2.Re_Find(-10) << std::endl;		// 1
	std::cout << t2.Re_Find(100) << std::endl;		// 0
	t2.~BSTree();
	t2.In_Order();

	BSTree<int> t3 = t1;
	t3.Erase(0);
	t3.Erase(8);
	t3.Erase(4);
	t3.Re_Erase(-10);
	t3.In_Order();		// -21 -20 -11 1 2 3 11 21
}

int main()
{
	test();

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AllinTome

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

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

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

打赏作者

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

抵扣说明:

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

余额充值