【C++】二叉搜索树的模拟实现

这篇具有很好参考价值的文章主要介绍了【C++】二叉搜索树的模拟实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

🌇个人主页:平凡的小苏
📚学习格言:命运给你一个低的起点,是想看你精彩的翻盘,而不是让你自甘堕落,脚下的路虽然难走,但我还能走,比起向阳而生,我更想尝试逆风翻盘
🛸C++专栏C++内功修炼基地
> 家人们更新不易,你们的👍点赞👍和⭐关注⭐真的对我真重要,各位路 过的友友麻烦多多点赞关注。 欢迎你们的私信提问,感谢你们的转发! 关注我,关注我,关注我,你们将会看到更多的优质内容!!

【C++】二叉搜索树的模拟实现,C++修炼内功,c++,开发语言

一、二叉搜索树的概念

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

1.若他的左子树不为空,则左子树上所有节点的值小于根节点的值

2.若他的右子树不为空,则右子树上所有节点的值大于根节点的值

3.它的左右子树也分别为二叉搜索树

二叉搜索树的结构如下

template <class K,class V>
struct BSTreeNode//生成二叉搜索树的节点
{
	BSTreeNode<K,V>* _left;
	BSTreeNode<K,V>* _right;
	K _key;
	V _value;
	BSTreeNode(const K& key, const V& value)
		:_left(nullptr)
		,_right(nullptr)
		,_key(key)
		,_value(value)
	{}
};

template<class K,class V>
class BSTree//表示整颗二叉搜索树
{
	typedef BSTreeNode<K,V> Node;
public:
	BSTree()
		:_root(nullptr)
	{}
private:
	Node* _root;
};

二、二叉搜索树的查找

a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。

b、最多查找高度次,走到到空,还没找到,这个值不存在。

  • 插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能.

  • 对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二

    叉搜索树的深度的函数,即结点越深,则比较次数越多。

    但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:如下图所示
    【C++】二叉搜索树的模拟实现,C++修炼内功,c++,开发语言

    最优情况下,二叉搜索树为完全二叉树(或者接近完全二叉树),其平均比较次数为:logN

    最差情况下,二叉搜索树退化为单支树(或者类似单支),其平均比较次数为:O(N)

1、递归版本的二叉搜索树的查找

Node* _FindR(Node* root, const K& key)
{
    if (root == nullptr)
    {
        return nullptr;
    }

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

2、非递归版本的二叉搜索树的查找

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

三、二叉搜索树的插入

二叉搜索树的插入需要考虑插入后,需要维持二叉搜索树的形态。

1、二叉搜索树的非递归写法

bool Insert(const K& key, const V& value)
{
    if (_root == nullptr)
    {
        _root = new Node(key,value);
        return true;
    }

    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
        {
            return false;
        }
    }
    cur = new Node(key,value);
    if (parent->_key > key)
    {
        parent->_left = cur;
    }
    else
    {
        parent->_right = cur;
    }
    return true;
}

注意:这里要记录一个父亲节点,来确保是插入在左侧还是插入在右侧

2、二叉搜索树的递归写法

bool _InsertR(Node*& root, const K& key,const V& value)
{
    if (root == nullptr)
    {
        root = new Node(key,value);
        return true;
    }

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

递归写法巧就巧在形参是指针的引用,例如我现在要插入9,下层的root是上一层root->_left的别名, 下层root = new Node(key);即为上一层root->_left=new Node(key);这样插入节点就自动和父节点连接上了。

【C++】二叉搜索树的模拟实现,C++修炼内功,c++,开发语言

四、二叉搜索树的删除

二叉搜索树的节点进行删除后,同样需要维持二叉搜索树的形态。

二叉搜索树的删除无非是三种情况:
【C++】二叉搜索树的模拟实现,C++修炼内功,c++,开发语言

1、二叉搜索树非递归版本的删除

bool Erase(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 (cur == _root)
                {
                    _root = cur->_right;
                }
                else
                {
                    if (parent->_right == cur)
                    {
                        parent->_right = cur->_right;
                    }
                    else
                    {
                        parent->_left = cur->_right;
                    }
                }
            }

            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;
                    }
                }
            }

            else
            {
                Node* parent = cur;
                Node* leftMax = cur->_left;
                while (leftMax->_right)//找到左树的最大节点,即在左树的最右边
                {
                    parent = leftMax;
                    leftMax = leftMax->_right;
                }
                swap(cur->_key, leftMax->_key);//交换数据
				//如上图,如果删除的是3,那么就会和1交换,即是如下这种情况,需要将3的左连接到1的左
                if (parent->_left == leftMax)
                {
                    parent->_left = leftMax->_left;
                }
                //如上图如果删除的是8,则会和6交换,则是如下这种情况,需要将10的有连接到6的左
                else
                {
                    parent->_right = leftMax->_left;
                }
                cur = leftMax;
            }
            delete cur;
            return true;
        }
    }
    return false;
}

1、先通过二叉搜索树的性质找到要删除的节点;

