本章重点
- 二叉搜索树的实现
- 二叉树搜索树应用分析
- 二叉树进阶面试题
1.二叉搜索树的实现
1.1 概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
二叉搜索树的性质保证了其中序遍历是有序的,同时还天然去重,所以也称为二叉排序树
1.2 基础结构
template<typename K>
struct BSTreeNode
{
BSTreeNode(const K& key)
:_key(key)
, _left(nullptr)
, _right(nullptr)
{
}
K _key;
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
};
template<typename K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
BSTree()
:_root(nullptr)
{ }
//拷贝构造
BSTree(const BSTree<K>& bst)
{
_root = Copy(bst._root);
}
//赋值拷贝
BSTree<K>& operator=(BSTree<K> bst)
{
swap(_root, bst._root);
return *this;
}
~BSTree()
{
Destory(_root);
}
//插入
bool Insert(const K& key);//循环
bool InsertR(const K& key);//递归
//查找
bool Find(const K& key);//循环
bool FindR(const K& key);//递归
//删除
bool Erase(const K& key);//循环
bool EraseR(const K& key);//递归
// 中序遍历
void InOrder();
private:
bool _InsertR(Node*& root, const K& key);
bool _EraseR(Node*& root, const K& key);
bool _FindR(Node* root, const K& key);
void _InOrder(Node* root);
//拷贝构造子函数
Node* Copy(Node* other)
{
if (other == nullptr)
return nullptr;
Node* newNode = new Node(other->_key);
newNode->_left = Copy(other->_left);
newNode->_right = Copy(other->_right);
return newNode;
}
//析构函数子函数
void Destory(Node*& root)
{
if (root == nullptr)
return;
Destory(root->_left);
Destory(root->_right);
delete root;
root = nullptr;
}
private:
Node* _root;
};
1.3 基础功能
1.3.1 查找
-
从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
-
最多查找高度次,走到到空,还没找到,这个值不存在。
//查找
bool Find(const K& key)//循环
{
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
cur = cur->_left;
}
else if (key > cur->_key)
{
cur = cur->_right;
}
else
{
return true;
}
}
return false;
}
//---------------------------------------------------
bool FindR(const K& key)//递归
{
return _FindR(_root, key);
}
bool _FindR(Node* root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (key < root->_key)
{
return _FindR(root->_left, key);
}
else if (key > root->_key)
{
return _FindR(root->_right, key);
}
else
{
return true;
}
}
1.3.2 插入
-
树为空,则直接新增节点,赋值给root指针
-
树不空,按二叉搜索树性质查找插入位置,插入新节点
//插入
bool Insert(const K& key)//循环
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
Node* newNode = new Node(key);
if (key < parent->_key)
{
parent->_left = newNode;
}
else
{
parent->_right = newNode;
}
return true;
}
//---------------------------------------------------
bool InsertR(const K& key)//递归
{
return _InsertR(_root, key);
}
bool _InsertR(Node*& root, const K& key)
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (key < root->_key)
{
return _InsertR(root->_left, key);
}
else if (key > root->_key)
{
return _InsertR(root->_right, key);
}
else
{
return false;
}
}
1.3.3 删除
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程
如下:
情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题–替换法删除(左树最大值,右树最小值)
//删除
bool Erase(const K& key)//循环
{
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
if (cur->_left == nullptr)
{
if (parent == nullptr)
{
_root = cur->_right;
}
else if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
else if (cur->_right == nullptr)
{
if (parent == nullptr)
{
_root = cur->_left;
}
else if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
else
{
Node* l_max_parent = cur;
Node* l_max = cur->_left;
while (l_max->_right)
{
l_max_parent = l_max;
l_max = l_max->_right;
}
swap(cur->_key, l_max->_key);
if (l_max_parent == cur)
{
l_max_parent->_left = l_max->_left;
}
else
{
l_max_parent->_right = l_max->_left;
}
delete l_max;
return true;
}
}
}
return false;
}
//---------------------------------------------------
bool EraseR(const K& key)//递归
{
return _EraseR(_root, key);
}
bool _EraseR(Node*& root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (root->_key > key)
{
return _EraseR(root->_left, key);
}
else if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else
{
Node* to_delete = root;
if (root->_left == nullptr) //1.左为空
{
root = root->_right;
}
else if (root->_right == nullptr) //2.右为空
{
root = root->_left;
}
else //3.左右不为空
{
Node* l_max = root->_left;
while (l_max->_right)
{
l_max = l_max->_right;
}
swap(root->_key, l_max->_key);
return _EraseR(root->_left, key);//转换为删除左子树最小节点
}
delete to_delete;
return true;
}
}
1.3.4 中序遍历
// 中序遍历
void InOrder()
{
_InOrder(_root);
cout << endl;
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
1.4 源码
#pragma once
#include<iostream>
using namespace std;
template<typename K>
struct BSTreeNode
{
BSTreeNode(const K& key)
:_key(key)
, _left(nullptr)
, _right(nullptr)
{
}
K _key;
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
};
template<typename K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
BSTree()
:_root(nullptr)
{ }
BSTree(const BSTree<K>& bst)
{
_root = Copy(bst._root);
}
BSTree<K>& operator=(BSTree<K> bst)
{
swap(_root, bst._root);
return *this;
}
~BSTree()
{
Destory(_root);
}
//插入
bool Insert(const K& key)//循环
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
Node* newNode = new Node(key);
if (key < parent->_key)
{
parent->_left = newNode;
}
else
{
parent->_right = newNode;
}
return true;
}
bool InsertR(const K& key)//递归
{
return _InsertR(_root, key);
}
//查找
bool Find(const K& key)//循环
{
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
cur = cur->_left;
}
else if (key > cur->_key)
{
cur = cur->_right;
}
else
{
return true;
}
}
return false;
}
bool FindR(const K& key)//递归
{
return _FindR(_root, key);
}
//删除
bool Erase(const K& key)//循环
{
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
if (cur->_left == nullptr)
{
if (parent == nullptr)
{
_root = cur->_right;
}
else if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
else if (cur->_right == nullptr)
{
if (parent == nullptr)
{
_root = cur->_left;
}
else if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
else
{
Node* l_max_parent = cur;
Node* l_max = cur->_left;
while (l_max->_right)
{
l_max_parent = l_max;
l_max = l_max->_right;
}
swap(cur->_key, l_max->_key);
if (l_max_parent == cur)
{
l_max_parent->_left = l_max->_left;
}
else
{
l_max_parent->_right = l_max->_left;
}
delete l_max;
return true;
}
}
}
return false;
}
bool EraseR(const K& key)//递归
{
return _EraseR(_root, key);
}
// 中序遍历
void InOrder()
{
_InOrder(_root);
cout << endl;
}
private:
bool _InsertR(Node*& root, const K& key)
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (key < root->_key)
{
return _InsertR(root->_left, key);
}
else if (key > root->_key)
{
return _InsertR(root->_right, key);
}
else
{
return false;
}
}
bool _EraseR(Node*& root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (root->_key > key)
{
return _EraseR(root->_left, key);
}
else if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else
{
Node* to_delete = root;
if (root->_left == nullptr) //1.左为空
{
root = root->_right;
}
else if (root->_right == nullptr) //2.右为空
{
root = root->_left;
}
else //3.左右不为空
{
Node* l_max = root->_left;
while (l_max->_right)
{
l_max = l_max->_right;
}
swap(root->_key, l_max->_key);
return _EraseR(root->_left, key);//转换为删除左子树最小节点
}
delete to_delete;
return true;
}
}
bool _FindR(Node* root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (key < root->_key)
{
return _FindR(root->_left, key);
}
else if (key > root->_key)
{
return _FindR(root->_right, key);
}
else
{
return true;
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
Node* Copy(Node* other)
{
if (other == nullptr)
return nullptr;
Node* newNode = new Node(other->_key);
newNode->_left = Copy(other->_left);
newNode->_right = Copy(other->_right);
return newNode;
}
void Destory(Node*& root)
{
if (root == nullptr)
return;
Destory(root->_left);
Destory(root->_right);
delete root;
root = nullptr;
}
private:
Node* _root;
};
2.二叉树搜索树应用分析
- key的搜索模型(快速判断在不在)
- 门禁系统
- 小区车辆出入系统
- 单词拼写检查系统
- …
- key/value的搜索模型(通过一个值找另一个值)
- 商场的车辆出入系统
- 高铁实名制车票系统
- …
3.二叉树进阶面试题
这些题目更适合使用C++完成,难度也更大一些