#include <iostream>
#include <stack>
using namespace std;
typedef enum {
Red,
Black,
}Color;
typedef struct Node {
int key;
Color color;
struct Node *parent;
struct Node *left;
struct Node *right;
}Node, *ptrNode;
class RedBlackTree {
private:
ptrNode tree;
ptrNode NIL;
void init();
void leftRotate(ptrNode p);
void rightRotate(ptrNode p);
void insertFixup(ptrNode p);
public:
RedBlackTree();
void insert(int key);
void traverse();
};
void RedBlackTree::insertFixup(ptrNode p) {
ptrNode uncle;
while(p->parent->color == Red) {
if (p->parent == p->parent->parent->left) {
uncle = p->parent->parent->right;
if (uncle->color == Red) {
p->parent->color = Black;
uncle->color = Black;
p->parent->parent->color = Red;
p = p->parent->parent;
} else {
if (p == p->parent->right) {
p = p->parent;
leftRotate(p);
}
p->parent->color = Black;
p->parent->parent->color = Red;
rightRotate(p->parent->parent);
}
} else {
uncle = p->parent->parent->left;
if (uncle->color == Red) {
p->parent->color = Black;
uncle->color = Black;
p->parent->parent->color = Red;
p = p->parent->parent;
} else {
if (p == p->parent->left) {
p = p->parent;
rightRotate(p);
}
p->parent->color = Black;
p->parent->parent->color = Red;
leftRotate(p->parent->parent);
}
}
}
tree->color = Black;
}
void RedBlackTree::traverse() {
ptrNode p = tree;
stack<ptrNode> s;
while (p!=NIL || !s.empty()) {
if (p!=NIL) {
s.push(p);
p = p->left;
} else {
std::cout << s.top()->key << " ";
p = s.top()->right;
s.pop();
}
}
}
void RedBlackTree::insert(int key) {
ptrNode p = new Node();
p->key = key;
p->color = Red;
p->parent = NIL;
p->left = NIL;
p->right = NIL;
ptrNode parent = NIL;
ptrNode cur = tree;
while (cur != NIL) {
parent = cur;
if (key < cur->key) {
cur = cur->left;
} else {
cur = cur->right;
}
}
p->parent = parent;
if (parent == NIL) {
tree = p;
} else {
if (key < parent->key) {
parent->left = p;
} else {
parent->right = p;
}
}
insertFixup(p);
}
void RedBlackTree::rightRotate(ptrNode p) {
ptrNode lChild = p->left;
p->left = lChild->right;
lChild->right->parent = p;
lChild->parent = p->parent;
if (p->parent == NIL) {
tree = lChild;
} else {
if (p == p->parent->left) {
p->parent->left = lChild;
} else {
p->parent->right = lChild;
}
}
lChild->right = p;
p->parent = lChild;
}
void RedBlackTree::leftRotate(ptrNode p) {
ptrNode rChild = p->right;
p->right = rChild->left;
rChild->left->parent = p;
rChild->parent = p->parent;
if (p->parent == NIL) {
tree = rChild;
} else {
if (p == p->parent->left) {
p->parent->left = rChild;
} else {
p->parent->right = rChild;
}
}
rChild->left = p;
p->parent = rChild;
}
RedBlackTree::RedBlackTree() {
init();
}
void RedBlackTree::init() {
NIL = new Node();
NIL->color = Black;
NIL->parent = NULL;
NIL->left = NULL;
NIL->right = NULL;
tree = NIL;
}
int main() {
RedBlackTree *rbt = new RedBlackTree();
rbt->insert(11);
rbt->insert(2);
rbt->insert(14);
rbt->insert(1);
rbt->insert(7);
rbt->insert(15);
rbt->insert(5);
rbt->insert(8);
rbt->insert(4);
rbt->traverse();
delete rbt;
return 0;
}
红黑树——删除操作(未完成)
最新推荐文章于 2024-05-29 16:54:31 发布