2、找到需要删除的节点后,分三种情况进行讨论:

​ 一、被删除节点的左孩子为空,除了cur等于根节点情况下,其他情况下,父节点的孩子指针由指向被删除节点转为指向被删除节点的右孩子。(如图删除14)

【C++】二叉搜索树的模拟实现,C++修炼内功,c++,开发语言

二、被删除节点的左孩子存在但右孩子为空,除了cur等于根节点情况下,其他情况下,父节点的孩子指针由指向被删除节点转为指向被删除节点的左孩子。(如图删除9)

【C++】二叉搜索树的模拟实现,C++修炼内功,c++,开发语言

三、被删除的节点均不为空,可以选用左树最大节点或者右树最小节点对被删除节点进行值替换,问题转化为第一种或第二种情况。(详见代码注释)

2、二叉搜索树递归版本的删除

bool _EraseR(Node*& root, const K& key)
{
    if (root == nullptr)
    {
        return false;
    }

    if (root->_key < key)
    {
        return _EraseR(root->_right, key);
    }
    else if (root->_key > key)
    {
        return _EraseR(root->_left, key);
    }
    else
    {
        Node* del = root;
        if (root->_left == nullptr)
        {
            root = root->_right;//因为是引用直接修改它的指向就可以
        }
        else if (root->_right == nullptr)
        {
            root = root->_left;
        }
        else
        {
            Node* leftmax = root->_left;
            while (leftmax->_right)//找到左树的最大节点
            {
                leftmax = leftmax->_right;
            }

            swap(root->_key, leftmax->_key);//交换
            swap(root->_value, leftmax->_value);


            return _EraseR(root->_left, key);//再次进行递归删除,这样就转换成了情况1或者情况2 的删除
        }
        delete del;
        return true;
    }

    return false;
}

注意:由于引用的语法,循环版本不能使用引用,因为递归使其每个栈帧不一样了,所以可以使用引用

五、二叉搜索树的源码

1、key/value的搜索模型(节点既存key又存value)

key/value搜索模型指每一个key值,都有与之对应的value值,例如英汉互译,一个英文单词可以对应一个翻译字符串。该模型还可以用于统计相同内容出现次数。文章来源地址https://www.toymoban.com/news/detail-636651.html

2、key/value模型的源码

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include <algorithm>
using namespace std;

template <class K,class V>
struct BSTreeNode//生成二叉搜索树的节点
{
	BSTreeNode<K,V>* _left;
	BSTreeNode<K,V>* _right;
	K _key;
	V _value;
	BSTreeNode(const K& key, const V& value)
		:_left(nullptr)
		,_right(nullptr)
		,_key(key)
		,_value(value)
	{}
};

template<class K,class V>
class BSTree//表示整颗二叉搜索树
{
	typedef BSTreeNode<K,V> Node;
public:
	BSTree()
		:_root(nullptr)
	{}

	BSTree(const BSTree<K,V>& t)
	{
		_root = Copy(t._root);
	}

	BSTree<K, V>& operator=(BSTree<K, V> t)
	{
		swap(_root, t._root);
		return *this;
	}
	bool Insert(const K& key, const V& value)
	{
		if (_root == nullptr)
		{
			_root = new Node(key,value);
			return true;
		}

		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
			{
				return false;
			}
		}
		cur = new Node(key,value);
		if (parent->_key > key)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		return true;
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	bool Erase(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 (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_right == cur)
						{
							parent->_right = cur->_right;
						}
						else
						{
							parent->_left = cur->_right;
						}
					}
				}

				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;
						}
					}
				}

				else
				{
					Node* parent = cur;
					Node* leftMax = cur->_left;
					while (leftMax->_right)
					{
						parent = leftMax;
						leftMax = leftMax->_right;
					}
					swap(cur->_key, leftMax->_key);

					if (parent->_left == leftMax)
					{
						parent->_left = leftMax->_left;
					}
					else
					{
						parent->_right = leftMax->_left;
					}
					cur = leftMax;
				}
				delete cur;
				return true;
			}
		}
		return false;
	}

	Node* FindR(const K& key)
	{
		return _FindR(_root, key);
	}

	bool InsertR(const K& key, const V& value)
	{
		return _InsertR(_root, key,value);
	}

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

	~BSTree()
	{
		Destroy(_root);
	}
private:
	void Destroy(Node*& root)
	{
		if (root == nullptr)
		{
			return;
		}

		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}
	Node* Copy(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* copynewnode = new Node(root->_key,root->_value);
		copynewnode->_left = Copy(root->_left);
		copynewnode->_right = Copy(root->_right);
		return copynewnode;
	}

	bool _EraseR(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}

		if (root->_key < key)
		{
			return _EraseR(root->_right, key);
		}
		else if (root->_key > key)
		{
			return _EraseR(root->_left, key);
		}
		else
		{
			Node* del = root;
			if (root->_left == nullptr)
			{
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* leftmax = root->_left;
				while (leftmax->_right)
				{
					leftmax = leftmax->_right;
				}

				swap(root->_key, leftmax->_key);
				swap(root->_value, leftmax->_value);


				return _EraseR(root->_left, key);
			}
			delete del;
			return true;
		}

		return false;
	}

	bool _InsertR(Node*& root, const K& key,const V& value)
	{
		if (root == nullptr)
		{
			root = new Node(key,value);
			return true;
		}

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

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

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_key << ":" << root->_value << endl;
		_InOrder(root->_right);
	}
