C语言红黑树

#include <stdio.h>
#include <stdlib.h>

// 定义红黑树节点的结构
typedef struct Node {
    int data;
    char color; // 'R' for red, 'B' for black
    struct Node *parent;
    struct Node *left;
    struct Node *right;
} Node;

// 定义红黑树的结构
typedef struct {
    Node *root;
    Node *nil; // 一个哨兵节点,代表空节点
} RedBlackTree;

// 函数声明
void leftRotate(RedBlackTree *tree, Node *x);
void rightRotate(RedBlackTree *tree, Node *y);
void insertFixup(RedBlackTree *tree, Node *z);
void insertNode(RedBlackTree *tree, int key);
void printTree(Node *root);

// 创建一个新的红黑树节点
Node *createNode(int key) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    if (newNode != NULL) {
        newNode->data = key;
        newNode->color = 'R'; // 新插入的节点默认为红色
        newNode->parent = NULL;
        newNode->left = NULL;
        newNode->right = NULL;
    }
    return newNode;
}

// 初始化红黑树
void initializeRedBlackTree(RedBlackTree *tree) {
    tree->nil = createNode(0);
    tree->nil->color = 'B'; // 哨兵节点为黑色
    tree->root = tree->nil;
}

// 主函数
int main() {
    RedBlackTree myTree;
    initializeRedBlackTree(&myTree);

    // 插入一些数据
    insertNode(&myTree, 10);
    insertNode(&myTree, 5);
    insertNode(&myTree, 20);
    insertNode(&myTree, 3);
    insertNode(&myTree, 8);
    insertNode(&myTree, 15);
    insertNode(&myTree, 25);

    // 打印红黑树
    printf("Red-Black Tree:\n");
    printTree(myTree.root);

    return 0;
}

// 左旋操作
void leftRotate(RedBlackTree *tree, Node *x) {
    Node *y = x->right;
    x->right = y->left;

    if (y->left != tree->nil) {
        y->left->parent = x;
    }

    y->parent = x->parent;

    if (x->parent == tree->nil) {
        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 rightRotate(RedBlackTree *tree, Node *y) {
    Node *x = y->left;
    y->left = x->right;

    if (x->right != tree->nil) {
        x->right->parent = y;
    }

    x->parent = y->parent;

    if (y->parent == tree->nil) {
        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 insertFixup(RedBlackTree *tree, Node *z) {
    while (z->parent->color == 'R') {
        if (z->parent == z->parent->parent->left) {
            Node *y = z->parent->parent->right;
            if (y->color == 'R') {
                z->parent->color = 'B';
                y->color = 'B';
                z->parent->parent->color = 'R';
                z = z->parent->parent;
            } else {
                if (z == z->parent->right) {
                    z = z->parent;
                    leftRotate(tree, z);
                }
                z->parent->color = 'B';
                z->parent->parent->color = 'R';
                rightRotate(tree, z->parent->parent);
            }
        } else {
            // 对称情况
            Node *y = z->parent->parent->left;
            if (y->color == 'R') {
                z->parent->color = 'B';
                y->color = 'B';
                z->parent->parent->color = 'R';
                z = z->parent->parent;
            } else {
                if (z == z->parent->left) {
                    z = z->parent;
                    rightRotate(tree, z);
                }
                z->parent->color = 'B';
                z->parent->parent->color = 'R';
                leftRotate(tree, z->parent->parent);
            }
        }
    }
    tree->root->color = 'B'; // 根节点始终是黑色
}

// 插入节点
void insertNode(RedBlackTree *tree, int key) {
    Node *z = createNode(key);
    Node *y = tree->nil;
    Node *x = tree->root;

    while (x != tree->nil) {
        y = x;
        if (z->data < x->data) {
            x = x->left;
        } else {
            x = x->right;
        }
    }

    z->parent = y;

    if (y == tree->nil) {
        tree->root = z; // 树为空,新节点成为根节点
    } else if (z->data < y->data) {
        y->left = z;
    } else {
        y->right = z;
    }

    z->left = tree->nil;
    z->right = tree->nil;
    z->color = 'R'; // 新插入的节点默认为红色

    // 插入修正操作
    insertFixup(tree, z);
}

// 中序遍历打印红黑树
void printTree(Node *root) {
    if (root != NULL && root->color != 'B') {
        printf("(");
    }

    if (root != NULL) {
        printTree(root->left);
        printf(" %d%c ", root->data, root->color);
        printTree(root->right);
    }

    if (root != NULL && root->color != 'B') {
        printf(")");
    }
}
 

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值