帮我写一段红黑树的实现代码用C++ 以下是红黑树的实现代码,包括插入、删除、查找等操作:

#include <iostream>
using namespace std;

enum Color {RED, BLACK};

struct Node {
    int val;
    Color color;
    Node *left, *right, *parent;
    Node(int v, Color c): val(v), color(c), left(NULL), right(NULL), parent(NULL) {}
};

class RedBlackTree {
private:
    Node *root;
    void leftRotate(Node *x);
    void rightRotate(Node *x);
    void insertFixup(Node *z);
    void deleteFixup(Node *x);
    Node* minimum(Node *x);
    Node* maximum(Node *x);
    void transplant(Node *u, Node *v);
public:
    RedBlackTree(): root(NULL) {}
    ~RedBlackTree() {}
    void insert(int val);
    void remove(int val);
    Node* search(int val);
    void inorderTraversal();
};

void RedBlackTree::leftRotate(Node *x) {
    Node *y = x->right;
    x->right = y->left;
    if (y->left != NULL) {
        y->left->parent = x;
    }
    y->parent = x->parent;
    if (x->parent == NULL) {
        root = y;
    } else if (x == x->parent->left) {
        x->parent->left = y;
    } else {
        x->parent->right = y;
    }
    y->left = x;
    x->parent = y;
}

void RedBlackTree::rightRotate(Node *x) {
    Node *y = x->left;
    x->left = y->right;
    if (y->right != NULL) {
        y->right->parent = x;
    }
    y->parent = x->parent;
    if (x->parent == NULL) {
        root = y;
    } else if (x == x->parent->left) {
        x->parent->left = y;
    } else {
        x->parent->right = y;
    }
    y->right = x;
    x->parent = y;
}

void RedBlackTree::insert(int val) {
    Node *z = new Node(val, RED);
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
        y = x;
        if (z->val < x->val) {
            x = x->left;
        } else {
            x = x->right;
        }
    }   
    z->parent = y;
    if (y == NULL) {
        root = z;
    } else if (z->val < y->val) {
        y->left = z;
    } else {
        y->right = z;
    }
    insertFixup(z);
}

void RedBlackTree::insertFixup(Node *z) {
    while (z->parent != NULL && z->parent->color == RED) {
        if (z->parent == z->parent->parent->left) {
            Node *y = z->parent->parent->right;
            if (y != NULL && 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;
                    leftRotate(z);
                }
                z->parent->color = BLACK;
                z->parent->parent->color = RED;
                rightRotate(z->parent->parent);
            }
        } else {
            Node *y = z->parent->parent->left;
            if (y != NULL && 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;
                    rightRotate(z);
                }
                z->parent->color = BLACK;
                z->parent->parent->color = RED;
                leftRotate(z->parent->parent);
            }
        }
    }
    root->color = BLACK;
}

void RedBlackTree::remove(int val) {
    Node *z = search(val);
    if (z == NULL) {
        return;
    }
    Node *y = z;
    Color yOriginalColor = y->color;
    Node *x;
    if (z->left == NULL) {
        x = z->right;
        transplant(z, z->right);
    } else if (z->right == NULL) {
        x = z->left;
        transplant(z, z->left);
    } else {
        y = minimum(z->right);
        yOriginalColor = y->color;
        x = y->right;
        if (y->parent == z) {
            x->parent = y;
        } else {
            transplant(y, y->right);
            y->right = z->right;
            y->right->parent = y;
        }
        transplant(z, y);
        y->left = z->left;
        y->left->parent = y;
        y->color = z->color;
    }
    if (yOriginalColor == BLACK) {
        deleteFixup(x);
    }
}

void RedBlackTree::deleteFixup(Node *x) {
    while (x != root && x->color == BLACK) {
        if (x == x->parent->left) {
            Node *w = x->parent->right;
            if (w->color == RED) {
                w->color = BLACK;
                x->parent->color = RED;
                leftRotate(x->parent);
                w = x->parent->right;
            }
            if (w->left->color == BLACK && w->right->color == BLACK) {
                w->color = RED;
                x = x->parent;
            } else {
                if (w->right->color == BLACK) {
                    w->left->color = BLACK;
                    w->color = RED;
                    rightRotate(w);
                    w = x->parent->right;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->right->color = BLACK;
                leftRotate(x->parent);
                x = root;
            }
        } else {
            Node *w = x->parent->left;
            if (w->color == RED) {
                w->color = BLACK;
                x->parent->color = RED;
                rightRotate(x->parent);
                w = x->parent->left;
            }
            if (w->right->color == BLACK && w->left->color == BLACK) {
                w->color = RED;
                x = x->parent;
            } else {
                if (w->left->color == BLACK) {
                    w->right->color = BLACK;
                    w->color = RED;
                    leftRotate(w);
                    w = x->parent->left;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->left->color = BLACK;
                rightRotate(x->parent);
                x = root;
            }
        }
    }
    x->color = BLACK;
}

Node* RedBlackTree::minimum(Node *x) {
    while (x->left != NULL) {
        x = x->left;
    }
    return x;
}

Node* RedBlackTree::maximum(Node *x) {
    while (x->right != NULL) {
        x = x->right;
    }
    return x;
}

void RedBlackTree::transplant(Node *u, Node *v) {
    if (u->parent == NULL) {
        root = v;
    } else if (u == u->parent->left) {
        u->parent->left = v;
    } else {
        u->parent->right = v;
    }
    if (v != NULL) {
        v->parent = u->parent;
    }
}

Node* RedBlackTree::search(int val) {
    Node *x = root;
    while (x != NULL && x->val != val) {
        if (val < x->val) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    return x;
}

void RedBlackTree::inorderTraversal() {
    Node *x = root;
    while (x != NULL) {
        if (x->left == NULL) {
            cout << x->val << " ";
            x = x->right;
        } else {
            Node *y = x->left;
            while (y->right != NULL && y->right != x) {
                y = y->right;
            }
            if (y->right == NULL) {
                y->right = x;
                x = x->left;
            } else {
                y->right = NULL;
                cout << x->val << " ";
                x = x->right;
            }
        }
    }
}

int main() {
    RedBlackTree tree;
    tree.insert(10);
    tree.insert(20);
    tree.insert(30);
    tree.insert(100);
    tree.insert(200);
    tree.insert(300);
    tree.insert(1000);
    tree.insert(2000);
    tree.insert(3000);
    tree.inorderTraversal(); // 10 20 30 100 200 300 1000 2000 3000
    tree.remove(20);
    tree.remove(1000);
    tree.remove(3000);
    tree.inorderTraversal(); // 10 30 100 200 300 2000
    return 0;
}

以上代码实现了红黑树的插入、删除、查找和中序遍历操作。

帮我写一段红黑树的实现代码用C++

原文地址: https://www.cveoy.top/t/topic/dvK4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录