目录
概述
搜索二叉树又称二叉排序树,它遵守二叉树的原则,每个节点都有规则:
- 若它的左节点不为空,则左子树的所有节点都小于根节点
- 若它的右节点不为空,则右子树的所有节点都大于根结点
- 它的左右子树也是搜索二叉树
二叉树的每个节点都包含数据:key值、左节点指针、右节点指针
二叉树的插入:新插入的节点一定是叶子节点,先按照key值找到需要插入节点的位置,然后新建节点在该位置插入新节点
二叉树的删除:先找到需要删除的节点,若无法找到,则删除失败;若找到了,则根据一下三种情况进行判断:1. 该节点的左节点为空;2. 该节点的右节点为空;3. 该节点的左右节点都不为空。若是前两种情况,则将不为空的子树上移即可完成删除,若是第三种情况,需要找到左子树的最大节点或者右子树的最小节点,将找到的极值节点与需要删除的节点的值交换,再删除找到的极值节点。
算法
严格遵守搜索二叉树原则:左节点一定小于根节点,右节点一定大于根节点,无重复值
分别实现了进行循环和递归两种模式的设计方案,递归方案代码更简洁,但对栈帧空间的损耗更大
搜索二叉树的平均时间复杂度是O(log n),但不稳定,存在极值为O(n),极值就是后面的值都往一边插入的情况,基于这种情况,后面通过设计AVL平衡二叉树来弥补这种缺陷
源码
BSTree.h
#pragma once
#include <iostream>
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)
{
_root = copy(t._root);
}
BSTree<K>& operator=(BSTree<K> t)
{
std::swap(_root, t._root);
return *this;
}
~BSTree()
{
destroy(_root);
_root = nullptr;
}
bool insert(const K& key)
{
if (_root == nullptr)
{
_root = new Node(key);
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);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
bool 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 true;
}
}
return false;
}
bool erase(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* minParent = cur;
Node* minRight = cur->_right;
while (minRight->_left)
{
minParent = minRight;
minRight = minRight->_left;
}
cur->_key = minRight->_key;
if (minRight = minParent->_left)
{
minParent->_left = minRight->_right;
}
else
{
minParent->_right = minRight->_right;
}
delete minRight;
}
return true;
}
}
return false;
}
// 用递归实现遍历、插入、查找、删除
void in_order()
{
_in_order(_root);
std::cout << std::endl;
}
bool insert_r(const K& key)
{
return _insert_r(_root, key);
}
bool find_r(const K& key)
{
return _find_r(_root, key);
}
bool erase_r(const K& key)
{
return _erase_r(_root, key);
}
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 destroy(Node* root)
{
if (root != nullptr)
{
destroy(root->_left);
destroy(root->_right);
delete root;
}
}
void _in_order(Node* root)
{
if (root != nullptr)
{
_in_order(root->_left);
std::cout << root->_key << " ";
_in_order(root->_right);
}
}
bool _insert_r(Node*& root, const K& key)
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (root->_key < key)
{
return _insert_r(root->_right, key);
}
else if (root->_key > key)
{
return _insert_r(root->_left, key);
}
else
{
return false;
}
}
bool _find_r(Node* root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (root->_key < key)
{
return _find_r(root->_right, key);
}
else if (root->_key > key)
{
return _find_r(root->_left, key);
}
else
{
return true;
}
}
bool _erase_r(Node*& root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (root->_key < key)
{
return _erase_r(root->_right, key);
}
else if (root->_key > key)
{
return _erase_r(root->_left, key);
}
else
{
Node* del = root;
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
Node* minRight = root->_right;
while (minRight->_left)
{
minRight = minRight->_left;
}
std::swap(root->_key, minRight->_key);
// 转换成在子树中删除节点
return _erase_r(root->_right, key);
}
delete del;
return true;
}
}
private:
Node* _root = nullptr;
};
test.cpp
#include "BSTree.h"
void test()
{
BSTree<int> t1;
t1.insert(2);
t1.insert(4);
t1.insert(6);
t1.insert(8);
t1.insert_r(1);
t1.insert_r(3);
t1.insert_r(5);
t1.insert_r(7);
t1.insert(-10);
t1.insert(-10); // 重复值无法插入
t1.insert(-20);
t1.insert(0);
t1.in_order(); // -20 -10 0 1 2 3 4 5 6 7 8
// 中序遍历,自动实现递增排序
BSTree<int> t2(t1);
t2.in_order(); // -20 -10 0 1 2 3 4 5 6 7 8
std::cout << t2.find(0) << std::endl; // 1
std::cout << t2.find_r(10) << std::endl; // 0
t2.~BSTree();
t2.in_order();
BSTree<int> t3 = t1;
t3.erase(0);
t3.erase(8);
t3.erase(4);
t3.erase_r(1);
t3.erase_r(5);
t3.in_order(); // -20 -10 2 3 6 7
}
int main()
{
test();
return 0;
}