rbtree search, insert, delete

#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)

时间: 2024-11-09 14:16:08

rbtree search, insert, delete的相关文章

LeetCode 380. Insert Delete GetRandom O(1)

380. Insert Delete GetRandom O(1) Add to List Description Submission Solutions Total Accepted: 21771 Total Submissions: 56175 Difficulty: Medium Contributors: Admin Design a data structure that supports all following operations in average O(1) time.

MySQL5.7 支持一个表有多个INSERT/DELETE/UPDATE触发器

在MySQL5.6版本里,不支持一个表有多个INSERT/DELETE/UPDATE触发器. 例如创建t1表两个INSERT的触发器: DELIMITER $$ USE `test`$$ DROP TRIGGER /*!50032 IF EXISTS */ `t1_1`$$ CREATE     /*!50017 DEFINER = 'admin'@'%' */     TRIGGER `t1_1` AFTER INSERT ON `t1`      FOR EACH ROW BEGIN INS

LeetCode——Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

[LeetCode] Search Insert Position [21]

题目 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]

【leetcode】Search Insert Position

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode: Search Insert Position 解题报告

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

Search Insert Position (LeetCode)

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode Insert Delete GetRandom O(1)

原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1/?tab=Description 题目: Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val)