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

溫馨提示×

通過C++實踐深入探討紅黑樹的性質

c++
小樊
82
2024-04-26 19:07:59
欄目: 編程語言

紅黑樹是一種自平衡二叉搜索樹,它在插入和刪除元素時能夠保持樹的平衡,從而保證了樹的查找、插入和刪除操作的時間復雜度都是O(logn)。紅黑樹有以下幾個性質:

  1. 每個節點要么是黑色,要么是紅色。
  2. 根節點是黑色。
  3. 每個葉子節點(NIL節點)是黑色。
  4. 如果一個節點是紅色,則它的子節點都是黑色。
  5. 對于每個節點,從該節點到其所有后代葉子節點的簡單路徑上,均包含相同數量的黑色節點。

下面是一個簡單的C++實現紅黑樹的例子:

#include <iostream>
#include <vector>

enum Color { RED, BLACK };

template <typename T>
struct Node {
    T data;
    Color color;
    Node* left;
    Node* right;
    Node* parent;

    Node(T data) : data(data), color(RED), left(nullptr), right(nullptr), parent(nullptr) {}
};

template <typename T>
class RedBlackTree {
public:
    RedBlackTree() : root(nullptr) {}

    void insert(T data) {
        Node<T>* node = new Node<T>(data);
        insertNode(node);
        fixInsert(node);
    }

    void printInorder() {
        printInorderHelper(root);
        std::cout << std::endl;
    }

private:
    Node<T>* root;

    void insertNode(Node<T>* node) {
        Node<T>* temp = nullptr;
        Node<T>* current = root;

        while (current != nullptr) {
            temp = current;
            if (node->data < current->data) {
                current = current->left;
            } else {
                current = current->right;
            }
        }

        node->parent = temp;
        if (temp == nullptr) {
            root = node;
        } else if (node->data < temp->data) {
            temp->left = node;
        } else {
            temp->right = node;
        }
    }

    void fixInsert(Node<T>* node) {
        while (node != root && node->parent->color == RED) {
            if (node->parent == node->parent->parent->left) {
                Node<T>* uncle = node->parent->parent->right;
                if (uncle->color == RED) {
                    node->parent->color = BLACK;
                    uncle->color = BLACK;
                    node->parent->parent->color = RED;
                    node = node->parent->parent;
                } else {
                    if (node == node->parent->right) {
                        node = node->parent;
                        leftRotate(node);
                    }
                    node->parent->color = BLACK;
                    node->parent->parent->color = RED;
                    rightRotate(node->parent->parent);
                }
            } else {
                Node<T>* uncle = node->parent->parent->left;
                if (uncle->color == RED) {
                    node->parent->color = BLACK;
                    uncle->color = BLACK;
                    node->parent->parent->color = RED;
                    node = node->parent->parent;
                } else {
                    if (node == node->parent->left) {
                        node = node->parent;
                        rightRotate(node);
                    }
                    node->parent->color = BLACK;
                    node->parent->parent->color = RED;
                    leftRotate(node->parent->parent);
                }
            }
        }
        root->color = BLACK;
    }

    void leftRotate(Node<T>* node) {
        Node<T>* temp = node->right;
        node->right = temp->left;
        if (temp->left != nullptr) {
            temp->left->parent = node;
        }
        temp->parent = node->parent;
        if (node->parent == nullptr) {
            root = temp;
        } else if (node == node->parent->left) {
            node->parent->left = temp;
        } else {
            node->parent->right = temp;
        }
        temp->left = node;
        node->parent = temp;
    }

    void rightRotate(Node<T>* node) {
        Node<T>* temp = node->left;
        node->left = temp->right;
        if (temp->right != nullptr) {
            temp->right->parent = node;
        }
        temp->parent = node->parent;
        if (node->parent == nullptr) {
            root = temp;
        } else if (node == node->parent->right) {
            node->parent->right

0
嵩明县| 搜索| 临潭县| 永顺县| 福泉市| 沧源| 潜山县| 穆棱市| 漳平市| 翼城县| 祁连县| 宜兰市| 安多县| 新宁县| 抚松县| 水富县| 四会市| 屏东县| 金湖县| 远安县| 堆龙德庆县| 安阳市| 连江县| 和平县| 芒康县| 抚远县| 蕉岭县| 孟村| 五寨县| 介休市| 儋州市| 苏尼特左旗| 宜章县| 孝昌县| 喀喇沁旗| 大洼县| 桃园市| 眉山市| 枣阳市| 临洮县| 连城县|