private:
	Node* _root;
};

void BSTreeTest()
{
	string arr[] = { "西瓜", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
	BSTree<string, int> countTree;
	for (auto& str : arr)
	{
		auto ret = countTree.FindR(str);
		if (ret == nullptr)
		{
			countTree.InsertR(str, 1);
		}
		else
		{
			ret->_value++;
		}
	}

	countTree.InOrder();
}

到了这里,关于【C++】二叉搜索树的模拟实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【数据结构】二叉搜索树——二叉搜索树的概念和介绍、二叉搜索树的简单实现、二叉搜索树的增删查改

      二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:   (1)若它的左子树不为空,则左子树上所有节点的值都小于根节点的值   (2)若它的右子树不为空,则右子树上所有节点的值都大于根节点的值   (3)它的左右子树也分别为二叉

    2024年02月09日
    浏览(31)
  • 【C++修炼之路】list 模拟实现

    👑作者主页:@安 度 因 🏠学习社区:StackFrame 📖专栏链接:C++修炼之路

    2024年02月16日
    浏览(28)
  • C++力扣题目530--二叉搜索树的最小绝对值

    给你一个二叉搜索树的根节点  root  ,返回  树中任意两不同节点值之间的最小差值  。 差值是一个正数,其数值等于两值之差的绝对值。 示例 1: 示例 2: 树中节点的数目范围是  [2, 104] 0 = Node.val = 105 题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。 注意

    2024年02月02日
    浏览(32)
  • 深入篇【C++】手搓模拟实现二叉搜索树(递归/非递归版本)&&常见应用场景(K模型与KV模型)

    二叉搜索树又称二叉排序树,它或者是一颗空树或者是具有以下性质的二叉树: 1.当它的左子树不为空,则左子树上所有的结点的值都要小于根节点。 2.当它的右子树不为空,则右子树上所有的结点的值都要大于根结点。 3.它的左右子树都是二叉搜索树。 ①.定义结点 二叉树

    2024年02月12日
    浏览(31)
  • 【C++从0到王者】第二十八站:二叉搜索树的应用

    二叉搜索树的在现实世界的应用很广泛,比如Key模型,Key-Value模型就是常见的两种的模型 K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。即就是判断key在不在就可以了。 比如:门禁系统,小区车辆出入系统等等 给一个单词word,判

    2024年02月09日
    浏览(23)
  • 二叉搜索树的实现(递归方式)

    目录 实现思路 插入操作 删除操作 完整代码 测试案例 总结 二叉搜索树(Binary Search Tree,BST)是一种常用的数据结构,它具有以下特点: 左子树上所有节点的值均小于它的根节点的值 右子树上所有节点的值均大于它的根节点的值 左右子树也分别为二叉搜索树 在实际应用中

    2024年02月08日
    浏览(24)
  • 【数据结构】二叉树的模拟实现

    前言:前面我们学习了堆的模拟实现,今天我们来进一步学习二叉树,当然了内容肯定是越来越难的,各位我们一起努力! 💖 博主CSDN主页:卫卫卫的个人主页 💞 👉 专栏分类:数据结构 👈 💯代码仓库:卫卫周大胖的学习日记💫 💪关注博主和博主一起学习!一起努力! 树是一

    2024年02月03日
    浏览(28)
  • 二叉搜索树的实现(C语言)

    目录 前言: 一:准备工作 (1)需要的头文件 (2)树节点结构体描述 (3)初始化 二:指针 三:插入新节点(建树) (1)生成一个新节点 (2)找插入位置 四:查找和遍历 (1)查找 (2)遍历 五:删除节点 六:全部代码 (1)BinarySearchTree.h(声明) (2)BinarySearchTree.c(函数具体实现) (3)test.c(测试) 二叉

    2024年02月06日
    浏览(24)
  • 【数据结构】 二叉搜索树的实现

    二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值 它的左右子树也分别为二叉搜索树 比如以下就为一个人二叉搜

    2024年02月09日
    浏览(30)
  • 数据结构之进阶二叉树(二叉搜索树和AVL树、红黑树的实现)超详细解析,附实操图和搜索二叉树的实现过程图

    绪论​ “生命有如铁砧,愈被敲打,愈能发出火花。——伽利略”;本章主要是数据结构 二叉树的进阶知识,若之前没学过二叉树建议看看这篇文章一篇掌握二叉树,本章的知识从浅到深的 对搜索二叉树的使用进行了介绍和对其底层逻辑的实现进行了讲解 ,希望能对你有所

    2024年02月04日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包