二叉树(进阶)

1.内容安排说明

二叉树在前面c数据结构阶段;已经讲过了;本节取名二叉树进阶的原因是:
1.map和set特性需要先铺垫二叉搜索树 ,而二叉搜索树也是一种树形结构;
2. 二叉搜索树的特性了解,有助于更好的理解map和set的特性
3. 二叉树中部分面试题稍微有点难度,在前面讲解大家不容易接受,且时间长容易忘
4. 有些OJ题使用C语言方式实现比较麻烦,比如有些地方要返回动态开辟的二维数组,非常麻烦

因此本节借二叉树搜索树,对二叉树部分进行收尾总结。

2. 二叉搜索树

2.1二叉搜索树的概念

二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
1.若它的左子树不为空,则左子树上所有节点的值都小于根节点的值;
2.若它的右子树不为空,则右子树上所有节点的值都大于根节点的值;
3.它的左右子树也分别为二叉搜索树
在这里插入图片描述

2.2二叉搜索树的实现

#pragma once
#include <iostream>
using namespace std;
template <class K>
struct BSTreeNode
{
	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;
	K _key;
	BSTreeNode(const K& key)
		:_left(nullptr)
		, _right(nullptr)
		, _key(key)
	{}

};
template <class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	
	void Swap(Node* a, Node* b)
	{
		Node s = *a;
		*a = *b;
		*b = s;
	}
	
   //出先两个函数的原因是:这里使用了递归;递归需要使用递归类控制循环次数;所以使用类get函数进行调用;
	bool Find(const K& key)
	{
		return _Find(_root, key);

	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	bool Insert(const K& key)
	{
		return _Insert(_root, key);
	}
	bool Erase(const K& key)
	{
		return _Erase(_root, key);
	}
	~BSTree()
	{
		Destroy(_root);
	}
	BSTree()
	{

	}
	BSTree(const BSTree<K>& t )
	{
		_root = Copy(t._root);
	}
	BSTree<K>& operator = (const BSTree<K> t)
	{
		swap(_root,t._root);
	    return *this;
	}
	

private:
	Node* Copy(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* newRoot = new Node(root->_key);
		newRoot->_left = Copy(root->_left);
		newRoot->_right = Copy(root->_right);
		return newRoot;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}
	bool _Find(Node* root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		else
		{
			if (key > root->_key)
			{
				_Find(root->_right, key);
			}
			else if (key < root->_key)
			{
				_Find(root->_left, key);
			}
			else
			{
				return true;
			}
		}

	}
	bool _Insert(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}
		if (key > root->_key)
		{
			_Insert(root->_right, key);

		}
		else if (key < root->_key)
		{
			_Insert(root->_left, key);

		}
		else
		{
			return false;
		}

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

		}
		else
		{
			if (root->_right == nullptr)
			{
				Node* n = root;
				root = root->_left;
				delete n;
				return true;
			}
			else if (root->_left == nullptr)
			{
				Node* n = root;
				root = root->_right;
				delete n;
				return true;
			}
			else
			{
				Node* cur = root->_left;
				while (cur->_right)
				{
					cur = cur->_right;
				}
				swap(cur->_key, root->_key);
				return _Erase(_root->_right, key);

			}
		}
	}
    }
	void Destroy(Node*& root)
	{
		if (root == nullptr)
		{
			return;
		}
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
		root = nullptr;
	}

private:

		Node* _root = nullptr;
};

2.3二叉树的性能:

二叉树的时间复杂度:这里的是时间复杂度测量的是查找的时间复杂度;原因:删除和添加都修要使用查找哦来进行;
最优情况下,二叉搜索树为完全二叉树(或者接近完全二叉树),其平均比较次数为: l o g 2 N log_2 N log2N
最差情况下,二叉搜索树退化为单支树(或者类似单支),其平均比较次数为: N 2 \frac{N}{2} 2N
在这里插入图片描述

搜索二叉树的应用

k 模型

定义:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到
的值。
例子

比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:

  • 以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
  • 在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。

kv模型

定义:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:

  • 比如:英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对;
  • 再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word, count>就构成一种键值对。
// 改造二叉搜索树为KV结构
template<class K, class V>
struct BSTNode
 {
 BSTNode(const K& key = K(), const V& value = V())
   : _pLeft(nullptr) , _pRight(nullptr), _key(key), _Value(value)
 {}
  BSTNode<T>* _pLeft;
 BSTNode<T>* _pRight;
 K _key;
    V _value
 };
template<class K, class V>
class BSTree
 {
 typedef BSTNode<K, V> Node;
 typedef Node* PNode;
public:
 BSTree(): _pRoot(nullptr){}
 PNode Find(const K& key);
 bool Insert(const K& key, const V& value)
 bool Erase(const K& key)
private:
 PNode _pRoot;
比特就业课
2.5 二叉搜索树的性能分析
插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
 };
void TestBSTree3()
{
 // 输入单词,查找单词对应的中文翻译
 BSTree<string, string> dict;
 dict.Insert("string", "字符串");
 dict.Insert("tree", "树");
 dict.Insert("left", "左边、剩余");
 dict.Insert("right", "右边");
 dict.Insert("sort", "排序");
 // 插入词库中所有单词
 string str;
 while (cin>>str)
 {
 BSTreeNode<string, string>* ret = dict.Find(str);
 if (ret == nullptr)
 {
 cout << "单词拼写错误,词库中没有这个单词:" <<str <<endl;
 }
 else
 {
 cout << str << "中文翻译:" << ret->_value << endl;
 }
 }
}
void TestBSTree4()
{
 // 统计水果出现的次数
 string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", 
"苹果", "香蕉", "苹果", "香蕉" };
 BSTree<string, int> countTree;
 for (const auto& str : arr)
 {
 // 先查找水果在不在搜索树中
 // 1、不在,说明水果第一次出现,则插入<水果, 1>
 // 2、在,则查找到的节点中水果对应的次数++
 //BSTreeNode<string, int>* ret = countTree.Find(str);
 auto ret = countTree.Find(str);
 if (ret == NULL)
 {
 countTree.Insert(str, 1);
 }
 else
 {
 ret->_value++;
 }
 }
 countTree.InOrder();
}

搜索二叉树的实现和应用源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值