橘皮书算法(第四版)中译本有对该数据结构的详细讨论 csdn上也有很多相关文章
实现如下
#include <iostream>
#include <stack>
#include <utility>
#include <algorithm>
#include <list>
#include <random>
#include <vector>
using namespace std;
enum class Color { RED, BLACK };
enum class node_type { Node3, Node4 };
template <typename T>
struct LeftLeaningRBTreeNode
{
T data;
Color color;
LeftLeaningRBTreeNode* left = nullptr;
LeftLeaningRBTreeNode* right = nullptr;
LeftLeaningRBTreeNode(const T& data, Color color) :data(data), color(color) {}
};
template <typename T>
void flipColor(LeftLeaningRBTreeNode<T>* cur)
{
if (cur->color == Color::BLACK)
{
cur->color = Color::RED;
}
else
{
cur->color = Color::BLACK;
}
}
template <typename T>
LeftLeaningRBTreeNode<T>* RotateLR(LeftLeaningRBTreeNode<T>* ptr)
{
LeftLeaningRBTreeNode<T>* p = ptr->left;
LeftLeaningRBTreeNode<T>* q = p->right;
p->right = q->left;
q->left = p;
ptr->left = q->right;
q->right = ptr;
return q;
}
template <typename T>
LeftLeaningRBTreeNode<T>* RotateRL(LeftLeaningRBTreeNode<T>* ptr)
{
LeftLeaningRBTreeNode<T>* p = ptr->right;
LeftLeaningRBTreeNode<T>* q = p->left;
p->left = q->right;
q->right = p;
ptr->right = q->left;
q->left = ptr;
return q;
}
template <typename T>
LeftLeaningRBTreeNode<T>* RotateR(LeftLeaningRBTreeNode<T>* ptr)
{
LeftLeaningRBTreeNode<T>* p = ptr->left;
ptr->left = p->right;
p->right = ptr;
return p;
}
template <typename T>
LeftLeaningRBTreeNode<T>* RotateL(LeftLeaningRBTreeNode<T>* ptr)
{
LeftLeaningRBTreeNode<T>* p = ptr->right;
ptr->right = p->left;
p->left = ptr;
return p;
}
template <typename T>
void R1R2CombineFor4Node(LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* R1, LeftLeaningRBTreeNode<T>* R2)
{
flipColor(mid);
flipColor(right);
flipColor(left);
flipColor(R1);
flipColor(R2);
RotateL(mid);
}
template <typename T>
void R2BorrowFromR1For4Node(LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R1, LeftLeaningRBTreeNode<T>*R1_left_child)
{
flipColor(R1);
flipColor(R1_left_child);
mid->left = RotateR(left);
R1->right = RotateL(left);
}
template <typename T>
void R1BorrowFromR2For4Node(LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T> *R1)
{
flipColor(left);
flipColor(R1);
mid->left = RotateRL(left);
}
template <typename T>
void R3R4CombineFor4Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>*R3, LeftLeaningRBTreeNode<T>* R4)
{
flipColor(right);
flipColor(R3);
flipColor(R4);
}
template <typename T>
void R3BorrowFromR4For4Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R3)
{
flipColor(right);
flipColor(R3);
mid->right = RotateRL(right);
}
template <typename T>
void R4BorrowFromR3For4Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R3, LeftLeaningRBTreeNode<T>* R3_left_child)
{
flipColor(R3);
flipColor(R3_left_child);
mid->right = RotateR(right);
R3->right = RotateL(right);
}
template <typename T>
void R2R3CombineFor4Node(LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* R2, LeftLeaningRBTreeNode<T>* R3)
{
flipColor(right);
flipColor(R2);
flipColor(R3);
RotateL(mid);
right->left = RotateR(mid);
}
template <typename T>
void R2BorrowFromR3For4Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R3, LeftLeaningRBTreeNode<T>* R3_left_child, LeftLeaningRBTreeNode<T>*left)
{
flipColor(left->right);
flipColor(R3_left_child);
right->left = RotateR(R3);
mid->right = RotateR(right);
RotateL(mid);
R3_left_child->left = RotateR(mid);
}
template <typename T>
void R3BorrowFromR2For4Node(LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R2, LeftLeaningRBTreeNode<T>* right)
{
flipColor(mid);
flipColor(R2->left);
RotateLR(mid);
R2->right = RotateL(mid);
right->left = RotateL(mid);
}
template <typename T>
void R1R2CombineFor3Node(LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* R1, LeftLeaningRBTreeNode<T>* R2)
{
flipColor(left);
flipColor(R1);
flipColor(R2);
}
template <typename T>
void R2R3CombineFor3Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* R3, LeftLeaningRBTreeNode<T>* R2)
{
flipColor(left);
flipColor(R2);
flipColor(R3);
RotateR(right);
}
template <typename T>
void R2BorrowFromR1For3Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* R1, LeftLeaningRBTreeNode<T>*R1_left_child)
{
flipColor(R1);
flipColor(R1_left_child);
right->left = RotateR(left);
R1->right = RotateL(left);
}
template <typename T>
void R1BorrowFromR2For3Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* left, LeftLeaningRBTreeNode<T>* R1)
{
flipColor(left);
flipColor(R1);
right->left = RotateRL(left);
}
template <typename T>
void R3BorrowFromR2For3Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* R2_left_child, LeftLeaningRBTreeNode<T>* R2)
{
flipColor(right);
flipColor(R2_left_child);
RotateLR(right);
R2->right = RotateL(right);
}
template <typename T>
void R2BorrowFromR3For3Node(LeftLeaningRBTreeNode<T>* right, LeftLeaningRBTreeNode<T>* R2, LeftLeaningRBTreeNode<T>* R3_left_child)
{
flipColor(R2);
flipColor(R3_left_child);
RotateRL(right);
R3_left_child->left = RotateR(right);
}
template <typename T>
void R2BorrowFromR1For2Node(LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R1, LeftLeaningRBTreeNode<T>* R1_left_child)
{
flipColor(mid);
flipColor(R1_left_child);
RotateR(mid);
R1->right = RotateL(mid);
}
template <typename T>
void R1BorrowFromR2For2Node(LeftLeaningRBTreeNode<T>* mid, LeftLeaningRBTreeNode<T>* R1, LeftLeaningRBTreeNode<T>* R2_left_child)
{
flipColor(R1);
flipColor(R2_left_child);
RotateRL(mid);
}
enum class Down_Node_Index { R1, R2, R3, R4 };
template <typename T>
struct StackNode
{
LeftLeaningRBTreeNode<T>* cur;
LeftLeaningRBTreeNode<T>** upper;
Down_Node_Index link_point;
StackNode(LeftLeaningRBTreeNode<T>* c, LeftLeaningRBTreeNode<T>** u, Down_Node_Index l) :cur(c), upper(u), link_point(l) {}
};
template <typename T>
class LeftLeaningRBTree
{
public:
friend int main();
bool deleteMax();
bool deleteMin();
bool deleteAnyKey(const T &key);
bool insert(const T& key);
private:
LeftLeaningRBTreeNode<T>* root = nullptr;
void linkWithUpper(LeftLeaningRBTreeNode<T>* cur, LeftLeaningRBTreeNode<T>** upper);
void linkWithUpperForDelete(LeftLeaningRBTreeNode<T>* cur, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack);
void linkWithUpperForFixUp(LeftLeaningRBTreeNode<T>* cur, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack);
void eliminate4Node(stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool,bool>>>& work_stack);
bool remove(const T& key, node_type &flag, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, LeftLeaningRBTreeNode<T>* &parent, LeftLeaningRBTreeNode<T>**& link_point);
LeftLeaningRBTreeNode<T>* deleteMinMaintain(LeftLeaningRBTreeNode<T>*& parent, LeftLeaningRBTreeNode<T>*& q, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, node_type& type_flag, bool link_or_set_sub_tree_root);
LeftLeaningRBTreeNode<T>* deleteMaxMaintain(LeftLeaningRBTreeNode<T>*& parent, LeftLeaningRBTreeNode<T>*& q, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, node_type& type_flag, bool link_or_set_sub_tree_root);
void link_with_upper_for_insert(LeftLeaningRBTreeNode<T>* Node2Father, LeftLeaningRBTreeNode<T>* Node2LowestLevel, LeftLeaningRBTreeNode<T>* cur);
void decideLinkOrSet(bool first_cycle, bool link_or_set_sub_tree_root, LeftLeaningRBTreeNode<T>*& root_after_process, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, LeftLeaningRBTreeNode<T>* r);
void decideLinkOrSetForLeaf(LeftLeaningRBTreeNode<T>* _q, bool link_or_set_sub_tree_root, LeftLeaningRBTreeNode<T>*& root_after_process, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, LeftLeaningRBTreeNode<T>* r);
};
template <typename T>
bool is3Node(LeftLeaningRBTreeNode<T>* cur)
{
return cur->left != nullptr && cur->left->color == Color::RED;
}
template <typename T>
bool is4Node(LeftLeaningRBTreeNode<T>* cur)
{
return cur->left != nullptr && cur->left->color == Color::RED && cur->right != nullptr && cur->right->color == Color::RED;
}
template <typename T>
void LeftLeaningRBTree<T>::linkWithUpperForFixUp(LeftLeaningRBTreeNode<T>* cur, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack)
{
if (work_stack.empty())
{
root = cur;
}
else
{
if (work_stack.top().second.first)
{
work_stack.top().first->left = cur;
}
else
{
work_stack.top().first->right = cur;
}
}
}
template <typename T>
void LeftLeaningRBTree<T>::linkWithUpper(LeftLeaningRBTreeNode<T>* cur, LeftLeaningRBTreeNode<T>** upper)
{
if (upper == nullptr)
{
root = cur;
}
else
{
*upper = cur;
}
}
template <typename T>
void LeftLeaningRBTree<T>::linkWithUpperForDelete(LeftLeaningRBTreeNode<T>* cur, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack)
{
if (work_stack.empty())
{
root = cur;
}
else
{
if (work_stack.top().second.first)
work_stack.top().first->left = cur;
else
work_stack.top().first->right = cur;
}
}
template <typename T>
void LeftLeaningRBTree<T>::link_with_upper_for_insert(LeftLeaningRBTreeNode<T>* Node2Father, LeftLeaningRBTreeNode<T>* Node2LowestLevel, LeftLeaningRBTreeNode<T>* cur)
{
if (Node2Father == nullptr)
{
root = cur;
}
else
{
if (Node2Father->left == Node2LowestLevel)
{
Node2Father->left = cur;
}
else
{
Node2Father->right = cur;
}
}
}
template <typename T>
bool LeftLeaningRBTree<T>::insert(const T& key)
{
if (root == nullptr)
{
root = new LeftLeaningRBTreeNode<T>(key, Color::BLACK);
return true;
}
LeftLeaningRBTreeNode<T>* run = root;
LeftLeaningRBTreeNode<T>* Node2LowestLevel = nullptr;
LeftLeaningRBTreeNode<T>* Node2Father = nullptr;
list<StackNode<T>> work_stack;
do
{
if (run->data == key)
return false;
bool less = key < run->data;
if (is3Node(run) == false)
{
if (work_stack.empty() == false)
{
if (work_stack.back().link_point == Down_Node_Index::R3)
Node2Father = work_stack.back().cur;
else
Node2Father = work_stack.back().cur->left;
}
else
{
Node2Father = Node2LowestLevel;
}
Node2LowestLevel = run;
work_stack.clear();
if (less)
run = run->left;
else
run = run->right;
}
else
{
if (less)
{
if (run->left->data == key)
return false;
if (key < run->left->data)
{
work_stack.push_back(StackNode<T>(run, &run->left->left, Down_Node_Index::R1));
run = run->left->left;
}
else
{
work_stack.push_back(StackNode<T>(run, &run->left->right, Down_Node_Index::R2));
run = run->left->right;
}
}
else
{
work_stack.push_back(StackNode<T>(run, &run->right, Down_Node_Index::R3));
run = run->right;
}
}
} while (run!= nullptr);
if (Node2LowestLevel != nullptr && work_stack.empty())
{
if (key < Node2LowestLevel->data)
{
Node2LowestLevel->left = new LeftLeaningRBTreeNode<T>(key, Color::RED);
}
else
{
LeftLeaningRBTreeNode<T> * temp = Node2LowestLevel->right = new LeftLeaningRBTreeNode<T>(key, Color::RED);
flipColor(Node2LowestLevel);
flipColor(temp);
RotateL(Node2LowestLevel);
link_with_upper_for_insert(Node2Father, Node2LowestLevel, temp);
}
}
else
{
StackNode<T> t = work_stack.back();
work_stack.pop_back();
*(t.upper) = new LeftLeaningRBTreeNode<T>(key, Color::RED);
LeftLeaningRBTreeNode<T>* p = nullptr;
LeftLeaningRBTreeNode<T>* original = nullptr;
original = t.cur;
if (t.link_point == Down_Node_Index::R1)
{
flipColor(original);
flipColor(original->left);
p = RotateR(original);
}
else if (t.link_point == Down_Node_Index::R2)
{
flipColor(original);
p = RotateLR(original);
flipColor(p);
}
else
{
p = original;
}
//注意 和上层链接是否有必要
while (work_stack.empty() == false)
{
LeftLeaningRBTreeNode<T>* q = work_stack.back().cur;
Down_Node_Index d = work_stack.back().link_point;
*(work_stack.back().upper) = p;
work_stack.pop_back();
original = q;
flipColor(p->left);
flipColor(p->right);
flipColor(p);
if (d == Down_Node_Index::R1 || d == Down_Node_Index::R2)
{
if (d == Down_Node_Index::R1)
{
p = RotateR(q);
}
else
{
RotateLR(q);
}
flipColor(p);
flipColor(q);
}
else
{
p = q;
}
}
flipColor(p->left);
flipColor(p->right);
if (Node2LowestLevel != nullptr)
{
flipColor(p);
if (original == Node2LowestLevel->right)
{
Node2LowestLevel->right = p;
flipColor(p);
flipColor(Node2LowestLevel);
RotateL(Node2LowestLevel);
link_with_upper_for_insert(Node2Father, Node2LowestLevel, p);
}
else
{
Node2LowestLevel->left = p;
}
}
else
{
root = p;
}
}
return true;
}
template <typename T>
void findMinValueAndSwap(LeftLeaningRBTreeNode<T>* cur, const T &key)
{
LeftLeaningRBTreeNode<T>* run = cur->right;
while (run->left != nullptr)
{
run = run->left;
}
cur->data = run->data;
run->data = key;
}
template <typename T>
void findMaxValueAndSwap(LeftLeaningRBTreeNode<T>* cur, const T& key)
{
LeftLeaningRBTreeNode<T>* run = cur->left;
while (run->right != nullptr)
{
run = run->right;
}
cur->data = run->data;
run->data = key;
}
template <typename T>
bool LeftLeaningRBTree<T>::deleteAnyKey(const T& key)
{
if (root == nullptr)
return false;
node_type flag;
stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>> work_stack;
LeftLeaningRBTreeNode<T>* parent = nullptr;
LeftLeaningRBTreeNode<T>** link_point = nullptr;
if (is3Node(root) == false)
{
if (root->left == nullptr)
{
if (root->data == key)
{
delete root;
root = nullptr;
return true;
}
return false;
}
bool l = is3Node(root->left);
bool r = is3Node(root->right);
bool less = key < root->data;
bool equal = key == root->data;
bool lessEqual = less || equal;
if (r && !lessEqual)
{
flag = node_type::Node3;
parent = root->right;
work_stack.push(make_pair(root, make_pair(false, false)));
link_point = &root->right;
}
else if (l && less)
{
flag = node_type::Node3;
parent = root->left;
work_stack.push(make_pair(root, make_pair(true, false)));
link_point = &root->left;
}
if (l == false)
{
if (r == false)
{
flipColor(root->left);
flipColor(root->right);
flag = node_type::Node4;
parent = root;
}
else
{
if (lessEqual)
{
parent = root;
root = root->right->left;
flag = node_type::Node3;
work_stack.push(make_pair(root, make_pair(true, false)));
link_point = &root->left;
R1BorrowFromR2For2Node(parent, parent->left, root);
}
}
}
else
{
if (r == false)
{
if (!less)
{
parent = root->left;
R2BorrowFromR1For2Node(root, parent, parent->left);
root = parent;
parent = parent->right;
work_stack.push(make_pair(root, make_pair(false, false)));
flag = node_type::Node3;
link_point = &root->right;
}
}
else
{
if (equal)
{
parent = root->left;
LeftLeaningRBTreeNode<T>* temp = root;
root = RotateRL(root);
root->left = RotateR(temp);
flipColor(temp);
flipColor(root);
flag = node_type::Node4;
work_stack.push(make_pair(root, make_pair(true, false)));
link_point = &root->left;
}
}
}
}
else
{
flag = node_type::Node3;
parent = root;
}
return remove(key, flag, work_stack, parent, link_point);
}
template <typename T>
bool LeftLeaningRBTree<T>::remove(const T& key, node_type& flag, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, LeftLeaningRBTreeNode<T>* &parent, LeftLeaningRBTreeNode<T>** &link_point)
{
bool remove_sucess;
while (true)
{
Down_Node_Index down_node_index;
LeftLeaningRBTreeNode<T>* q = nullptr;
LeftLeaningRBTreeNode<T>* second_level = nullptr;
if (flag == node_type::Node4)
{
if (key < parent->data)
{
second_level = parent->left;
if (key < second_level->data)
{
q = second_level->left;
down_node_index = Down_Node_Index::R1;
}
else if (key > second_level->data)
{
q = second_level->right;
down_node_index = Down_Node_Index::R2;
}
else
{
if (second_level->left != nullptr)
{
bool l;
if ((l = is3Node(second_level->left)) || is3Node(second_level->right))
{
work_stack.push(make_pair(parent, make_pair(true, true)));
flag = node_type::Node3;
if (l)
{
work_stack.push(make_pair(second_level, make_pair(true, false)));
parent = second_level->left;
q = parent->right;
findMaxValueAndSwap(second_level, key);
second_level->left = deleteMaxMaintain(parent, q, work_stack, flag, false);
}
else
{
parent = second_level->right;
}
}
else
{
LeftLeaningRBTreeNode<T>* c = parent->right;
if (is3Node(c->left) == false)
{
work_stack.push(make_pair(c, make_pair(true, false)));
flag = node_type::Node4;
linkWithUpper(c, link_point);
R2R3CombineFor4Node(parent, c, second_level->right, c->left);
}
else
{
work_stack.push(make_pair(c->left->left, make_pair(true, true)));
flag = node_type::Node3;
linkWithUpper(c->left->left, link_point);
R2BorrowFromR3For4Node(c, parent, c->left, c->left->left, second_level);
}
}
if (l == false)
{
work_stack.push(make_pair(second_level, make_pair(false, false)));
q = parent->left->left;
findMinValueAndSwap(second_level, key);
second_level->right = deleteMinMaintain(parent, q, work_stack, flag, false);
}
}
else
{
delete parent->left;
parent->left = nullptr;
flipColor(parent->right);
flipColor(parent);
parent = RotateL(parent);
linkWithUpper(parent, link_point);
}
remove_sucess = true;
break;
}
}
else if (key > parent->data)
{
second_level = parent->right;
if (key < second_level->data)
{
q = second_level->left;
down_node_index = Down_Node_Index::R3;
}
else if (key > second_level->data)
{
q = second_level->right;
down_node_index = Down_Node_Index::R4;
}
else
{
if (second_level->left != nullptr)
{
LeftLeaningRBTreeNode<T>* b = parent->left;
if (is3Node(second_level->left))
{
work_stack.push(make_pair(parent, make_pair(false, true)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
flag = node_type::Node3;
parent = second_level->left;
q = parent->right;
findMaxValueAndSwap(second_level, key);
second_level->left = deleteMaxMaintain(parent, q, work_stack, flag, false);
}
else if (is3Node(second_level->right))
{
work_stack.push(make_pair(parent, make_pair(false, true)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
flag = node_type::Node3;
parent = second_level->right;
q = parent->left->left;
findMinValueAndSwap(second_level, key);
second_level->right = deleteMinMaintain(parent, q, work_stack, flag, false);
}
else
{
if (is3Node(b->right) == false)
{
work_stack.push(make_pair(second_level, make_pair(true, false)));
work_stack.push(make_pair(b, make_pair(false, false)));
flag = node_type::Node4;
linkWithUpper(second_level, link_point);
findMaxValueAndSwap(second_level, key);
R2R3CombineFor4Node(parent, second_level, b->right, second_level->left);
q = parent->right->right;
deleteMaxMaintain(parent, q, work_stack, flag, true);
}
else
{
work_stack.push(make_pair(b->right, make_pair(false, true)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
flag = node_type::Node3;
linkWithUpper(b->right, link_point);
findMaxValueAndSwap(second_level, key);
R3BorrowFromR2For4Node(parent, b->right, second_level);
parent = second_level->left;
q = parent->right;
second_level->left = deleteMaxMaintain(parent, q, work_stack, flag, false);
}
}
}
else
{
delete parent->right;
parent->right = nullptr;
}
remove_sucess = true;
break;
}
}
else
{
second_level = parent->right;
if (second_level->left != nullptr)
{
LeftLeaningRBTreeNode<T>* b = parent->left;
if (is3Node(b->right))
{
work_stack.push(make_pair(parent, make_pair(true, true)));
work_stack.push(make_pair(b, make_pair(false, false)));
flag = node_type::Node3;
findMaxValueAndSwap(parent, key);
parent = b->right;
q = parent->right;
deleteMaxMaintain(parent, q, work_stack, flag, true);
}
else if (is3Node(second_level->left))
{
work_stack.push(make_pair(parent, make_pair(false, true)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
flag = node_type::Node3;
findMinValueAndSwap(parent, key);
parent = second_level->left;
q = parent->left->left;
deleteMinMaintain(parent, q, work_stack, flag, true);
}
else
{
if (is3Node(second_level->right) == false)
{
work_stack.push(make_pair(parent, make_pair(false, false)));
flag = node_type::Node4;
R3R4CombineFor4Node(second_level, second_level->left, second_level->right);
findMinValueAndSwap(parent, key);
b = parent;
parent = second_level;
q = parent->left->left;
b->right = deleteMinMaintain(parent, q, work_stack, flag, false);
}
else
{
if (is3Node(b->left) == false)
{
work_stack.push(make_pair(second_level, make_pair(true, false)));
work_stack.push(make_pair(parent, make_pair(true, false)));
flag = node_type::Node4;
linkWithUpper(second_level, link_point);
R1R2CombineFor4Node(parent, b, second_level, b->left, b->right);
findMaxValueAndSwap(parent, key);
second_level = parent;
parent = b;
q = parent->right->right;
second_level->left = deleteMaxMaintain(parent, q, work_stack, flag, false);
}
else
{
second_level = b->left;
work_stack.push(make_pair(parent, make_pair(true, true)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
flag = node_type::Node3;
R2BorrowFromR1For4Node(b, parent, second_level, second_level->left);
findMaxValueAndSwap(parent, key);
parent = second_level->right;
q = parent->right;
deleteMaxMaintain(parent, q, work_stack, flag, true);
}
}
}
}
else
{
second_level = parent->left;
q = parent->right;
delete parent;
flipColor(q);
q->left = second_level;
linkWithUpper(q, link_point);
}
remove_sucess = true;
break;
}
if (q == nullptr) // 更新flag work_stack parent link_point //注意second_level和q
{
work_stack.push(make_pair(parent, make_pair(second_level == parent->left ? true : false, true)));
remove_sucess = false;
break;
}
else if(is3Node(q) == false)
{
if (down_node_index == Down_Node_Index::R2 || down_node_index == Down_Node_Index::R1)
{
if (down_node_index == Down_Node_Index::R2 ? is3Node(second_level->left) == false : is3Node(second_level->right) == false)
{
LeftLeaningRBTreeNode<T>* t = parent->right;
if (down_node_index == Down_Node_Index::R2)
R1R2CombineFor4Node(parent, second_level, t, second_level->left, q);
else
R1R2CombineFor4Node(parent, second_level, parent->right, q, second_level->right);
linkWithUpper(t, link_point);
work_stack.push(make_pair(t, make_pair(true, false)));
work_stack.push(make_pair(parent, make_pair(true, false)));
link_point = &parent->left;
parent = second_level;
flag = node_type::Node4;
}
else
{
work_stack.push(make_pair(parent, make_pair(true, true)));
if (down_node_index == Down_Node_Index::R2)
{
work_stack.push(make_pair(second_level->left, make_pair(false, false)));
R2BorrowFromR1For4Node(second_level, parent, second_level->left, second_level->left->left);
link_point = &parent->left->right;
parent = q;
}
else
{
work_stack.push(make_pair(second_level->right->left, make_pair(true, false)));
R1BorrowFromR2For4Node(second_level, parent, q);
link_point = &parent->left->left;
parent = second_level;
}
flag = node_type::Node3;
}
}
else
{
if (down_node_index == Down_Node_Index::R3 ? is3Node(second_level->right) == false : is3Node(second_level->left) == false)
{
if (down_node_index == Down_Node_Index::R3)
R3R4CombineFor4Node(second_level, q, second_level->right);
else
R3R4CombineFor4Node(second_level, second_level->left, q);
work_stack.push(make_pair(parent, make_pair(false, false)));
link_point = &parent->right;
parent = second_level;
flag = node_type::Node4;
}
else
{
work_stack.push(make_pair(parent, make_pair(false, true)));
if (down_node_index == Down_Node_Index::R3)
{
work_stack.push(make_pair(second_level->right->left, make_pair(true, false)));
R3BorrowFromR4For4Node(second_level, parent, q);
link_point = &parent->right->left;
parent = second_level;
}
else
{
work_stack.push(make_pair(second_level->left, make_pair(false, false)));
R4BorrowFromR3For4Node(second_level, parent, second_level->left, second_level->left->left);
link_point = &parent->right->right;
parent = q;
}
flag = node_type::Node3;
}
}
continue;
}
else
{
if (down_node_index == Down_Node_Index::R1)
{
work_stack.push(make_pair(parent, make_pair(true, true)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
link_point = &second_level->left;
}
else if (down_node_index == Down_Node_Index::R2)
{
work_stack.push(make_pair(parent, make_pair(true, true)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
link_point = &second_level->right;
}
else if (down_node_index == Down_Node_Index::R3)
{
work_stack.push(make_pair(parent, make_pair(false, true)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
link_point = &second_level->left;
}
else
{
work_stack.push(make_pair(parent, make_pair(false, true)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
link_point = &second_level->right;
}
}
}
else
{
if (key == parent->data)
{
if (parent->right != nullptr)
{
second_level = parent->left;
if (is3Node(second_level->right))
{
work_stack.push(make_pair(parent, make_pair(true, false)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
flag = node_type::Node3;
findMaxValueAndSwap(parent, key);
parent = second_level->right;
q = parent->right;
deleteMaxMaintain(parent, q, work_stack, flag, true);
}
else if (is3Node(parent->right))
{
work_stack.push(make_pair(parent, make_pair(false, false)));
flag = node_type::Node3;
findMinValueAndSwap(parent, key);
second_level = parent;
parent = parent->right;
q = parent->left->left;
second_level->right = deleteMinMaintain(parent, q, work_stack, flag, false);
}
else
{
if (is3Node(second_level->left) == false)
{
work_stack.push(make_pair(parent, make_pair(true, false)));
flag = node_type::Node4;
findMaxValueAndSwap(parent, key);
R1R2CombineFor3Node(second_level, second_level->left, second_level->right);
LeftLeaningRBTreeNode<T>* temp = parent;
parent = second_level;
q = parent->right->right;
temp->left = deleteMaxMaintain(parent, q, work_stack, flag, false);
}
else
{
work_stack.push(make_pair(parent, make_pair(true, false)));
q = second_level->left;
work_stack.push(make_pair(q, make_pair(false, false)));
flag = node_type::Node3;
findMaxValueAndSwap(parent, key);
R2BorrowFromR1For3Node(parent, second_level, q, q->left);
parent = q->right;
q = parent->right;
deleteMaxMaintain(parent, q, work_stack, flag, true);
}
}
}
else
{
second_level = parent->left;
delete parent;
flipColor(second_level);
linkWithUpper(second_level, link_point);
}
remove_sucess = true;
break;
}
second_level = parent->left;
if (key < parent->data)
{
if (key < second_level->data)
{
q = second_level->left;
down_node_index = Down_Node_Index::R1;
}
else if (key > second_level->data)
{
q = second_level->right;
down_node_index = Down_Node_Index::R2;
}
else
{
if (second_level->left != nullptr)
{
if (is3Node(second_level->left))
{
work_stack.push(make_pair(parent, make_pair(true, false)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
flag = node_type::Node3;
findMaxValueAndSwap(second_level, key);
parent = second_level->left;
q = parent->right;
second_level->left = deleteMaxMaintain(parent, q, work_stack, flag, false);
}
else
{
findMinValueAndSwap(second_level, key);
if (is3Node(second_level->right))
{
work_stack.push(make_pair(parent, make_pair(true, false)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
flag = node_type::Node3;
parent = second_level->right;
}
else
{
if (is3Node(parent->right) == false)
{
work_stack.push(make_pair(second_level, make_pair(false, false)));
flag = node_type::Node4;
linkWithUpper(second_level, link_point); //当前
R2R3CombineFor3Node(parent, second_level, parent->right, second_level->right);
}
else
{
LeftLeaningRBTreeNode<T>* temp = parent->right->left;
work_stack.push(make_pair(temp, make_pair(true, false)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
flag = node_type::Node3;
linkWithUpper(temp, link_point);
R2BorrowFromR3For3Node(parent, second_level->right, temp);
}
}
q = parent->left->left;
second_level->right = deleteMinMaintain(parent, q, work_stack, flag, false);
}
}
else
{
delete second_level;
parent->left = nullptr;
}
remove_sucess = true;
break;
}
}
else
{
q = parent->right;
down_node_index = Down_Node_Index::R3;
}
if (q == nullptr)
{
remove_sucess = false;
break;
}
else if (is3Node(q) == false)
{
if (down_node_index == Down_Node_Index::R2 ? is3Node(second_level->left) == false : (down_node_index == Down_Node_Index::R1 ? is3Node(second_level->right) == false : is3Node(second_level->right) == true))
{
if (down_node_index == Down_Node_Index::R2 || down_node_index == Down_Node_Index::R1)
{
if (down_node_index == Down_Node_Index::R2)
R1R2CombineFor3Node(second_level, second_level->left, q); //r1
else
R1R2CombineFor3Node(second_level, q, second_level->right); //r4
work_stack.push(make_pair(parent, make_pair(true, false)));
link_point = &parent->left;
parent = second_level;
flag = node_type::Node4;
}
else
{
linkWithUpper(second_level->right, link_point);
work_stack.push(make_pair(second_level->right, make_pair(false, false))); //r7
link_point = &second_level->right->right;
R3BorrowFromR2For3Node(parent, second_level->right->left, second_level->right);
parent = q;
flag = node_type::Node3;
}
}
else
{
if (down_node_index == Down_Node_Index::R2 ? is3Node(parent->right) == false : (down_node_index == Down_Node_Index::R3 ? true : false))
{
if (down_node_index == Down_Node_Index::R2)
{
R2R3CombineFor3Node(parent, second_level, parent->right, q); //*r2
}
else
{
R2R3CombineFor3Node(parent, second_level, q, second_level->right); //*r6
}
linkWithUpper(second_level, link_point);
work_stack.push(make_pair(second_level, make_pair(false, false)));
link_point = &second_level->right;
flag = node_type::Node4;
}
else //3 5
{
work_stack.push(make_pair(parent, make_pair(true, false)));
if (down_node_index == Down_Node_Index::R2)
{
R2BorrowFromR1For3Node(parent, second_level, second_level->left, second_level->left->left);
work_stack.push(make_pair(parent->left, make_pair(false, false))); //r3
link_point = &parent->left->right;
parent = q;
}
else
{
R1BorrowFromR2For3Node(parent, second_level, q);
work_stack.push(make_pair(parent->left, make_pair(true, false))); //r5
link_point = &parent->left->left;
parent = second_level;
}
flag = node_type::Node3;
}
}
continue;
}
else
{
if (down_node_index == Down_Node_Index::R1)
{
work_stack.push(make_pair(parent, make_pair(true, false)));
work_stack.push(make_pair(second_level, make_pair(true, false)));
link_point = &second_level->left;
}
else if (down_node_index == Down_Node_Index::R2)
{
work_stack.push(make_pair(parent, make_pair(true, false)));
work_stack.push(make_pair(second_level, make_pair(false, false)));
link_point = &second_level->right;
}
else
{
work_stack.push(make_pair(parent, make_pair(false, false)));
link_point = &parent->right;
}
}
}
flag = node_type::Node3;
parent = q;
}
eliminate4Node(work_stack);
return remove_sucess;
}
template <typename T>
void LeftLeaningRBTree<T>::eliminate4Node(stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack)
{
while (true)
{
while (work_stack.empty() == false)
{
if (work_stack.top().second.second)
break;
work_stack.pop();
}
if (work_stack.empty())
break;
LeftLeaningRBTreeNode<T>* q = work_stack.top().first;
work_stack.pop();
while (true)
{
flipColor(q->left);
flipColor(q->right);
if (work_stack.empty())
break;
flipColor(q);
LeftLeaningRBTreeNode<T>* parent = work_stack.top().first;
bool link_d = work_stack.top().second.first;
work_stack.pop();
if (parent->color == Color::BLACK)
{
if (link_d == false)
{
if (parent->left->color == Color::RED)
{
q = parent;
}
else
{
flipColor(parent);
flipColor(q);
RotateL(parent);
linkWithUpperForFixUp(q, work_stack);
break;
}
}
else
{
break;
}
}
else
{
LeftLeaningRBTreeNode<T>* pp = work_stack.top().first;
bool is4_Node = work_stack.top().second.second;
work_stack.pop();
if (is4_Node)
{
q = pp;
}
else
{
if (link_d == false)
{
flipColor(pp);
flipColor(q);
RotateLR(pp);
linkWithUpperForFixUp(q, work_stack);
}
else
{
flipColor(pp);
flipColor(parent);
RotateR(pp);
linkWithUpperForFixUp(parent, work_stack);
q = parent;
}
}
}
}
}
}
template <typename T>
void LeftLeaningRBTree<T>::decideLinkOrSet(bool first_cycle, bool link_or_set_sub_tree_root, LeftLeaningRBTreeNode<T>* &root_after_process, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, LeftLeaningRBTreeNode<T>* r)
{
if (first_cycle && link_or_set_sub_tree_root == false)
{
root_after_process = r;
}
else
{
linkWithUpperForDelete(r, work_stack);
}
}
template <typename T>
void LeftLeaningRBTree<T>::decideLinkOrSetForLeaf(LeftLeaningRBTreeNode<T>* _q, bool link_or_set_sub_tree_root, LeftLeaningRBTreeNode<T>*& root_after_process, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, LeftLeaningRBTreeNode<T>* r)
{
if (_q == nullptr && link_or_set_sub_tree_root == false)
{
root_after_process = r;
}
else
{
linkWithUpperForDelete(r, work_stack);
}
}
template <typename T>
LeftLeaningRBTreeNode<T>* LeftLeaningRBTree<T>::deleteMinMaintain(LeftLeaningRBTreeNode<T>* &parent, LeftLeaningRBTreeNode<T>* &q, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>&work_stack, node_type &type_flag, bool link_or_set_sub_tree_root)
{
LeftLeaningRBTreeNode<T>* root_after_process = parent;
LeftLeaningRBTreeNode<T>* _q = q;
bool first_cycle = true;
while (q != nullptr)
{
if (is3Node(q) == false)
{
LeftLeaningRBTreeNode<T>* m = parent->left;
LeftLeaningRBTreeNode<T>* p = m->right;
if (type_flag == node_type::Node3)
{
work_stack.push(make_pair(parent, make_pair(true, false)));
if (is3Node(p) == false)
{
R1R2CombineFor3Node(m, q, p);
type_flag = node_type::Node4;
}
else
{
R1BorrowFromR2For3Node(parent, m, q);
work_stack.push(make_pair(parent->left, make_pair(true, false)));
}
}
else
{
if (is3Node(p) == false)
{
LeftLeaningRBTreeNode<T>* right = parent->right;
decideLinkOrSet(first_cycle, link_or_set_sub_tree_root, root_after_process, work_stack, right);
work_stack.push(make_pair(right, make_pair(true, false)));
work_stack.push(make_pair(parent, make_pair(true, false)));
R1R2CombineFor4Node(parent, m, right, q, p);
}
else
{
work_stack.push(make_pair(parent, make_pair(true, true)));
work_stack.push(make_pair(p->left, make_pair(true, false)));
R1BorrowFromR2For4Node(m, parent, q);
type_flag = node_type::Node3;
}
}
parent = m;
q = q->left;
}
else
{
if (type_flag == node_type::Node3)
work_stack.push(make_pair(parent, make_pair(true, false)));
else
{
type_flag = node_type::Node3;
work_stack.push(make_pair(parent, make_pair(true, true)));
}
work_stack.push(make_pair(parent->left, make_pair(true, false)));
parent = q;
q = q->left->left;
}
first_cycle = false;
}
delete parent->left;
parent->left = nullptr;
if (type_flag == node_type::Node4)
{
flipColor(parent->right);
flipColor(parent);
parent = RotateL(parent);
decideLinkOrSetForLeaf(_q, link_or_set_sub_tree_root, root_after_process, work_stack, parent);
}
return root_after_process;
}
template <typename T>
bool LeftLeaningRBTree<T>::deleteMin()
{
if (root == nullptr)
return false;
LeftLeaningRBTreeNode<T>* parent = nullptr;
LeftLeaningRBTreeNode<T>* q = nullptr;
stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>> work_stack;
node_type type_flag;
bool l;
if ((l = is3Node(root)) == false)
{
if (root->left == nullptr)
{
delete root;
root = nullptr;
return true;
}
}
bool r;
if (l == false && ((r = is3Node(root->left)) || is3Node(root->right)))
{
if (r)
{
parent = root->left;
q = parent->left->left;
}
else
{
q = root->left;
parent = root->right->left;
R1BorrowFromR2For2Node(root, q, parent);
swap(parent, root);
q = q->left;
}
work_stack.push(make_pair(root, make_pair(true, false)));
type_flag = node_type::Node3;
}
else
{
if (l)
{
type_flag = node_type::Node3;
}
else
{
flipColor(root->left);
flipColor(root->right);
type_flag = node_type::Node4;
}
parent = root;
q = root->left->left;
}
deleteMinMaintain(parent, q, work_stack, type_flag, true);
eliminate4Node(work_stack);
return true;
}
template <typename T>
LeftLeaningRBTreeNode<T>* LeftLeaningRBTree<T>::deleteMaxMaintain(LeftLeaningRBTreeNode<T>*& parent, LeftLeaningRBTreeNode<T>*& q, stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>>& work_stack, node_type& type_flag, bool link_or_set_sub_tree_root)
{
LeftLeaningRBTreeNode<T>* root_after_process = parent;
LeftLeaningRBTreeNode<T>* _q = q;
bool first_cycle = true;
while (q != nullptr) //q parent stack flag
{
if (is3Node(q) == false)
{
LeftLeaningRBTreeNode<T>* l = nullptr;
LeftLeaningRBTreeNode<T>* r = nullptr;
if (type_flag == node_type::Node3)
{
l = parent->left;
r = l->right;
}
else
{
l = parent->right;
r = l->left;
}
if (is3Node(r) == false)
{
if (type_flag == node_type::Node3)
{
decideLinkOrSet(first_cycle, link_or_set_sub_tree_root, root_after_process, work_stack, l);
R2R3CombineFor3Node(parent, l, q, r);
work_stack.push(make_pair(l, make_pair(false, false)));
type_flag = node_type::Node4;
}
else
{
work_stack.push(make_pair(parent, make_pair(false, false)));
R3R4CombineFor4Node(l, r, q);
parent = l;
}
q = q->right;
first_cycle = false;
continue;
}
if (type_flag == node_type::Node3)
{
decideLinkOrSet(first_cycle, link_or_set_sub_tree_root, root_after_process, work_stack, r);
R3BorrowFromR2For3Node(parent, r->left, r);
}
else
{
work_stack.push(make_pair(parent, make_pair(false, true)));
R4BorrowFromR3For4Node(l, parent, r, r->left);
type_flag = node_type::Node3;
}
work_stack.push(make_pair(r, make_pair(false, false)));
}
else
{
if (type_flag == node_type::Node4)
{
work_stack.push(make_pair(parent, make_pair(false, true)));
work_stack.push(make_pair(parent->right, make_pair(false, false))); 可能有错
type_flag = node_type::Node3;
}
else
{
work_stack.push(make_pair(parent, make_pair(false, false)));
}
}
first_cycle = false;
parent = q;
q = q->right;
}
if (type_flag == node_type::Node3) //parent是3-Node
{
q = parent;
parent = parent->left;
delete q;
parent->color = Color::BLACK;
decideLinkOrSetForLeaf(_q, link_or_set_sub_tree_root, root_after_process, work_stack, parent);
}
else //parent是4-Node
{
delete parent->right;
parent->right = nullptr;
}
return root_after_process;
}
template <typename T>
bool LeftLeaningRBTree<T>::deleteMax()
{
if (root == nullptr)
return false;
LeftLeaningRBTreeNode<T>* parent = nullptr;
LeftLeaningRBTreeNode<T>* q = nullptr;
stack<pair<LeftLeaningRBTreeNode<T>*, pair<bool, bool>>> work_stack;
node_type type_flag;
if (is3Node(root) == false)
{
if (root->left == nullptr)
{
delete root;
root = nullptr;
return true;
}
bool r = is3Node(root->right);
if (r || is3Node(root->left))
{
parent = root->right;
if (r == false)
{
q = root->left;
R2BorrowFromR1For2Node(root, q, q->left);
root = q;
}
q = parent->right;
type_flag = node_type::Node3;
work_stack.push(make_pair(root, make_pair(false, false)));
}
else
{
flipColor(root->left);
flipColor(root->right);
parent = root;
q = root->right->right;
type_flag = node_type::Node4;
}
}
else
{
parent = root;
q = root->right;
type_flag = node_type::Node3;
}
deleteMaxMaintain(parent, q, work_stack, type_flag, true);
eliminate4Node(work_stack);
return true;
}
template <typename T>
bool checkLeftLeaningRBTreeProperty(LeftLeaningRBTreeNode<T>* root)
{
if (root == nullptr)
return true;
if (root->color != Color::BLACK)
return false;
T min;
T max;
if (isLeftLeaningRBTree(root, min, max) != -1)
return true;
return false;
}
template <typename T>
long long isLeftLeaningRBTree(LeftLeaningRBTreeNode<T>* root, T &min, T &max) //每次递归调用root一定不为空且颜色为黑色
{
if (root->right == nullptr)
{
if (root->left == nullptr || root->left->color == Color::RED && root->left->left == nullptr && root->left->right == nullptr)
{
if (root->left == nullptr)
{
min = root->data;
}
else
{
if (root->left->data >= root->data)
{
return -1;
}
min = root->left->data;
}
max = root->data;
return 0;
}
return -1;
}
else
{
if (root->left == nullptr || root->right->color == Color::RED)
{
return -1;
}
if (root->left->color == Color::RED)
{
if (root->left->left != nullptr && root->left->left->color == Color::BLACK && root->left->right != nullptr && root->left->right->color == Color::BLACK)
{
T left_left_min;
T left_left_max;
long long l = isLeftLeaningRBTree(root->left->left, left_left_min, left_left_max);
if (l == -1)
return -1;
if (left_left_max >= root->left->data)
return -1;
T left_right_min;
T left_right_max;
long long m = isLeftLeaningRBTree(root->left->right, left_right_min, left_right_max);
if (m == -1 || l != m)
return -1;
if (left_right_min <= root->left->data || left_right_max >= root->data)
return -1;
T right_min;
T right_max;
long long r = isLeftLeaningRBTree(root->right, right_min, right_max);
if (r == -1 || r != m)
return -1;
if (right_min <= root->data)
return -1;
min = left_left_min;
max = right_max;
return l + 1;
}
else
{
return -1;
}
}
else
{
T left_min;
T left_max;
long long l = isLeftLeaningRBTree(root->left, left_min, left_max);
if (l == -1)
return -1;
if (left_max >= root->data)
return -1;
T right_min;
T right_max;
long long m = isLeftLeaningRBTree(root->right, right_min, right_max);
if (m == -1 || l != m)
return -1;
if (right_min <= root->data)
return -1;
min = left_min;
max = right_max;
return l + 1;
}
}
}
int main()
{
const int N = 10000;
vector<int> seq(N);
for (int i = 0; i < N; i++)
{
seq[i] = i + 1;
}
shuffle(seq.begin(), seq.end(), default_random_engine());
LeftLeaningRBTree<int> obj;
for (size_t i = 0; i < seq.size(); ++i)
{
cout << "插入" << seq[i] << endl;
if (obj.insert(seq[i]))
{
cout << "插入成功!" << endl;
if (checkLeftLeaningRBTreeProperty(obj.root))
{
cout << "当前树是左倾红黑树" << endl;
}
else
{
cout << "ERROR:当前树不是左倾红黑树!" << endl;
exit(-1);
}
}
else
{
cout << "插入失败!" << endl;
exit(-1);
}
}
/*
for (size_t i = 0; i < seq.size(); ++i)
{
cout << "删除" << seq[i] << endl;
if (obj.deleteAnyKey(seq[i]))
{
cout << "删除成功!" << endl;
if (checkLeftLeaningRBTreeProperty(obj.root))
{
cout << "当前树是左倾红黑树" << endl;
}
else
{
cout << "ERROR:当前树不是左倾红黑树!" << endl;
exit(-1);
}
}
else
{
cout << "删除失败!" << endl;
exit(-1);
}
}*/
/*for (size_t i = 1; i <= N; ++i)
{
cout << "删除最小值" << i << endl;
if (obj.deleteMin())
{
cout << "删除成功!" << endl;
if (checkLeftLeaningRBTreeProperty(obj.root))
{
cout << "当前树是左倾红黑树" << endl;
}
else
{
cout << "ERROR:当前树不是左倾红黑树!" << endl;
exit(-1);
}
}
else
{
cout << "删除失败!" << endl;
exit(-1);
}
}*/
for (size_t i = N; i >= 1; --i)
{
cout << "删除最大值" << i << endl;
if (obj.deleteMax())
{
cout << "删除成功!" << endl;
if (checkLeftLeaningRBTreeProperty(obj.root))
{
cout << "当前树是左倾红黑树" << endl;
}
else
{
cout << "ERROR:当前树不是左倾红黑树!" << endl;
exit(-1);
}
}
else
{
cout << "删除失败!" << endl;
exit(-1);
}
}
return 0;
}