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

溫馨提示×

c++紅黑樹實現代碼怎么寫

c++
小億
82
2024-04-28 09:31:53
欄目: 編程語言

以下是一個簡單的紅黑樹實現代碼示例:

```cpp

#include

enum class Color { RED, BLACK };

template

class Node {

public:

T data;

Color color;

Node* parent;

Node* left;

Node* right;

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

};

template

class RedBlackTree {

private:

Node* root;

void rotateLeft(Node* node) {

Node* 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 rotateRight(Node* node) {

Node* 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 = temp;

} else {

node->parent->left = temp;

}

temp->right = node;

node->parent = temp;

}

public:

RedBlackTree() : root(nullptr) {}

void insert(T data) {

Node* newNode = new Node(data);

Node* parent = nullptr;

Node* current = root;

while(current != nullptr) {

parent = current;

if(newNode->data < current->data) {

current = current->left;

} else {

current = current->right;

}

}

newNode->parent = parent;

if(parent == nullptr) {

root = newNode;

} else if(newNode->data < parent->data) {

parent->left = newNode;

} else {

parent->right = newNode;

}

// Fix the tree

insertionFixup(newNode);

}

void insertionFixup(Node* node) {

while(node != root && node->parent->color == Color::RED) {

if(node->parent == node->parent->parent->left) {

Node* uncle = node->parent->parent->right;

if(uncle != nullptr && uncle->color == Color::RED) {

node->parent->color = Color::BLACK;

uncle->color = Color::BLACK;

node->parent->parent->color = Color::RED;

node = node->parent->parent;

} else {

if(node == node->parent->right) {

node = node->parent;

rotateLeft(node);

}

node->parent->color = Color::BLACK;

node->parent->parent->color = Color::RED;

rotateRight(node->parent->parent);

}

} else {

Node* uncle = node->parent->parent->left;

if(uncle != nullptr && uncle->color == Color::RED) {

node->parent->color = Color::BLACK;

uncle->color = Color::BLACK;

node->parent->parent->color = Color::RED;

node = node->parent->parent;

} else {

if(node == node->parent->left) {

node = node->parent;

rotateRight(node);

}

node->parent->color = Color::BLACK;

node->parent->parent->color = Color::RED;

rotateLeft(node->parent->parent);

}

}

}

root->color = Color::BLACK;

}

};

int main() {

RedBlackTree rbTree;

rbTree.insert(10);

rbTree.insert(20);

rbTree.insert(30);

return 0;

}

```

這段代碼實現了一個簡單的紅黑樹,并實現了插入節點以及插入后的修復操作。您可以根據需要進行擴展和修改。

0
铜鼓县| 枞阳县| 民乐县| 内江市| 大关县| 襄城县| 体育| 邹平县| 徐州市| 绿春县| 嘉鱼县| 安泽县| 泰宁县| 遵义县| 互助| 兰西县| 泽库县| 鄂托克前旗| 平陆县| 霞浦县| 隆回县| 天门市| 安义县| 祁东县| 竹溪县| 潍坊市| 泰安市| 内乡县| 乌拉特前旗| 应城市| 淄博市| 镇赉县| 新野县| 湖州市| 博客| 汝南县| 铅山县| 治多县| 密山市| 黄龙县| 宿松县|