二叉搜索树之红黑树

Rbtree 是一种自平衡的二叉查找树,称为红黑树。它在计算机科学中用于实现关联数组,可以在 O(log n) 时间内进行查找、插入和删除操作,其中 n 是树中元素的数量。红黑树是一种复杂的树形数据结构,但它的最坏情况运行时间性能非常好,并且在实践中是高效的。

红黑树是一种每个节点都带有颜色属性的二叉查找树,颜色可以是红色或黑色。除了满足二叉查找树的一般要求外,红黑树还满足以下额外的性质:

每个节点是红色或黑色。
根节点是黑色。
每个叶节点(NIL 或空节点)是黑色。
每个红色节点必须有两个黑色的子节点(从每个叶子到根的所有路径上不能有两个连续的红色节点)。
从任一节点到其每个叶节点的所有路径都包含相同数目的黑色节点。
红黑树的这些性质保证了它的平衡性,即最长路径不超过最短路径的两倍,从而使得它在最坏情况下仍然具有高效的性能。与 AVL 树相比,红黑树在某些操作中可能需要更多的旋转操作来保持平衡,但在实践中,红黑树通常更快,因为它们的插入和删除操作通常需要更少的旋转。

以下是一个简单的红黑树的实现例子:

#include <stdio.h>  
#include <stdlib.h>  
  
typedef enum { RED, BLACK } Color;  
  
typedef struct Node {  
    int data;  
    struct Node *left, *right, *parent;  
    Color color;  
} Node;  
  
typedef struct RBTree {  
    Node *root;  
} RBTree;  
  
Node* createNode(int data) {  
    Node *newNode = (Node*) malloc(sizeof(Node));  
    newNode->data = data;  
    newNode->left = newNode->right = newNode->parent = NULL;  
    newNode->color = RED;  
    return newNode;  
}  
  
void rotateLeft(RBTree *tree, Node *x) {  
    Node *y = x->right;  
    x->right = y->left;  
    if (y->left) {  
        y->left->parent = x;  
    }  
    y->parent = x->parent;  
    if (!x->parent) {  
        tree->root = y;  
    } else if (x == x->parent->left) {  
        x->parent->left = y;  
    } else {  
        x->parent->right = y;  
    }  
    y->left = x;  
    x->parent = y;  
}  
  
void rotateRight(RBTree *tree, Node *y) {  
    Node *x = y->left;  
    y->left = x->right;  
    if (x->right) {  
        x->right->parent = y;  
    }  
    x->parent = y->parent;  
    if (!y->parent) {  
        tree->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(RBTree *tree, Node *z) {  
    while (z->parent && z->parent->color == RED) {  
        if (z->parent == z->parent->parent->left) {  
            Node *y = z->parent->parent->right;  
            if (y && 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;  
                    rotateLeft(tree, z);  
                }  
                z->parent->color = BLACK;  
                z->parent->parent->color = RED;  
                rotateRight(tree, z->parent->parent);  
            }  
        } else {  
            Node *y = z->parent->parent->left;  
            if (y && 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;  
                    rotateRight(tree, z);  
                }  
                z->parent->color = BLACK;  
                z->parent->parent->color = RED;  
                rotateLeft(tree, z->parent->parent);  
            }  
        }  
    }  
    tree->root->color = BLACK;  
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值