• 二叉搜索树


    一、二叉搜索树的概念

    一颗二叉搜索树可以为 空树 或者满足如下条件

    • 左子树的所有结点的值小于根结点的值
    • 右子树的所有结点的值大于根结点的值
    • 左右子树均为二叉搜索树

    在这里插入图片描述
    对上述的二叉搜索树进行中序遍历的结果为: [1, 3, 4, 6, 7, 8, 10, 13, 14],因此二叉搜索树又称为二叉排序树

    二、二叉搜索树的实现

    1. 二叉搜索树的存储结构

    // 二叉搜索树结点
    template<class K>
    struct BSTreeNode
    {
    	BSTreeNode<K>(const K& key = K())
    		: _key(key)
    		, _left(nullptr)
    		, _right(nullptr)
    	{}
    
    	K _key;
    	BSTreeNode<K>* _left;
    	BSTreeNode<K>* _right;
    };
    
    // 二叉搜索树
    template<class K>
    class BSTree
    {
    	typedef BSTreeNode<K> Node;
    public:
    	BSTree<K>()
    		: _root(nullptr)
    	{}
    
    private:
    	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

    2. 二叉搜索树的插入

    在二叉搜索树中,插入节点后仍需满足二叉搜索树的性质,如果插入的值在二叉搜索树中已经存在,则插入失败

    时间复杂度 O(N): 因为二叉搜索树可能只有左子树或右子树

    // 非递归版本
    bool Insert(const K& key)
    {
    	// 树为空时
    	if (_root == nullptr)
    	{
    		_root = new Node(key);
    
    		return true;
    	}
    
    	// 为了找到插入位置后可以链接,需要保存父节点
    	Node* parent = nullptr;
    	Node* cur = _root;
    
    	// 找插入位置
    	while (cur)
    	{
    		// 保存父节点的位置
    		parent = cur;
    
    		// 当前结点值 大于 key 时,则插入位置在左子树
    		// 当前结点值 小于 key 时,则插入位置在右子树
    		// 当前结点值 等于 key 时,则插入失败
    		if (cur->_key > key) cur = cur->_left;
    		else if (cur->_key < key) cur = cur->_right;
    		else return false;
    	}
    
    	// 链接:判断插入位置在左子树还是右子树
    	cur = new Node(key);
    	if (parent->_key > key) parent->_left = cur;
    	else parent->_right = cur;
    
    	// 插入成功
    	return true;
    }
    
    // 递归版本
    bool Insert(const K& key)
    {
    	// 调用子函数递归
    	return _Insert(_root, key);
    }
    
    // 插入递归
    static bool _Insert(Node*& root, const K& key)
    {
    	if (root == nullptr)
    	{
    		root = new Node(key);
    
    		return true;
    	}
    
    	if (root->_key > key) return _Insert(root->_left, key);
    	else if (root->_key < key) return _Insert(root->_right, 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
    • 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

    3. 二叉搜索树的删除

    在二叉搜索树中,删除节点后仍需满足二叉搜索树的性质,将删除结点分四种情况

    • 结点无左右孩子,可以直接删除

    • 结点无左孩子,让父节点链接自己的右孩子后删除结点
      在这里插入图片描述

    • 结点无右孩子,让父节点链接自己的左孩子后删除结点
      在这里插入图片描述

    在结点无左孩子(或者无右孩子) 的情况中,如果删除的是根结点,则直接让根结点指针链接自己的右孩子(或者左孩子),然后删除结点
    在这里插入图片描述

    • 结点有左右孩子,可以使用左子树的最大结点(或者右子树的最小结点) 替换该结点,然后根据结点无右孩子(或者结点无左孩子) 的情况删除结点
      在这里插入图片描述

    如果删除的值在二叉树中不存在,则删除失败

    时间复杂度 O(N): 因为二叉搜索树可能只有左子树或右子树

    // 非递归版本
    bool Erase(const K& key)
    {
    	// 树为空时,删除失败
    	if (_root == nullptr) return false;
    
    	Node* parent = nullptr;
    	Node* cur = _root;
    	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 (cur == _root) _root = cur->_right;
    				else if (parent->_key > key) 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->_key > key) parent->_left = cur->_left;
    				else parent->_right = cur->_left;
    
    				delete cur; 
    			}
    			else
    			{
    				// 找到左子树的最大结点
    				Node* maxLeftParent = cur;
    				Node* maxLeft = cur->_left;
    				while (maxLeft->_right)
    				{
    					maxLeftParent = maxLeft;
    					maxLeft = maxLeft->_right;
    				}
    
    				// 交换
    				std::swap(cur->_key, maxLeft->_key);
    
    				// 删除结点
    				if (maxLeftParent == cur) maxLeftParent->_left = maxLeft->_left;
    				else maxLeftParent->_right = maxLeft->_left;
    
    				delete maxLeft;
    			}
    
    			return true;
    		}
    	}
    
    	// 删除的值不存在
    	return false;
    }
    
    // 递归版本
    bool Erase(const K& key)
    {
    	return _Erase(_root, key);
    }
    
    static bool _Erase(Node*& root, const K& key)
    {
    	if (root == nullptr) return false;
    
    	if (root->_key > key) return _Erase(root->_left, key);
    	else if (root->_key < key) return _Erase(root->_right, key);
    	else
    	{
    		Node* del = root;
    
    		if (root->_left == nullptr) root = root->_right;
    		else if (root->_right == nullptr) root = root->_left;
    		else
    		{
    			Node* minRightParent = root;
    			Node* minRight = root->_right;
    			while (minRight->_left)
    			{
    				minRightParent = minRight;
    				minRight = minRight->_left;
    			}
    
    			std::swap(root->_key, minRight->_key);
    
    			if (minRightParent == root) return _Erase(minRightParent->_right, key);
    			else return _Erase(minRightParent->_left, key);
    		}
    
    		delete del;
    
    		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
    • 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

    4. 二叉搜索树的查找和中序遍历

    时间复杂度 O(N): 因为二叉搜索树可能只有左子树或右子树

    // 非递归版本
    bool Find(const K& key)
    {
    	Node* cur = _root;
    	while (cur)
    	{
    		// 当前结点值 大于 key 时,则 key 不可能在右子树
    		// 当前结点值 小于 key 时,则 key 不可能在左子树
    		// 当前结点值 等于 key 时,key 存在
    		if (cur->_key > key) cur = cur->_left;
    		else if (cur->_key < key) cur = cur->_right;
    		else return true;
    	}
    
    	// 不存在
    	return false;
    }
    
    // 递归版本
    bool Find(const K& key)
    {
    	return _Find(_root, key);
    }
    
    // 查找递归
    static bool _Find(Node* root, const K& key)
    {
    	if (root == nullptr) return false;
    
    	if (root->_key > key) return _Find(root->_left, key);
    	else if (root->_key < key) return _Find(root->_right, key);
    	else 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

    二叉搜索树的中序遍历:

    void Inorder()
    {
    	// 调用子函数递归
    	_Inorder(_root);
    	cout << endl;
    }
    
    // 中序递归
    static void _Inorder(Node* root)
    {
    	if (root == nullptr) return;
    
    	_Inorder(root->_left);
    	cout << root->_key << " ";
    	_Inorder(root->_right);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5. 二叉搜索树的拷贝构造、赋值重载和析构

    BSTree<K>(const BSTree<K>& bst)
    	: _root(nullptr)
    {
    	_root = _BSTreeCopy(bst._root);
    }
    
    BSTree<K>& operator=(BSTree<K> bst)
    {
    	std::swap(_root, bst._root);
    
    	return *this;
    }
    
    ~BSTree<K>()
    {
    	_BSTreeDestroy(_root);
    	_root = nullptr;
    }
    
    // 深拷贝递归
    static Node* _BSTreeCopy(Node* root)
    {
    	if (root == nullptr) return nullptr;
    
    	Node* rootCopy = new Node(root->_key);
    
    	rootCopy->_left = _BSTreeCopy(root->_left);
    	rootCopy->_right = _BSTreeCopy(root->_right);
    
    	return rootCopy;
    }
    
    // 销毁递归
    static void _BSTreeDestroy(Node* root)
    {
    	if (root == nullptr) return;
    
    	_BSTreeDestroy(root->_left);
    	_BSTreeDestroy(root->_right);
    
    	delete 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

    三、二叉搜索树的应用

    K模型:结点只存储 key,用于快速查找 key 是否存在

    判断单词 word 是否拼写错误:将词库中的所有单词作为 key 构建一颗二叉搜索树,在 K 模型中查找该单词,查找成功,则拼写正确,否则拼写错误


    KV模型:结点存储 键值对,一个 key 值对应一个 value 值,用于通过 key 快速查找 value

    英语翻译汉语:将词库中的所有单词及其翻译作为 构建一颗二叉搜索树(单词作为 key,单词翻译作为 value),在 KV 模型中查找该单词,查找成功,即可获取单词的中文翻译

  • 相关阅读:
    方案分享:F5机器人防御助企业应对复杂攻击
    中国传统节日春节网页HTML代码 春节大学生网页设计制作成品下载 学生网页课程设计期末作业下载 DW春节节日网页作业代码下载
    go test用法(获取单元测试覆盖率)
    CCES软件做开发,如果仿真器连不进目标板怎么解决?(Failed to connect to processor)
    4. 执行引擎
    PHP调用调用API接口的方法及实现
    树莓派在Raspbian系统(Bookworm)中无法获取RJ45网口eth0或end0的IP地址(没有IPv4的地址无法操作)
    数据挖掘(3)特征化
    Design A Youtube
    基于Ngrok 内网穿透的TCP服务器
  • 原文地址:https://blog.csdn.net/qq_70793373/article/details/130221376