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

溫馨提示×

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
海安县| 巴南区| 宁津县| 华阴市| 江安县| 新干县| 平谷区| 大关县| 洞头县| 清原| 尉氏县| 新闻| 江都市| 大理市| 平定县| 长垣县| 巴东县| 曲松县| 措勤县| 扎赉特旗| 双江| 北安市| 虎林市| 扶绥县| 内黄县| 新巴尔虎右旗| 仲巴县| 拉萨市| 老河口市| 青州市| 泗水县| 定兴县| 石阡县| 遂平县| 祁连县| 河池市| 手机| 新乡市| 南投县| 云梦县| 庆阳市|