rbtree search, insert, delete
Posted roadmap
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
以上是关于rbtree search, insert, delete的主要内容,如果未能解决你的问题,请参考以下文章
[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST