91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

在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
寿光市| 客服| 云龙县| 卢氏县| 瓦房店市| 贡觉县| 遵义县| 乌拉特后旗| 阳东县| 游戏| 顺义区| 林甸县| 蕲春县| 纳雍县| 昆明市| 新巴尔虎左旗| 荔浦县| 刚察县| 河间市| 永昌县| 凤庆县| 张家界市| 新干县| 安乡县| 淮南市| 鄂尔多斯市| 河西区| 石屏县| 铁岭县| 文化| 沁阳市| 调兵山市| 平安县| 卓资县| 田林县| 新巴尔虎右旗| 哈密市| 荣成市| 定日县| 隆林| 磐石市|