算法导论代码 第14章 数据结构的扩张
2011-12-30 16:02
417 查看
第14章 数据结构的扩张
14.1 动态顺序统计
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct red_black_tree_type *tree; enum color_enum { color_red, color_black }; struct tree_node { void *key; enum color_enum color; struct tree_node *parent; struct tree_node *left; struct tree_node *right; int size; }; struct red_black_tree_type { int (*comp)(const void*,const void*); struct tree_node *root; struct tree_node *nil; }; void tree_node_ini(struct tree_node *p, void *key) { p->key = key; p->parent = NULL; p->left = NULL; p->right = NULL; p->color = color_black; p->size = 0; } void tree_left_rotate(tree t, struct tree_node *x) { struct tree_node *y = x->right; x->right = y->left; y->left->parent = x; y->parent = x->parent; if (x->parent == t->nil) { t->root = y; } else { if (x == x->parent->left) { x->parent->left = y; } else { x->parent->right = y; } } y->left = x; x->parent = y; y->size = x->size; x->size = x->left->size + x->right->size + 1; } void tree_right_rotate(tree t, struct tree_node *x) { struct tree_node *y = x->left; x->left = y->right; y->right->parent = x; y->parent = x->parent; if (x->parent == t->nil) { t->root = y; } else { if (x == x->parent->left) { x->parent->left = y; } else { x->parent->right = y; } } y->right = x; x->parent = y; y->size = x->size; x->size = x->left->size + x->right->size + 1; } tree tree_create(int (*comp)(const void*,const void*)) { tree t = malloc(sizeof(struct red_black_tree_type)); t->nil = malloc(sizeof(struct tree_node)); t->comp=comp; tree_node_ini(t->nil, NULL); t->root = t->nil; return t; } void tree_destroy_all_node(tree t, struct tree_node *x, void (*free_key) (void *)) { if (x != t->nil) { tree_destroy_all_node(t, x->left, free_key); tree_destroy_all_node(t, x->right, free_key); free_key(x->key); free(x); } } void tree_destroy(tree t, void (*free_key) (void *)) { tree_destroy_all_node(t, t->root, free_key); free(t->nil); free(t); } void tree_inorder_tree_walk(tree t, struct tree_node *x, void (*handle) (const void *)) { if (x != t->nil) { tree_inorder_tree_walk(t, x->left, handle); handle(x->key); tree_inorder_tree_walk(t, x->right, handle); } } struct tree_node *tree_search(tree t, struct tree_node *x, void *key, int (*comp) (const void *, const void *)) { if (x == t->nil || comp(key, x->key) == 0) { return x; } if (comp(key, x->key) < 0) { return tree_search(t, x->left, key, comp); } else { return tree_search(t, x->right, key, comp); } } struct tree_node *tree_minimum(tree t, struct tree_node *x) { while (x != t->nil && x->left != t->nil) { x = x->left; } return x; } struct tree_node *tree_successor(tree t, struct tree_node *x) { if (x->right != t->nil) { return tree_minimum(t, x->right); } struct tree_node *y = x->parent; while (y != t->nil && x == y->right) { x = y; y = y->parent; } return y; } void tree_insert_fixup(tree t, struct tree_node *z) { struct tree_node *y = t->nil; while (z->parent->color == color_red) { if (z->parent == z->parent->parent->left) { y = z->parent->parent->right; //情况1:z的叔叔y是红色的 if (y->color == color_red) { z->parent->color = color_black; y->color = color_black; z->parent->parent->color = color_red; z = z->parent->parent; } else { //情况2:z的叔叔y是黑色的,z是右孩子 if (z == z->parent->right) { z = z->parent; tree_left_rotate(t, z); } //情况3:z的叔叔y是黑色的,z是左孩子 z->parent->color = color_black; z->parent->parent->color = color_red; tree_right_rotate(t, z->parent->parent); } } else { y = z->parent->parent->left; if (y->color == color_red) { z->parent->color = color_black; y->color = color_black; z->parent->parent->color = color_red; z = z->parent->parent; } else { if (z == z->parent->left) { z = z->parent; tree_right_rotate(t, z); } z->parent->color = color_black; z->parent->parent->color = color_red; tree_left_rotate(t, z->parent->parent); } } } t->root->color = color_black; } void tree_insert(tree t, struct tree_node *z) { struct tree_node *y = t->nil; struct tree_node *x = t->root; while (x != t->nil) { y = x; ++y->size; if (t->comp(z->key, x->key) < 0) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == t->nil) { t->root = z; } else { if (t->comp(z->key, y->key) < 0) { y->left = z; } else { y->right = z; } } z->left = t->nil; z->right = t->nil; z->color = color_red; z->size=1; tree_insert_fixup(t, z); } void tree_delete_fixup(tree t, struct tree_node *x) { struct tree_node *w = t->nil; while (x != t->root && x->color == color_black) { if (x == x->parent->left) { w = x->parent->right; //情况1:x的兄弟w是红色的 if (w->color == color_red) { w->color = color_black; x->parent->color = color_red; tree_left_rotate(t, x->parent); w = x->parent->right; } else { //情况2:x的兄弟w是黑色的,而且w的两个孩子都是黑色的 if (w->left->color == color_black && w->right->color == color_black) { w->color = color_red; x = x->parent; } else { //情况3:x的兄弟w是黑色的,w的左孩子是红的,右孩子是黑的 if (w->right->color == color_black) { w->left->color = color_black; w->color = color_red; tree_right_rotate(t, w); w = x->parent->right; } //情况4:x的兄弟w是黑色的,而且w的右孩子是红色的 w->color = x->parent->color; x->parent->color = color_black; w->right->color = color_black; tree_left_rotate(t, x->parent); x = t->root; } } } else { w = x->parent->left; if (w->color == color_red) { w->color = color_black; x->parent->color = color_red; tree_right_rotate(t, x->parent); w = x->parent->left; } else { if (w->right->color == color_black && w->left->color == color_black) { w->color = color_red; x = x->parent; } else { if (w->left->color == color_black) { w->right->color = color_black; w->color = color_red; tree_left_rotate(t, w); w = x->parent->left; } w->color = x->parent->color; x->parent->color = color_black; w->left->color = color_black; tree_right_rotate(t, x->parent); x = t->root; } } } } x->color = color_black; } void swap(void *a, void *b, size_t elem_size) { if (a == NULL || b == NULL || a == b) return; char temp[elem_size]; /*变长数组 */ memcpy(temp, a, elem_size); memcpy(a, b, elem_size); memcpy(b, temp, elem_size); } struct tree_node *tree_delete(tree t, struct tree_node *z) { struct tree_node *y; struct tree_node *x; if (z->left == t->nil || z->right == t->nil) { y = z; } else { y = tree_successor(t, z); } if (y->left != t->nil) { x = y->left; } else { x = y->right; } x->parent = y->parent; if (y->parent == t->nil) { t->root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { /*要删除的结点y是z的后继,交换z和y结点的内容 */ swap(&z->key, &y->key, sizeof(void *)); } struct tree_node *p = y; do { p = p->parent; --p->size; } while (p != t->root); /*更新size */ if (y->color == color_black) { tree_delete_fixup(t, x); } return y; } /*检索具有给定顺序的元素*/ struct tree_node *tree_select(struct tree_node *x, int i) { int r = x->left->size + 1; if (i == r) { return x; } else { if (i < r) { return tree_select(x->left, i); } else { return tree_select(x->right, i - r); } } } /*确定一个元素的秩*/ int tree_rank(tree t, struct tree_node *x) { int r = x->left->size + 1; struct tree_node *y = x; while (y != t->root) { if (y == y->parent->right) { r = r + y->parent->left->size + 1; } y = y->parent; } return r; } void print_key(const void *key) { const int *p = key; printf("%d ", *p); } int cmp_int(const void *p1, const void *p2) { const int *pa = p1; const int *pb = p2; if (*pa < *pb) return -1; if (*pa == *pb) return 0; return 1; } int main() { tree t = tree_create(cmp_int); for (int i = 0; i < 10; i++) { struct tree_node *node = malloc(sizeof(struct tree_node)); int *ip = malloc(sizeof(int)); *ip = i; tree_node_ini(node, ip); tree_insert(t, node); } printf("中序遍历结果:\n"); tree_inorder_tree_walk(t, t->root, print_key); printf("\n"); int rank = tree_rank(t, t->root); printf("根结点:%d,排名:%d,Size:%d\n", *(int *)t->root->key, rank, t->root->size); rank = 6; struct tree_node *node = tree_select(t->root, rank); printf("第%d个元素是:%d\n", rank, *(int *)node->key); printf("删掉第%d个元素的结果是:\n", rank); struct tree_node *del_node = tree_delete(t, node); free(del_node->key); free(del_node); tree_inorder_tree_walk(t, t->root, print_key); printf("\n"); rank = tree_rank(t, t->root); printf("删除后,根结点:%d,排名:%d,Size:%d\n", *(int *)t->root->key, rank, t->root->size); /*遍历树,释放结点的key */ tree_destroy(t, free); return 0; }
14.3 区间树
#include <stdio.h> #include <time.h> #include <stdbool.h> #include <string.h> #include <stdlib.h> #define MAX(a,b) (((a)>(b))?(a):(b)) typedef struct red_black_tree_type *tree; enum color_enum { color_red, color_black }; struct interval { int low; int hight; }; struct tree_node { struct interval it; enum color_enum color; struct tree_node *parent; struct tree_node *left; struct tree_node *right; int interval_max; }; struct red_black_tree_type { struct tree_node *root; struct tree_node *nil; }; void interval_ini(struct interval *it, int low, int hight) { it->low = low; it->hight = hight; } bool interval_is_overlap(struct interval *it_a, struct interval *it_b) { if (it_a->low <= it_b->hight && it_b->low <= it_a->hight) { return true; } else { return false; } } void tree_node_ini(struct tree_node *p, struct interval *it) { p->it = *it; p->interval_max = it->hight; p->parent = NULL; p->left = NULL; p->right = NULL; p->color = color_black; } void tree_left_rotate(tree t, struct tree_node *x) { struct tree_node *y = x->right; x->right = y->left; y->left->parent = x; y->parent = x->parent; if (x->parent == t->nil) { t->root = y; } else { if (x == x->parent->left) { x->parent->left = y; } else { x->parent->right = y; } } y->left = x; x->parent = y; y->interval_max = x->interval_max; int max = MAX(x->left->interval_max, x->right->interval_max); x->interval_max = MAX(x->it.hight, max); } void tree_right_rotate(tree t, struct tree_node *x) { struct tree_node *y = x->left; x->left = y->right; y->right->parent = x; y->parent = x->parent; if (x->parent == t->nil) { t->root = y; } else { if (x == x->parent->left) { x->parent->left = y; } else { x->parent->right = y; } } y->right = x; x->parent = y; y->interval_max = x->interval_max; int max = MAX(x->left->interval_max, x->right->interval_max); x->interval_max = MAX(x->it.hight, max); } tree tree_create() { tree t = malloc(sizeof(struct red_black_tree_type)); t->nil = malloc(sizeof(struct tree_node)); tree_node_ini(t->nil, &(struct interval) { 0, 0}); t->root = t->nil; return t; } void tree_delete_node(tree t, struct tree_node *x) { if (x != t->nil) { tree_delete_node(t, x->left); tree_delete_node(t, x->right); free(x); } } void tree_destroy(tree t) { tree_delete_node(t, t->root); free(t->nil); free(t); } void tree_inorder_tree_walk(tree t, struct tree_node *x, void (*handle) (struct interval *)) { if (x != t->nil) { tree_inorder_tree_walk(t, x->left, handle); handle(&x->it); tree_inorder_tree_walk(t, x->right, handle); } } struct tree_node *tree_interval_search(tree t, struct interval *it) { struct tree_node *x = t->root; while (x != t->nil && !interval_is_overlap(&x->it, it)) { if (x->left != t->nil && x->left->interval_max >= it->low) { x = x->left; } else { x = x->right; } } return x; } struct tree_node *tree_minimum(tree t, struct tree_node *x) { while (x != t->nil && x->left != t->nil) { x = x->left; } return x; } struct tree_node *tree_successor(tree t, struct tree_node *x) { if (x->right != t->nil) { return tree_minimum(t, x->right); } struct tree_node *y = x->parent; while (y != t->nil && x == y->right) { x = y; y = y->parent; } return y; } void tree_insert_fixup(tree t, struct tree_node *z) { struct tree_node *y = t->nil; while (z->parent->color == color_red) { if (z->parent == z->parent->parent->left) { y = z->parent->parent->right; //情况1:z的叔叔y是红色的 if (y->color == color_red) { z->parent->color = color_black; y->color = color_black; z->parent->parent->color = color_red; z = z->parent->parent; } else { //情况2:z的叔叔y是黑色的,z是右孩子 if (z == z->parent->right) { z = z->parent; tree_left_rotate(t, z); } //情况3:z的叔叔y是黑色的,z是左孩子 z->parent->color = color_black; z->parent->parent->color = color_red; tree_right_rotate(t, z->parent->parent); } } else { y = z->parent->parent->left; if (y->color == color_red) { z->parent->color = color_black; y->color = color_black; z->parent->parent->color = color_red; z = z->parent->parent; } else { if (z == z->parent->left) { z = z->parent; tree_right_rotate(t, z); } z->parent->color = color_black; z->parent->parent->color = color_red; tree_left_rotate(t, z->parent->parent); } } } t->root->color = color_black; } void tree_insert(tree t, struct tree_node *z) { struct tree_node *y = t->nil; struct tree_node *x = t->root; while (x != t->nil) { y = x; y->interval_max = MAX(y->interval_max, z->interval_max); if (z->it.low < x->it.low) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == t->nil) { t->root = z; } else { if (z->it.low < y->it.low) { y->left = z; } else { y->right = z; } } z->left = t->nil; z->right = t->nil; z->color = color_red; tree_insert_fixup(t, z); } void tree_delete_fixup(tree t, struct tree_node *x) { struct tree_node *w = t->nil; while (x != t->root && x->color == color_black) { if (x == x->parent->left) { w = x->parent->right; //情况1:x的兄弟w是红色的 if (w->color == color_red) { w->color = color_black; x->parent->color = color_red; tree_left_rotate(t, x->parent); w = x->parent->right; } else { //情况2:x的兄弟w是黑色的,而且w的两个孩子都是黑色的 if (w->left->color == color_black && w->right->color == color_black) { w->color = color_red; x = x->parent; } else { //情况3:x的兄弟w是黑色的,w的左孩子是红的,右孩子是黑的 if (w->right->color == color_black) { w->left->color = color_black; w->color = color_red; tree_right_rotate(t, w); w = x->parent->right; } //情况4:x的兄弟w是黑色的,而且w的右孩子是红色的 w->color = x->parent->color; x->parent->color = color_black; w->right->color = color_black; tree_left_rotate(t, x->parent); x = t->root; } } } else { w = x->parent->left; if (w->color == color_red) { w->color = color_black; x->parent->color = color_red; tree_right_rotate(t, x->parent); w = x->parent->left; } else { if (w->right->color == color_black && w->left->color == color_black) { w->color = color_red; x = x->parent; } else { if (w->left->color == color_black) { w->right->color = color_black; w->color = color_red; tree_left_rotate(t, w); w = x->parent->left; } w->color = x->parent->color; x->parent->color = color_black; w->left->color = color_black; tree_right_rotate(t, x->parent); x = t->root; } } } } x->color = color_black; } void swap(void *a, void *b, size_t elem_size) { if (a == NULL || b == NULL || a == b) return; char temp[elem_size]; /*变长数组 */ memcpy(temp, a, elem_size); memcpy(a, b, elem_size); memcpy(b, temp, elem_size); } struct tree_node *tree_delete(tree t, struct tree_node *z) { struct tree_node *y; struct tree_node *x; if (z->left == t->nil || z->right == t->nil) { y = z; } else { y = tree_successor(t, z); } if (y->left != t->nil) { x = y->left; } else { x = y->right; } x->parent = y->parent; if (y->parent == t->nil) { t->root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { /*要删除的结点y是z的后继,交换z和y结点的内容 */ swap(&z->it, &y->it, sizeof(struct interval)); } struct tree_node *p = y; do { p = p->parent; int max = MAX(p->left->interval_max, p->right->interval_max); p->interval_max = MAX(p->it.hight, max); } while (p != t->root); /*更新interval_max */ if (y->color == color_black) { tree_delete_fixup(t, x); } return y; } void print_interval(struct interval *it) { printf("(%d,%d)", it->low, it->hight); } int main() { srand((unsigned)time(NULL)); tree t = tree_create(); for (int i = 0; i < 10; i++) { struct tree_node *node = malloc(sizeof(struct tree_node)); struct interval it; interval_ini(&it, i, i + rand() % 10); tree_node_ini(node, &it); tree_insert(t, node); } printf("中序遍历结果:\n"); tree_inorder_tree_walk(t, t->root, print_interval); printf("\n"); printf("根结点区间:(%d,%d),区间最大值:%d\n", t->root->it.low, t->root->it.hight, t->root->interval_max); struct interval it = { 10, 15 }; printf("查找与区间:(%d,%d)重叠的区间:\n", it.low, it.hight); struct tree_node *result = tree_interval_search(t, &it); printf("查找的结果:%s\n", result != t->nil ? "成功" : "失败"); if (result != t->nil) { printf("区间是:(%d,%d)\n", result->it.low, result->it.hight); printf("删除区间:(%d,%d)\n", result->it.low, result->it.hight); struct tree_node *del_node = tree_delete(t, result); free(del_node); printf("中序遍历结果:\n"); tree_inorder_tree_walk(t, t->root, print_interval); printf("\n"); printf("删掉后,根结点区间:(%d,%d),区间最大值:%d\n", t->root->it.low, t->root->it.hight, t->root->interval_max); } /*遍历树,释放结点的key */ tree_destroy(t); return 0; }
相关文章推荐
- 算法导论 ch14 数据结构的扩张 区间树
- 算法导论 14章 数据结构的扩张(二) 区间树
- 算法导论--第十四章【数据结构的扩张】14.1
- 算法导论学习笔记-第十四章-数据结构的扩张
- 算法导论 第14章 数据结构的扩张(三) 14.3-6 最小差幅树
- 【算法导论】第十一课 扩充的数据结构、动态有序统计和区间树
- 算法导论第10章基本数据结构10.1栈
- 严蔚敏版数据结构课本代码——算法2.2
- 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 数据结构C语言实现之链式队列的6种算法代码
- 经典面试题(一)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 经典面试题(二)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 程序员代码面试指南:IT名企算法与数据结构题目最优解
- 程序员代码面试指南:IT名企算法与数据结构题目最优解(题目)
- 有根树的表示_第10章_基本数据结构_算法导论
- 经典面试题(四)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 算法导论-第14章-数据结构的扩张
- 算法导论 python代码 第四章
- 算法与数据结构-二叉树 讲解与java代码实现
- 算法导论——第七章——基本数据结构