117.info
人生若只如初见

实现一个红黑树迭代器:C++编程挑战

#include 
#include 
#include 

enum Color {
    RED,
    BLACK
};

struct Node {
    int data;
    Color color;
    Node* left;
    Node* right;
    Node* parent;

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

class RedBlackTree {
private:
    Node* root;

public:
    RedBlackTree() : root(nullptr) {}

    void insert(int data) {
        Node* new_node = new Node(data, RED);
        if (root == nullptr) {
            root = new_node;
            root->color = BLACK;
            return;
        }

        Node* current = root;
        Node* parent = nullptr;

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

        new_node->parent = parent;
        if (data < parent->data) {
            parent->left = new_node;
        } else {
            parent->right = new_node;
        }

        fix_insert(new_node);
    }

    void fix_insert(Node* node) {
        while (node != root && node->parent->color == RED) {
            if (node->parent == node->parent->parent->left) {
                Node* uncle = node->parent->parent->right;
                if (uncle != nullptr && 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;
                        rotate_left(node);
                    }
                    node->parent->color = BLACK;
                    node->parent->parent->color = RED;
                    rotate_right(node->parent->parent);
                }
            } else {
                Node* uncle = node->parent->parent->left;
                if (uncle != nullptr && 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;
                        rotate_right(node);
                    }
                    node->parent->color = BLACK;
                    node->parent->parent->color = RED;
                    rotate_left(node->parent->parent);
                }
            }
        }
        root->color = BLACK;
    }

    void rotate_left(Node* node) {
        Node* right_child = node->right;
        node->right = right_child->left;
        if (right_child->left != nullptr) {
            right_child->left->parent = node;
        }
        right_child->parent = node->parent;
        if (node->parent == nullptr) {
            root = right_child;
        } else if (node == node->parent->left) {
            node->parent->left = right_child;
        } else {
            node->parent->right = right_child;
        }
        right_child->left = node;
        node->parent = right_child;
    }

    void rotate_right(Node* node) {
        Node* left_child = node->left;
        node->left = left_child->right;
        if (left_child->right != nullptr) {
            left_child->right->parent = node;
        }
        left_child->parent = node->parent;
        if (node->parent == nullptr) {
            root = left_child;
        } else if (node == node->parent->right) {
            node->parent->right = left_child;
        } else {
            node->parent->left = left_child;
        }
        left_child->right = node;
        node->parent = left_child;
    }

    void inorder_traversal(Node* node, std::queue<int>& q) {
        if (node == nullptr) {
            return;
        }
        inorder_traversal(node->left, q);
        q.push(node->data);
        inorder_traversal(node->right, q);
    }

    std::queue inorder() {
        std::queue q;
        inorder_traversal(root, q);
        return q;
    }
};

class RedBlackTreeIterator {
private:
    std::queue inorder_list;

public:
    RedBlackTreeIterator(RedBlackTree& tree) {
        inorder_list = tree.inorder();
    }

    int next() {
        int data = https://www.yisu.com/ask/inorder_list.front();"hljs">bool hasNext() {
        return !inorder_list.empty();
    }
};

int main() {
    RedBlackTree tree;
    tree.insert(10);
    tree

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe7f4AzsIBgVfBlw.html

推荐文章

  • c# winform组件 怎样集成

    在C# WinForms应用程序中集成组件通常涉及以下几个步骤: 添加引用: 首先,确保你已经安装了所需的组件。例如,如果你想要集成一个第三方库,你需要将该库的DLL...

  • c# winform组件 怎样提高效率

    要提高C# WinForms应用程序的效率,可以从多个方面入手,包括优化绘图过程、使用异步操作、合理设置控件属性等。以下是一些具体的优化策略:
    优化绘图过程 ...

  • c# winform组件 怎样自定义

    在C#中,要自定义WinForms组件,您需要创建一个继承自现有控件的新类,并重写其构造函数、事件处理程序和其他相关方法。以下是一个简单的示例,说明如何创建一个...

  • c# winform组件 怎样选择合适

    在C# WinForms应用程序开发中,选择合适的组件对于构建高效、用户友好的界面至关重要。以下是一些选择合适组件的建议:
    控件选择 根据需求选择:选择控件时...

  • 红黑树的查询效率:C++实例分析

    红黑树是一种自平衡的二叉搜索树,其查询效率非常高,时间复杂度为O(log n),其中n为树中节点的个数。下面通过一个C++实例来演示红黑树的查询效率。
    #inclu...

  • 使用红黑树优化C++应用程序的数据结构

    红黑树是一种自平衡的二叉搜索树,它能够在插入和删除操作时保持树的平衡,从而确保树的高度不会过高,提高了查找、插入和删除操作的效率。
    在C++应用程序中...

  • 分析红黑树与AVL树在C++中的性能差异

    在C++中,红黑树和AVL树是两种常见的自平衡二叉搜索树。它们都具有对数时间复杂度的查找、插入和删除操作,但在某些情况下它们的性能会有一些差异。 插入和删除操...

  • 应对C++红黑树的常见面试问题

    什么是红黑树?
    红黑树是一种自平衡的二叉搜索树,它在每个节点上增加了一个额外的属性表示节点的颜色(红色或黑色),并通过一些规则来确保树的平衡性。 红...