插入代码参考
红黑树的插入和删除完整代码
#include <iostream>
#include <cassert>
using namespace std;
enum colour {
RED,
BLACK,
};
template<class K, class V>
struct RBTreeNode {
RBTreeNode<K, V> *_left;
RBTreeNode<K, V> *_right;
RBTreeNode<K, V> *_parent;
pair<K, V> _kv;
colour _col;
RBTreeNode(const pair<K, V> &kv)
: _left(nullptr),
_right(nullptr),
_parent(nullptr),
_kv(kv),
_col(RED) {
}
};
template<class K, class V>
class RBTree {
typedef RBTreeNode<K, V> Node;
public:
bool insert(const pair<K, V> &kv) {
if (_root == nullptr) {
_root = new Node(kv);
_root->_col = BLACK;
return true;
}
Node *parent = nullptr;
Node *cur = _root;
while (cur) {
if (cur->_kv.first > kv.first) {
parent = cur;
cur = cur->_left;
}
else if (cur->_kv.first < kv.first) {
parent = cur;
cur = cur->_right;
} else {
return false;
}
}
cur = new Node(kv);
if (parent->_kv.first > kv.first) {
parent->_left = cur;
} else {
parent->_right = cur;
}
cur->_parent = parent;
while (parent != nullptr && parent->_col == RED) {
Node *grandfather = parent->_parent;
if (grandfather->_left == parent) {
Node *uncle = grandfather->_right;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else {
if (cur == parent->_left) {
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else {
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
parent->_col = RED;
}
break;
}
}
else
{
Node *uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else {
if (cur == parent->_right) {
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else {
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
void inorder() {
_inorder(_root);
cout << endl;
}
bool isbalance() {
if (_root && _root->_col == RED) {
cout << "根节点颜色是红色" << endl;
return false;
}
return _check(_root, 0);
}
bool _check(Node *root, int blacknum) {
if (root == nullptr) {
cout << blacknum << " ";
return true;
}
if (root->_col == BLACK) {
blacknum++;
}
if (root->_col == RED
&& root->_parent
&& root->_parent->_col == RED) {
cout << "存在连续的红色节点" << endl;
return false;
}
return _check(root->_left, blacknum) && _check(root->_right, blacknum);
}
void _inorder(Node *root) {
if (root == nullptr) {
return;
}
_inorder(root->_left);
cout << root->_kv.first << " ";
_inorder(root->_right);
}
void RotateL(Node *parent) {
Node *subR = parent->_right;
Node *subRL = subR->_left;
parent->_right = subRL;
if (subRL != nullptr) {
subRL->_parent = parent;
}
Node *ppnode = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (ppnode == nullptr) {
_root = subR;
_root->_parent = nullptr;
} else {
if (ppnode->_left == parent) {
ppnode->_left = subR;
} else {
ppnode->_right = subR;
}
subR->_parent = ppnode;
}
}
void RotateR(Node *parent) {
Node *subL = parent->_left;
Node *subLR = subL->_right;
parent->_left = subLR;
if (subLR != nullptr) {
subLR->_parent = parent;
}
Node *ppnode = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (ppnode == nullptr) {
_root = subL;
_root->_parent = nullptr;
} else {
if (ppnode->_left == parent) {
ppnode->_left = subL;
} else {
ppnode->_right = subL;
}
subL->_parent = ppnode;
}
}
bool deleteNode (const int key) {
if (_root == nullptr) {
cout << "没有节点" << endl;
return false;
}
Node *cur = _root;
while (cur) {
if (cur->_kv.first > key) {
cur = cur->_left;
} else if (cur->_kv.first < key) {
cur = cur->_right;
} else {
break;
}
}
if (cur == nullptr) {
cout << "删除失败,未查找到" << endl;
return false;
}
while (cur) {
Node *parent = cur->_parent;
Node *subL = cur->_left;
Node *subR = cur->_right;
if ((subL && subL->_col == RED && subR == nullptr) || (subL && subL->_col == RED && subR && subR->_col == RED)) {
cur->_kv.first = subL->_kv.first;
cur->_left = nullptr;
delete (subL);
return true;
} else if (subR && subL == nullptr) {
cur->_kv.first = subR->_kv.first;
cur->_right = nullptr;
delete (subR);
return true;
} else if (subL && subR) {
Node *firstbehind = cur->_left;
while (firstbehind->_right) {
firstbehind = firstbehind->_right;
}
cur->_kv = firstbehind->_kv;
cur = firstbehind;
} else if (subL == nullptr && subR == nullptr) {
if (cur->_col == RED) {
if (parent->_left == cur) {
parent->_left = nullptr;
} else {
parent->_right = nullptr;
}
delete (cur);
return true;
} else {
if (parent == nullptr) {
delete (cur);
_root = nullptr;
return true;
}
Node *uncle = nullptr;
bool is_delete = false;
while (cur) {
parent = cur->_parent;
if (parent == nullptr) {
return true;
}
if (cur == parent->_right) {
if (is_delete == false) {
parent->_right = nullptr;
delete (cur);
is_delete = true;
}
uncle = parent->_left;
Node *uncleL = uncle->_left;
Node *uncleR = uncle->_right;
if (uncle->_col == BLACK) {
if (uncleL && uncleL->_col == RED) {
uncleL->_col = uncle->_col;
colour tempcolor = uncle->_col;
uncle->_col = parent->_col;
parent->_col = tempcolor;
RotateR(parent);
return true;
} else if (uncleL == nullptr && uncleR && uncleR->_col == RED) {
uncleR->_col = parent->_col;
parent->_col = BLACK;
RotateL(uncle);
RotateR(parent);
return true;
} else {
uncle->_col = RED;
if (parent->_col == RED) {
parent->_col = BLACK;
return true;
} else {
cur = parent;
}
}
} else {
parent->_col = RED;
uncle->_col = BLACK;
RotateR(parent);
return true;
}
} else {
if (is_delete == false) {
parent->_left = nullptr;
delete (cur);
is_delete = true;
}
uncle = parent->_right;
Node *uncleL = uncle->_left;
Node *uncleR = uncle->_right;
if (uncle && uncle->_col == BLACK) {
if (uncleR && uncleR->_col == RED) {
uncleR->_col = uncle->_col;
colour tempcolor = uncle->_col;
uncle->_col = parent->_col;
parent->_col = tempcolor;
RotateL(parent);
return true;
} else if (uncleR == nullptr && uncleL && uncleL->_col == RED) {
uncleL->_col = parent->_col;
parent->_col = uncle->_col;
RotateR(uncle);
RotateL(parent);
return true;
} else {
uncle->_col = RED;
if (parent->_col == RED) {
parent->_col = BLACK;
return true;
} else {
cur = parent;
}
}
} else {
parent->_col = RED;
uncle->_col = BLACK;
RotateL(parent);
return true;
}
}
}
}
}
}
return true;
}
private:
Node *_root = nullptr;
};
void test_RBTree1() {
int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
RBTree<int, int>v1;
for (auto e : a) {
v1.insert(make_pair(e, e));
}
v1.inorder();
cout << v1.isbalance() << endl;
while (1) {
int temp;
cin >> temp;
v1.deleteNode(temp);
v1.inorder();
}
}
int main() {
test_RBTree1();
return 0;
}