中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

在C++中實現紅黑樹的基本結構

c++
小樊
82
2024-04-26 18:56:08
欄目: 編程語言

紅黑樹是一種自平衡的二叉搜索樹,其基本結構可以通過以下代碼實現:

#include <iostream>
using namespace std;

enum Color {RED, BLACK};

struct Node {
    int data;
    Color color;
    Node* left;
    Node* right;
    Node* parent;
    
    Node(int val) : data(val), color(RED), left(nullptr), right(nullptr), parent(nullptr) {}
};

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

public:
    RedBlackTree() : root(nullptr) {}
    
    void insert(int val) {
        Node* z = new Node(val);
        Node* y = nullptr;
        Node* x = root;
        
        while (x != nullptr) {
            y = x;
            if (z->data < x->data) {
                x = x->left;
            } else {
                x = x->right;
            }
        }
        
        z->parent = y;
        if (y == nullptr) {
            root = z;
        } else if (z->data < y->data) {
            y->left = z;
        } else {
            y->right = z;
        }
        
        fixViolation(z);
    }
    
    // Traversal functions (inorder, preorder, postorder)
    // Other functions like delete, search, etc. can also be added
    
};

int main() {
    RedBlackTree rbTree;
    
    rbTree.insert(7);
    rbTree.insert(3);
    rbTree.insert(18);
    rbTree.insert(10);
    rbTree.insert(22);
    
    return 0;
}

在上面的代碼中,我們定義了一個Node結構體表示紅黑樹的節點,其中包含數據值、顏色、左右孩子節點以及父節點。然后定義了一個RedBlackTree類來實現紅黑樹的基本操作,包括左旋轉、右旋轉、修正插入操作中可能出現的違反規則的情況等。

0
莒南县| 天全县| 刚察县| 常德市| 铜川市| 兴义市| 潞西市| 洛南县| 高淳县| 五河县| 永善县| 偃师市| 宁德市| 旬邑县| 杭州市| 若尔盖县| 罗甸县| 黑河市| 锡林浩特市| 安阳县| 泰和县| 来宾市| 孙吴县| 霍邱县| 那坡县| 白玉县| 泰和县| 民勤县| 东阳市| 陆河县| 宾阳县| 潜山县| 项城市| 克东县| 嘉兴市| 苍南县| 屏东县| 五华县| 九江市| 孟村| 民勤县|