目录
前言
二叉搜索树为后序所学的 map 和 set 做准备
二叉搜索树
概念
二叉搜索树(BST,Binary Search Tree)又称二叉排序树,它可以是一棵空树
二叉树搜索树具有以下性质:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
下面就是一颗二叉搜索树
二叉搜索树的应用
K模型
K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。 比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:
将每个单词作为key,构建一棵二叉搜索树
在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
这种实现方式通过上面的代码的Find就可以完成,只不过模板参数转化成了string类型。
KV模型
KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方
式在现实生活中非常常见:
如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英
文单词与其对应的中文<word, chinese>就构成一种键值对;
再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出
现次数就是<word, count>就构成一种键值对。
实现二叉搜索树
总的框架
我们首先需要封装一个节点类,结点类当中包含三个成员变量:结点的值、左指针、右指针,结点类当中只需实现一个构造函数即可
二叉搜索树类(BSTree)里面的成员变量只要包含一个根节点 _root 即可
为了书写简单,对 BSTreeNode 进行 typedef 为 Node
template<class K>
// K 模型
struct BSTreeNode // Binary Search Tree Node
{
K _key;
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
BSTreeNode(const K& key)
:_key(key)
, _left(nullptr)
, _right(nullptr)
{}
};
template<class K>
class BSTree // Binary Search Tree 二叉搜索树
{
typedef BSTreeNode<K> Node;
public:
//构造函数 构造一个空树
BSTree()
:_root(nullptr)
{}
//拷贝构造
BSTree(const BSTree<K>& t)
{}
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{}
//析构
~BSTree()
{}
bool Insert(const K& key)
{}
bool Find(const K& key)
{}
bool Erase(const K& key)
{}
//中序遍历树
void InOrder()
{}
private:
Node* _root;
};
插入函数
根据二叉搜索树的性质,其插入操作非常简单:
- 如果是空树,则直接将插入结点作为二叉搜索树的根结点
- 如果不是空树,则按照二叉搜索树的性质进行结点的插入
若不是空树,插入结点的具体操作如下:
- 若待插入结点的值小于根结点的值,则需要将结点插入到左子树当中
- 若待插入结点的值大于根结点的值,则需要将结点插入到右子树当中
- 若待插入结点的值等于根结点的值,则插入结点失败(二叉搜索树)
插入成功返回 true,插入失败返回 false
// 不可能存在在中间插数据,就是要插入在下面为空的位置
// 一样的数据,不同的插入顺序会形状不一样
bool insert(const K& key)
{
if (nullptr == _root)
{
_root = new Node(key);
return true;
}
Node* cur = _root;
Node* parent = cur;
while (cur)
{
parent = cur; // 记录上一个
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 if (parent->_key < key)
parent->right = cur;
return true;
}
查找函数
找到返回true,反之返回false
bool 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 true;
}
return false;
}
中序遍历
二叉搜索树进行中序遍历,遍历出来的结果是有序的(升序),二叉搜索树一般都是去重的,即没有相同的值
//遍历树
void InOrder()
{
_InOrder(_root); // 因为_root是私有的,需要另写一个函数
cout << endl;
}
private:
// 中序遍历
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
删除函数(重点)
两种情况
1、如果要删除的Node的左为空,则他的父亲指向Node的右,
如果要删除的Node的右为空,则他的父亲指向Node的左。
最后删除自己
叶子结点也适用
特殊情况:删除根节点时(没有父亲),将Node的左或Node的右设置为新的根。
左为空删除的情况(右边是一样的):
特殊情况
下面是代码实现:
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;
}
2、 如果要删除的Node的左右都不为空,不能直接删除,使用替代法
可以找左子树的最大的结点(key)或者找右子树的最小的结点(key)去替代要删除的Node
(左子树最右边的节点 ) (右子树最左边的结点)
示例 寻找右子树的最小的结点去替代要删除的Node(寻找左子树的最大的结点去替代要删除的Node是一样的):
特殊情况:
代码实现:
else//替换删除
{
Node* parent = cur;
Node* minright = cur->right;
while (minright->left) // 找右子树最小的结点
{
parent = minright;
minright = minright->left;
}
cur->_key = minright->_key;
if (minright == parent->left)
{
parent->left = minright->right; // 连接上最小节点(最左边)的右边
}
else
{
parent->right = minright->right; // 当循环没有进去时(特殊情况)
}
delete minright;
}
return true;
}
拷贝构造
//拷贝构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
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;
}
赋值重载
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
析构函数
//析构
~BSTree()
{
Destroy(_root);
_root = nullptr;
}
private:
void Destroy(Node* root)
{
if (root == nullptr)
{
return;
}
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
完整代码
#pragma once
namespace hek1{
// K 模型
template<class K>
struct BSTreeNode // Binary Search Tree Node
{
BSTreeNode<K>* left;
BSTreeNode<K>* right;
K _key;
BSTreeNode(const K& key)
:_key(key)
,left(nullptr)
,right(nullptr)
{}
};
template<class K>
class BSTree // Binary Search Tree 二叉搜索树
{
typedef BSTreeNode<K> Node;
public:
//构造函数
BSTree()
:_root(nullptr)
{}
//拷贝构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
//析构
~BSTree()
{
Destroy(_root);
_root = nullptr;
}
// 不可能存在在中间插数据,就是要插入在下面为空的位置
// 一样的数据,不同的插入顺序会形状不一样
bool insert(const K& key)
{
if (nullptr == _root)
{
_root = new Node(key);
return true;
}
Node* cur = _root;
Node* parent = cur;
while (cur)
{
parent = cur; // 记录上一个
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 if (parent->_key < key)
parent->right = cur;
return true;
}
bool 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 true;
}
return false;
}
// 两种情况
// 1、如果要删除的Node的左为空,则他的父亲指向Node的右,
// 如果要删除的Node的右为空,则他的父亲指向Node的左。
// 最后删除自己
// 叶子结点也适用
// 2、 如果要删除的Node的左右都不为空,不能直接删除,使用替代法
// 可以找左子树的最大的结点(key)或者找右子树的最小的结点(key)去替代要删除的Node
// 最右边的节点 最左边的结点
bool earse(const K& 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* parent = cur;
Node* minright = cur->right;
while (minright->left) // 找右子树最小的结点
{
parent = minright;
minright = minright->left;
}
cur->_key = minright->_key;
if (minright == parent->left)
{
parent->left = minright->right; // 连接上最小节点(最左边)的右边
}
else
{
parent->right = minright->right; // 当循环没有进去时
}
delete minright;
}
return true;
2
//else
//{
// Node* cur2 = cur->left; // 左子树
// Node* parent2 = cur;
// while (cur2->right)// 找左子树最大的结点
// {
// parent2 = cur2;
// cur2 = cur2->right;
// }
// // 替代删除
// cur->_key = cur2->_key;
// // 第一个结点的下一个
// if (parent2->right == cur2) {
// parent2->right = cur2->left;
// }
// else { // 当循环没有进去时
// parent2->left = cur2->left;
// }
// delete cur2;
// }
// return true;
}
}
return false;
}
void _InOrder(Node* root) // 注意是Node* 类型的
{
if (root == nullptr) return;
_InOrder(root->left);
cout << root->_key <<" ";
_InOrder(root->right);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
private:
Node* _root = nullptr;
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 Destroy(Node* root)
{
if (root == nullptr)
{
return;
}
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
};
}
性能分析
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树
对于有n个结点的二叉搜索树:
1、最优的情况下,二叉搜索树为完全二叉树,其平均比较次数为:O(logN)
2、最差的情况下,二叉搜索树退化为单支树,其平均比较次数为:O(N / 2)
所以实际上,,二叉搜索树在极端情况下是没办法保证效率的,因此由二叉搜索树又衍生出来了AVL树、红黑树,
补充KV模型的部分实现
#pragma once
template<class K,class V>
struct BSTreeNode // Binary Search Tree Node
{
BSTreeNode<K,V>* left;
BSTreeNode<K,V>* right;
K _key;
V _value; // 添加value
BSTreeNode(const K& key, const V& value)
:_key(key)
,_value(value)
, left(nullptr)
, right(nullptr)
{}
};
//
template<class K,class V>
class BSTree // Binary Search Tree 二叉搜索树
{
typedef BSTreeNode<K,V> Node;
public:
// 不可能存在在中间插数据,就是要插入在下面为空的位置
// 一样的数据,不同的插入顺序会形状不一样
bool insert(const K& key,const V& value)
{
if (nullptr == _root)
{
_root = new Node(key,value);
return true;
}
Node* cur = _root;
Node* parent = cur;
while (cur)
{
parent = cur;
if (cur->_key > key)
cur = cur->left;
else if (cur->_key < key)
cur = cur->right;
else
return false;
}
cur = new Node(key,value);
if (parent->_key > key)
parent->left = cur;
else if (parent->_key < key)
parent->right = cur;
return true;
}
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;
}
void _InOrder(Node* root) // 注意是Node* 类型的
{
if (root == nullptr) return;
_InOrder(root->left);
cout << root->_key << ":" << root->_value<<endl;
_InOrder(root->right);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
private:
Node* _root = nullptr;
};
}
K模型递归实现
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_key(key)
, _left(nullptr)
, _right(nullptr)
{}
};
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
BSTree()//构造
:_root(nullptr)
{}
BSTree(const BSTree<K>& t)//不能直接insert,因为顺序不一样,形状不一样
{
_root = Copy(t._root);
}
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
~BSTree()//析构
{
Destory(_root);
_root = nullptr;
}
void Inorder()
{
_Inorder(_root);
cout << endl;
}
// 递归版本实现插入、删除和查找
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
bool FindR(const K& key)
{
return _FindR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
private:
void Destory(Node* root)
{
if (root == nullptr)
{
return;
}
Destory(root->_left);
Destory(root->_right);
delete root;
}
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;
}
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->_right == nullptr)
{
root = root->_left;
}
else if (root->_left == nullptr)
{
root = root->_right;
}
else
{
Node* minright = root->_right;
while (minright->_left)
{
minright = minright->_left;
}
swap(root->_key, minright->_key);//交换之后,递归到子树删交换的值,当然直接赋值不删也行,不过递归删掉就不是key了
return _EraseR(root->_right, key);
}
delete del;
return true;
}
}
bool _InsertR(Node*& root, const K& key)//加引用,与上层调用链接起来,非常巧妙
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (root->_key < key)
{
return _InsertR(root->_right, key);
}
else if (root->_key > key)
{
return _InsertR(root->_left, key);
}
else
{
return false;
}
}
bool _FindR(Node* root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (root->_key == key)
{
return true;
}
else if (root->_key > key)
{
_FindR(root->_left, key);
}
else
{
_FindR(root->_right, key);
}
}
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_key << " ";
_Inorder(root->_right);
}
Node* _root = nullptr;
};