Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //реализация функций для КЧ дерева
- //1 - red 2 - black
- BinaryTree::RbNode::RbNode(int input, TreeNode* parent) {
- node = input;
- color = 1;
- L = R = nullptr;
- P = parent;
- }
- BinaryTree::RbNode::RbNode() {
- node = NULL;
- color = 2;
- L = R = P = nullptr;
- }
- void BinaryTree::RbNode::print_color() {
- if (color == 1) cout << "R";
- else cout << "B";
- }
- unsigned char BinaryTree::RbNode::get_color() {
- return color;
- }
- void BinaryTree::RbNode::change_color(unsigned char c) {
- color = c;
- }
- BinaryTree::TreeNode* BinaryTree::RbNode::find_grandpa() {
- if (P != NULL) {
- return P->P;
- }
- else {
- return nullptr;
- }
- }
- BinaryTree::TreeNode* BinaryTree::RbNode::find_uncle() {
- TreeNode* p = find_grandpa();
- if (p == nullptr) {
- return nullptr;
- }
- if (P == p->L) {
- return p->R;
- }
- else {
- return p->L;
- }
- }
- void BinaryTree::RbNode::rb_left_Rotate() {
- TreeNode* pivot = R;
- pivot->P = P;
- if (P != nullptr) {
- if (P->L == this) {
- P->L = pivot;
- }
- else {
- P->R = pivot;
- }
- }
- R = pivot->L;
- if (pivot->L != nullptr) {
- pivot->L->P = this;
- }
- P = pivot;
- pivot->L = this;
- }
- void BinaryTree::RbNode::rb_right_Rotate() {
- TreeNode* pivot = L;
- pivot->P = P;
- if (P != nullptr) {
- if (P->L == this) {
- P->L = pivot;
- }
- else {
- P->R = pivot;
- }
- }
- L = pivot->R;
- if (pivot->R != nullptr) {
- pivot->R->P = this;
- }
- P = pivot;
- pivot->R = this;
- }
- void BinaryTree::RbNode::insert_1(TreeNode* bl) {
- if (P == nullptr) {
- color = 2;
- }
- else {
- insert_2(bl);
- }
- }
- void BinaryTree::RbNode::insert_2(TreeNode* bl) {
- if (P->get_color() == 2) {
- return;
- }
- else {
- insert_3(bl);
- }
- }
- void BinaryTree::RbNode::insert_3(TreeNode* bl) {
- TreeNode* u = find_uncle();
- TreeNode* g;
- if (u != nullptr && u->get_color() == 1) {
- P->change_color(2);
- u->change_color(2);
- g = find_grandpa();
- g->change_color(1);
- g->insert_1(bl);
- }
- else {
- insert_4(bl);
- }
- }
- void BinaryTree::RbNode::insert_4(TreeNode* bl) {
- TreeNode* g = find_grandpa();
- if (g && this == P->R && P == g->L) {
- P->rb_left_Rotate();
- bl = bl->L;
- }
- else if (g && this == P->L && P == g->R) {
- P->rb_right_Rotate();
- bl = bl->R;
- }
- insert_5(bl);
- }
- void BinaryTree::RbNode::insert_5(TreeNode* bl) {
- TreeNode* g = bl->find_grandpa();
- bl->P->change_color(2);
- if (g) g->change_color(1);
- if (g && bl == bl->P->L && bl->P == g->L) {
- g->rb_right_Rotate();
- }
- else if (g) {
- g->rb_left_Rotate();
- }
- }
- BinaryTree::TreeNode* BinaryTree::RbNode::add(int input, TreeNode* bl) {
- if (!bl) bl = new RbNode(input, nullptr);
- TreeNode* ptr = bl;
- TreeNode* parent = bl;
- while (ptr != nullptr) {
- parent = ptr;
- if (input > ptr->node) {
- ptr = ptr->R;
- }
- else if (input < ptr->node) {
- ptr = ptr->L;
- }
- }
- ptr = new RbNode(input, parent);
- if (input > parent->node) {
- parent->R = ptr;
- }
- else {
- parent->L = ptr;
- }
- ptr->insert_1(ptr);
- if (bl->P) return P;
- else return bl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement