繁体   English   中英

红黑树插入CLRS

[英]Red-Black Tree Insertion CLRS

CLRS 3版中描述的算法。 看起来错了。 我尝试过工具,但插入效果不佳。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

const char BLACK = 'B';
const char RED = 'R';

template <class T>
class Node{

    public:
        Node *left;
        Node *right;
        Node *parent;
        char color;
        T key;

        Node(T x){
            this->left = NULL;
            this->right = NULL;
            this->parent = NULL;
            this->key = x;
            this->color = RED;
        };

        virtual ~Node(){};
};

template <class T>
class RedBlackTree{

    private:

        int ammount;
        int h;
        int lastAmmount;
        Node<T> *root;
        Node<T> *NIL;

        void destroy_node(Node<T> *&node);
        Node<T> *remove_node(Node<T> *&node, T x);  // not imeplemented yet
        Node<T> *search_node(Node<T> *&node, T x);  // not imeplemented yet

        void printInfo(Node<T> *&x);
        void printInOrder_node(Node<T> *&node);

        void printInLevel_node(Node<T> *&node, int level);
        int calculeHeight(Node<T> *&node);

        void rotateLeft(Node<T> *&x);
        void rotateRight(Node<T> *&y);
        void insertFixUp(Node<T> *&x);


    public:

        RedBlackTree();
        virtual ~RedBlackTree();
        void destroy();

        void insert(T x);
        void remove(T x);       // not imeplemented yet
        Node<T> *search(T x);   // not implemented yet

        int height();
        void printInOrder();
        void printInLevel();
};


template <class T>
RedBlackTree<T>::RedBlackTree(){

    this->ammount = 0;
    this->lastAmmount = -1;
    this->h = 0;

    this->NIL = new Node<T>(-1);
    this->NIL->color = BLACK;
    this->NIL->left = this->NIL->right = this->NIL->parent = this->NIL;


    this->root = this->NIL;
    this->root->color = BLACK;
}

template <class T>
RedBlackTree<T>::~RedBlackTree(){
    delete this->root;
}



template <class T>
void RedBlackTree<T>::destroy_node(Node<T> *&node){
    if(node != NULL){
        this->destroy(node->left);
        this->destroy(node->right);
        delete node;
    }
}

template <class T>
void RedBlackTree<T>::destroy(){
    this->destroy_node(this->root);
}





// RB methods
template <class T>
void RedBlackTree<T>::rotateLeft(Node<T> *&x){

    Node<T> *y = x->right;
    x->right = y->left;

    if(y->left != this->NIL)
        y->left->parent = x;

    y->parent = x->parent;

    if(x->parent == this->NIL)
        this->root = y;
    else if(x == x->parent->left)
        x->parent->left = y;
    else
        x->parent->right = y;

    y->left = x;
    x->parent = y;
}


template <class T>
void RedBlackTree<T>::rotateRight(Node<T> *&y){

    Node<T> *x = y->left;
    y->left = x->right;

    if(x->right != this->NIL)
        x->right->parent = y;

    x->parent = y->parent;

    if(y->parent == this->NIL)
        this->root = x;
    else if(y == y->parent->left)
        y->parent->left = x;
    else
        y->parent->right = x;

    x->right = y;
    y->parent = x;
}


template <class T>
void RedBlackTree<T>::insertFixUp(Node<T> *&z){


    Node<T> *y;

    while(z != this->root and z->parent->color == RED){
        if(z->parent == z->parent->parent->left){
            y = z->parent->parent->right;
            if(y->color == RED){
                z->parent->color = BLACK;
                y->color = BLACK;
                z->parent->parent->color = RED;
                z = z->parent->parent;
            }
            else{
                if(z == z->parent->right){
                    z = z->parent;
                    this->rotateLeft(z);
                }

                z->parent->color = BLACK;
                z->parent->parent->color = RED;
                this->rotateRight(z->parent->parent);
            }
        }
        else{
            y = z->parent->parent->left;
            if(y->color == RED){
                z->parent->color = BLACK;
                y->color = BLACK;
                z->parent->parent->color = RED;
                z = z->parent->parent;
            }
            else{
                if(z == z->parent->left){
                    z = z->parent;
                    this->rotateRight(z);
                }

                z->parent->color = BLACK;
                z->parent->parent->color = RED;
                this->rotateLeft(z->parent->parent);
            }
        }
    }


    this->root->color = BLACK;

}



template <class T>
void RedBlackTree<T>::insert(T val){

    Node<T> *z = new Node<T>(val);
    Node<T> *x = this->root;
    Node<T> *y = this->NIL;

    while(x != this->NIL){
        y = x;
        if(z->key < x->key)
            x = x->left;
        else
            x = x->right;
    }


    z->parent = y;

    if(y == this->NIL)
        this->root = z;
    else if(z->key < y->key)
        y->left = z;
    else
        y->right = z;


    z->left = this->NIL;
    z->right = this->NIL;
    z->color = RED;

    this->insertFixUp(z);
}





template <class T>
int RedBlackTree<T>::height(){

    if(this->lastAmmount == this->ammount)
        return this->h;

    this->h = this->calculeHeight(this->root);
    this->lastAmmount = this->ammount;
    return this->h;
}


template <class T>
int RedBlackTree<T>::calculeHeight(Node<T> *&node){
    if(node == this->NIL)
        return 0;

    int l_h = this->calculeHeight(node->left);
    int r_h = this->calculeHeight(node->right);

    if(l_h > r_h)
        return l_h+1;

    return r_h+1;
}



template <class T>
void RedBlackTree<T>::printInfo(Node<T> *&x){
    cout << "key=";
    cout << x->key;

    cout << "  l->key=";
    if( x->left == this->NIL) 
        cout << "N";
    else 
        cout << x->left->key;

    cout << "  r->key=";
    if( x->right == this->NIL) 
        cout << "N";
    else 
        cout << x->right->key;

    cout << "  p->key=";
    if( x->parent == this->NIL) 
        cout << "N";
    else 
        cout << x->parent->key;

    cout << "  color=" << x->color << endl;
}

template <class T>
void RedBlackTree<T>::printInOrder_node(Node<T> *&node){
    if(node != this->NIL){
        this->printInOrder_node(node->left);
        //cout << " " << node->key;
        this->printInfo(node);
        this->printInOrder_node(node->right);
    }
}

template <class T>
void RedBlackTree<T>::printInOrder(){
    this->printInOrder_node(this->root);
}



template <class T>
void RedBlackTree<T>::printInLevel(){
    int h = this->height();
    for(int i=1; i<=h; i++)
        this->printInLevel_node(this->root, i);
}


template <class T>
void RedBlackTree<T>::printInLevel_node(Node<T> *&node, int level){
    if(node == this->NIL)
        return;

    if(level == 1)
        this->printInfo(node); //cout << node->key << " ";
    else if(level > 1){
        this->printInLevel_node(node->left, level-1);
        this->printInLevel_node(node->right, level-1);
    }
}





int main(){


    RedBlackTree<int> *bt = new RedBlackTree<int>();

    int v[9] = {11, 2, 14, 1, 7, 15, 5, 8, 4};
    for(int i=0; i<9; i++){
        int x = v[i];
        cout << x << " ";
        bt->insert(x);
    }

    cout << endl;

    cout << "In Level:" << endl; 
    bt->printInLevel();
    cout << endl;



    delete bt;

    return 0;
}

我试图找到以前的话题一些人的解释,但不是似乎工作。 此代码基本上等于本书的伪代码。 如预期的结果是在级别

       7
     /   \
   2      11
 /  \    /  \
1    5  8    14
    /         \
   4           15

那怎么了 谢谢!

考虑添加第二个节点时发生的情况。 您从此开始:

11 (black)

然后添加新节点:

  11 (black)
 /
2 (red)

然后尝试修复它:

template <class T>
void RedBlackTree<T>::insertFixUp(Node<T> *&z){
  Node<T> *y;

  while(z != this->root and z->parent->color == RED){
    ...
  }
this->root->color = BLACK;
}

请注意,由于11开头是黑色,所以控件永远不会进入循环,因此对insertFixUp的调用没有任何作用。 因此,您只剩下一棵有红叶的树,这不是有效的红黑树。 之后,如果您的代码期望在有效的红黑树上工作,则可能会出错。

可能还有其他错误,但是在尝试添加第三个节点之前,应尝试使两节点树正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM