码迷,mamicode.com
首页 > 其他好文 > 详细

rbtree search, insert, delete

时间:2017-04-23 16:55:54      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:eof   srand   char   node   nts   int   i++   eve   htm   

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <assert.h>
#include <string.h>

enum {
    BLACK,
    RED
};
enum {
    LEFT,
    RIGHT
};
typedef struct node_s {
    int data;
    int color;
    struct node_s *left, *right;
} node_t, *p_node_t;

int data[16] = {11, 18, 15, 17, 19, 12, 5, 3, 4, 8, 7, 6, 2, 9, 1, 10};

p_node_t null = NULL;
p_node_t create_node(int data, p_node_t left, p_node_t right, int color)
{
    p_node_t node = (p_node_t)malloc(sizeof(node_t));
    assert(node);
    memset(node, 0, sizeof(node_t));
    node->data = data;
    node->color = color;
    node->left = node->right = null;
    printf("node: %p, data: %d, color: %d\n", node, node->data, node->color);
    return node;
}
p_node_t rotate_left(p_node_t root)
{
    p_node_t temp = root->right;
    root->right = temp->left;
    temp->left = root;
    return temp;
}
p_node_t rotate_right(p_node_t root)
{
    p_node_t temp = root->left;
    root->left = temp->right;
    temp->right = root;
    return temp;
}

p_node_t insert_node(p_node_t root, int data, int dir)
{
    if (root == null)
        return create_node(data, null, null, RED);
    if (root->left->color == RED && root->right->color == RED) {
        root->left->color = BLACK;
        root->right->color = BLACK;
        root->color = RED;
    }
    if (data < root->data) {
        root->left = insert_node(root->left, data, LEFT);
        if (root->color == RED && root->left->color == RED && dir == RIGHT) {
            root = rotate_right(root);
        }
        if (root->left->color == RED && root->left->left->color == RED) {
            root = rotate_right(root);
            root->color = BLACK;
            root->right->color = RED;
        }
    } else if (data > root->data) {
        root->right = insert_node(root->right, data, RIGHT);
        if (root->color == RED && root->right->color == RED && dir == LEFT) {
            root = rotate_left(root);
        }
        if (root->right->color == RED && root->right->right->color == RED) {
            root = rotate_left(root);
            root->color = BLACK;
            root->left->color = RED;
        }
    } else {
        return root;
    }
    return root;
}

enum { kWidth = 6 };
void PrintSpace(int n)
{
    for (int i = 0; i < n; ++i)
        printf("-");
}
void print_rbtree(p_node_t root, int level)
{
        if (root == NULL) return;
        print_rbtree(root->right, level + 1);
        PrintSpace(level * kWidth);
        printf("%d%c\n", root->data, (root->color == RED) ? ‘r‘:‘b‘);
        print_rbtree(root->left, level + 1);

}

p_node_t create_rbtree()
{
    int i = 0;
    null = create_node(0, NULL, NULL, 0);
    p_node_t root = null;
    for (i=0; i<16; i++) {
        root = insert_node(root, data[i], LEFT);
    }
    root->color = BLACK;
    return root;
}
int main(int argc, char **argv)
{
    int i = 0;
    p_node_t root = NULL;
    srand(time(NULL));
    root = create_rbtree();
    print_rbtree(root, 1);
}

 

reference:

http://v.youku.com/v_show/id_XMjE4MjMzNjA4.html?spm=a2hzp.8253869.0.0 (1)

http://v.youku.com/v_show/id_XMjE4MjM5MDcy.html?spm=a2hzp.8253869.0.0 (2)

http://v.youku.com/v_show/id_XMjE4MjQwMTQ4.html?spm=a2hzp.8253869.0.0 (3)

rbtree search, insert, delete

标签:eof   srand   char   node   nts   int   i++   eve   htm   

原文地址:http://www.cnblogs.com/roadmap99/p/6752772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!