建立红黑树(左旋、右旋、插入、维护)代码+验证

    关于红黑树的理论讲解,网上有很多,大家可以自己找,这里重点在于实现,代码供大家参考。代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define RED     0
#define BLACK   1
#define NUM     9

typedef struct RBTNode
{
    int key;
    struct RBTNode *parent,*left,*right;
    int color;
}RBTNode,*RBTree;

RBTree nil,root;

void InitLeafNode()
{
    nil =(RBTree)malloc(sizeof(RBTree));
    root=(RBTree)malloc(sizeof(RBTree));
    nil->color      = BLACK;
    nil->left       = NULL;
    nil->right      = NULL;
    nil->key        = -1;
    root            = nil;
}

RBTree CreatNode(int key,int color)
{
    RBTree x;
    x=(RBTNode *)malloc(sizeof(RBTNode));
    x->color        = color;
    x->key          = key;
    x->left         = nil;
    x->right        = nil;
    x->parent       = NULL;
    return x;
}
void LeftRotate(RBTree z)                  //左旋
{
    RBTree y;
    y = z->right;
    z->right = y->left;             //set y
    if(y->left != nil)
        y->left->parent = z;
    y->parent = z->parent;

    if(z->parent == nil)
        root = y;
    else if(z == z->parent->left)
        z->parent->left 	= y;
    else
        z->parent->right	= y;
    //Put z on y's left.
    y->left     = z;
    z->parent   = y;
}
void RightRotate(RBTree z)      //右旋
{
    RBTree y;
    y = z->left;
    z->left = y->right;
    if(y->right != nil)
        y->right->parent = z;
    y->parent = z->parent;
    if(z->parent == nil)
        root = y;
    else if(z == z->parent->left)
        z->parent->left     = y;
    else
        z->parent->right    = y;
    y->right  = z;
    z->parent = y;
}
void TreeInsert(RBTree z)     //插入
{
    RBTree x,y;
    y = nil;
    x = root;
    while(x!=nil){         //Find the insertion position.
        y = x;
        if(z->key<x->key)
            x = x->left;
        else
            x = x->right;
    }
    z->parent = y;
    if(y == nil)
        root = z;
    else{
        if(z->key < y->key)
            y->left  = z;
        else
            y->right = z;
    }
}
void RBTreeInsertFixup(RBTree z)       //维护
{
    RBTree y;
    while(z->parent->color == RED){
        if(z->parent == z->parent->parent->left){
            y = z->parent->parent->right;
            if(y->color == RED){						//case 1.
                z->parent->color         = BLACK;
                z->parent->parent->color = RED;
                y->color                 = BLACK;
                z = z->parent->parent;
            }
            else{
                if(z == z->parent->right){					//case 2.
                    z = z->parent;
                    LeftRotate(z);
                }
                z->parent->color            = BLACK;		           	//case 3.
                z->parent->parent->color    = RED;
                z                           = z->parent->parent;
                RightRotate(z);
            }
        }
        else{                                //symmetry of (case 1,2,3)
            y = z->parent->parent->left;
            if(y->color == RED){
                z->parent->color         =BLACK;
                z->parent->parent->color =RED;
                y->color                 =BLACK;
                z = z->parent->parent;
            }
            else{
                if(z == z->parent->left){
                    z = z->parent;
                    RightRotate(z);
                }
                z->parent->color 		= BLACK;
                z->parent->parent->color 	= RED;
                z                           	= z->parent->parent;
                LeftRotate(z);
            }
        }
    }
    root->color = BLACK;
}
void RBTreeInsert(RBTree z)
{
    TreeInsert(z);
    z->left  = nil;
    z->right = nil;
    //z->color = RED;
    RBTreeInsertFixup(z);
}

int main(void)
{
    RBTree node[NUM];
    int key;
    int i;
    int A[NUM]={8,11,17,15,6,1,22,25,27};
    InitLeafNode();

    for(i=0;i<NUM;i++){
        key=A[i];
        node[i]=CreatNode(key,RED);
        RBTreeInsert(node[i]);
    }

    //验证的时候就是打印每个节点的左孩子和右孩子的key,key为-1代表为叶子节点。 
    printf("The RBT is :\n");
    for(i=0;i<NUM;i++)
        printf("%d + %d\n",node[i]->left->key,node[i]->right->key);

    return 0;
}
vc6.0下验证结果:




以下是一个简单的 C 语言红黑树代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义红黑树节点结构体 typedef struct RBNode { int data; int color; struct RBNode *left; struct RBNode *right; struct RBNode *parent; } RBNode; // 定义红黑树结构体 typedef struct RBTree { RBNode *root; } RBTree; // 定义常量 #define RED 0 #define BLACK 1 // 初始化红黑树 void init(RBTree *tree) { tree->root = NULL; } // 树的左旋操作 void leftRotate(RBTree *tree, RBNode *node) { RBNode *right = node->right; node->right = right->left; if (right->left != NULL) { right->left->parent = node; } right->parent = node->parent; if (node->parent == NULL) { tree->root = right; } else if (node == node->parent->left) { node->parent->left = right; } else { node->parent->right = right; } right->left = node; node->parent = right; } // 树的右旋操作 void rightRotate(RBTree *tree, RBNode *node) { RBNode *left = node->left; node->left = left->right; if (left->right != NULL) { left->right->parent = node; } left->parent = node->parent; if (node->parent == NULL) { tree->root = left; } else if (node == node->parent->right) { node->parent->right = left; } else { node->parent->left = left; } left->right = node; node->parent = left; } // 插入节点 void insert(RBTree *tree, int data) { RBNode *node = (RBNode *)malloc(sizeof(RBNode)); node->data = data; node->color = RED; node->left = NULL; node->right = NULL; node->parent = NULL; RBNode *parent = NULL; RBNode *current = tree->root; while (current != NULL) { parent = current; if (node->data < current->data) { current = current->left; } else { current = current->right; } } node->parent = parent; if (parent == NULL) { tree->root = node; } else if (node->data < parent->data) { parent->left = node; } else { parent->right = node; } while (node != tree->root && node->parent->color == RED) { if (node->parent == node->parent->parent->left) { RBNode *uncle = node->parent->parent->right; if (uncle != NULL && 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; leftRotate(tree, node); } node->parent->color = BLACK; node->parent->parent->color = RED; rightRotate(tree, node->parent->parent); } } else { RBNode *uncle = node->parent->parent->left; if (uncle != NULL && 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; rightRotate(tree, node); } node->parent->color = BLACK; node->parent->parent->color = RED; leftRotate(tree, node->parent->parent); } } } tree->root->color = BLACK; } // 中序遍历 void inorder(RBNode *node) { if (node != NULL) { inorder(node->left); printf("%d ", node->data); inorder(node->right); } } int main() { RBTree tree; init(&tree); insert(&tree, 10); insert(&tree, 20); insert(&tree, 30); insert(&tree, 15); insert(&tree, 18); insert(&tree, 25); insert(&tree, 5); inorder(tree.root); return 0; } ``` 这段代码使用了结构体定义红黑树节点和红黑树,以及一些常量和函数来实现红黑树插入和中序遍历操作。代码中的 `insert` 函数使用了红黑树插入算法来插入新的节点,并维护红黑树的性质。在 `main` 函数中,我们插入了一些数据并进行了中序遍历操作,以验证红黑树的正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值