目录
1.二叉搜索树概念
2.二叉搜索树非递归实现
2.3搜索二叉树的删除(最重要也是最有难度的接口)
3.(key)二叉搜索树递归实现
4.(KV)二叉搜索树递归实现
搜索二叉树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树
下面都大部分例子都由下面这可搜索二叉树来距离:
2.1 二叉搜索数的查找一个节点时间复杂度O(N),应是最坏
2.1二叉搜索树的插入
思路:
bool Insert(const K& key)
{
//空树直接插入
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* cur = _root;
//保留要插入位置的父节点
Node* parent = nullptr;
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);
//判断在父节点的左还是右
if (parent->_key > key)
{
parent->_left = cur;
return true;
}
else
{
parent->_right = cur;
return true;
}
}
2.2搜索二叉树找
思路和插入差不多不赘述
Node* Find(const K& key)
{
Node* cur = _root;
while(cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
处理带两个节点方法:替换法,选择key节点左子树的最大或者右子树最小的节点,把值给key节点,删除的就是带一个节点的了;
bool Erase(const K& key)//删除
{
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
//寻找key
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
//找到key,如果只有一个子节点
if (cur->_left == nullptr)
{
if (cur=_root)
{
//没有左子树cur还是头节点
_root = cur->_right;
delete cur;
}
else
{
//cur是parent是左还是右不明确
if (parent->_left == cur) {
parent->_left = cur->_right;
}
else {
parent->_right = cur->_right;
}
delete cur;
}
}
else if (cur->_right == nullptr)
{
//没有左子树cur还是头节点
if (cur = _root)
{
_root = cur->_left;
delete cur;
}
else
{
if (parent->_left == cur) {
parent->_left = cur->_left;
}
else {
parent->_right = cur->_left;
}
delete cur;
}
}
else
{
parent = cur;//保存key节点
Node* curparent = cur;//保存cur的父节点
cur = cur->_right;
//右子树最小或者左子树最大
while (cur->_left != nullptr)
{
curparent = cur;
cur = cur->_left;
}
parent->_key = cur->_key;// 右子树最小替换
if (cur->_left == nullptr)
{
//cur是parent是左还是右不明确
if (curparent->_left == cur) {
curparent->_left = cur->_right;
}
else {
curparent->_right = cur->_right;
}
}
else if (cur->_right == nullptr)
{
if (curparent->_left == cur) {
curparent->_left = cur->_left;
}
else {
curparent->_right = cur->_left;
}
}
delete cur;
}
return true;
}
}
return false;
}
2.4二叉搜索数全部代码
#pragma once
#include
using namespace std;
template
struct BSTreeNode
{
BSTreeNode(const K& key)
:_left(nullptr)
,_right(nullptr)
,_key(key)
{}
BSTreeNode* _left;
BSTreeNode* _right;
K _key;
};
template
class BSTree
{
typedef BSTreeNode Node;
public:
BSTree()
:_root(nullptr)
{}
bool Insert(const K& key)
{
//空树直接插入
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* cur = _root;
//保留要插入位置的父节点
Node* parent = nullptr;
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);
//判断在父节点的左还是右
if (parent->_key > key)
{
parent->_left = cur;
return true;
}
else
{
parent->_right = cur;
return true;
}
}
Node* Find(const K& key)
{
Node* cur = _root;
while(cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
bool Erase(const K& key)//删除
{
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
//寻找key
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
//找到key,如果只有一个子节点
if (cur->_left == nullptr)
{
if (cur=_root)
{
//没有左子树cur还是头节点
_root = cur->_right;
delete cur;
}
else
{
//cur是parent是左还是右不明确
if (parent->_left == cur) {
parent->_left = cur->_right;
}
else {
parent->_right = cur->_right;
}
delete cur;
}
}
else if (cur->_right == nullptr)
{
//没有左子树cur还是头节点
if (cur = _root)
{
_root = cur->_left;
delete cur;
}
else
{
if (parent->_left == cur) {
parent->_left = cur->_left;
}
else {
parent->_right = cur->_left;
}
delete cur;
}
}
//else ///2、左右都不为空,替换法删除
//{
// Node* minRight = cur->_right;
// while (minRight->_left)
// {
// minRight = minRight->_left;
// }
// K min = minRight->_key;
// // 递归调用自己去删除替换节点,一定会走到左为空的情况处理
// this->Erase(min);
// cur->_key = min;
//}
else
{
parent = cur;//保存key节点
Node* curparent = cur;//保存cur的父节点
cur = cur->_right;
//右子树最小或者左子树最大
while (cur->_left != nullptr)
{
curparent = cur;
cur = cur->_left;
}
parent->_key = cur->_key;// 右子树最小替换
if (cur->_left == nullptr)
{
//cur是parent是左还是右不明确
if (curparent->_left == cur) {
curparent->_left = cur->_right;
}
else {
curparent->_right = cur->_right;
}
}
else if (cur->_right == nullptr)
{
if (curparent->_left == cur) {
curparent->_left = cur->_left;
}
else {
curparent->_right = cur->_left;
}
}
delete cur;
}
return true;
}
}
return false;
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
void InOrder()
{
_InOrder(_root);
cout<
key的搜索场景(适用于在不在的问题)
namespace key
{
template
struct BinarySearchNode
{
BinarySearchNode(const K& key)
:_left(nullptr)
, _right(nullptr)
, _key(key)
{}
BinarySearchNode* _left;
BinarySearchNode* _right;
K _key;
};
template
class BSTree
{
typedef BinarySearchNode Node;
public:
BSTree()
:_root(nullptr)
{}
Node* FindR(const K& key)
{
return _FindR(_root, key);
}
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
~BSTree()
{
_Destory(_root);
_root = nullptr;
}
BSTree(const BSTree& t)
{
_root = Copy(t._root);
}
BSTree& operator=(BSTree t)
{
swap(_root, t._root);
return *this;
}
private:
Node* _FindR(Node* root, const K& key)
{
if (root == nullptr)
{
return nullptr;
}
if (root->_key < key)
{
return _FindR(root->left, key);
}
else if (root->_key > key)
{
return _FindR(root->right, key);
}
else
{
return root;
}
}
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 _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
{
if (root->_left == nullptr)
{
Node* del = root;
root = root->_right;
delete del;
}
else if (root->_right == nullptr)
{
Node* del = root;
root = root->_left;
delete del;
}
else
{
Node* minRight = root->_right;
while (minRight->_left)
{
minRight = minRight->_left;
}
K min = minRight->_key;
// 转换成在root的右子树删除min
_EraseR(root->_right, min);
root->_key = min;
}
return true;
}
}
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* copyNode = new Node(root->_key);
copyNode->_left = Copy(root->_left);
copyNode->_right = Copy(root->_right);
return copyNode;
}
Node* _root;
};
}
搜索场景
3.统计一个文本出现次数
namespace KV
{
template
struct BSTreeNode
{
BSTreeNode* _left;
BSTreeNode* _right;
K _key;
V _value;
BSTreeNode(const K& key, const V& value)
: _left(nullptr)
, _right(nullptr)
, _key(key)
, _value(value)
{}
};
template
class BSTree
{
typedef BSTreeNode Node;
private:
// 如果树中已经存在key,返回false
bool _InsertR(Node*& root, const K& key, const V& value)
{
if (root == NULL) // 插入
{
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;
}
}
// 如果树中不存在key,返回false
// 存在,删除后,返回true
bool _EraseR(Node*& root, const K& key)
{
if (root == NULL)
{
return false;
}
if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else if (root->_key > key)
{
return _EraseR(root->_left, key);
}
else
{
// 找到了,root就是要删除的节点
if (root->_left == nullptr)
{
Node* del = root;
root = root->_right;
delete del;
}
else if (root->_right == nullptr)
{
Node* del = root;
root = root->_left;
delete del;
}
else
{
Node* minRight = root->_right;
while (minRight->_left)
{
minRight = minRight->_left;
}
K kmin = minRight->_key;
V vMin = minRight->_value;
// 转换成在root的右子树删除min
_EraseR(root->_right, kmin);
root->_key = kmin;
root->_value = vMin;
}
return true;
}
}
void _Destory(Node* root)
{
if (root == NULL)
{
return;
}
_Destory(root->_left);
_Destory(root->_right);
delete root;
}
Node* _Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* copyNode = new Node(root->_key, root->_value);
copyNode->_left = _Copy(root->_left);
copyNode->_right = _Copy(root->_right);
return copyNode;
}
public:
BSTree()
:_root(nullptr)
{}
BSTree(const BSTree& t)
{
_root = _Copy(t._root);
}
// t1 = t2
BSTree& operator=(BSTree t)
{
swap(_root, t._root);
return *this;
}
~BSTree()
{
_Destory(_root);
_root = nullptr;
}
// 涉及深浅拷贝,需要实现拷贝构造 operator=等
bool InsertR(const K& key, const V& value)
{
return _InsertR(_root, key, value);
}
Node* FindR(const K& key)
{
return _FindR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
void _InOrder(Node* root)
{
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;
};
}