Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12927
  • 博文数量: 16
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-08 11:44
文章分类
文章存档

2014年(16)

我的朋友
最近访客

分类: C/C++

2014-09-09 12:31:28


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <assert.h>
  4. #include <stdlib.h>

  5. struct node {
  6.         int key;
  7.         int degree;
  8.         struct node *child;
  9.         struct node *prev;
  10.         struct node *next;
  11. };

  12. void init_node (struct node *node, int key)
  13. {
  14.         node->key = key;
  15.         node->degree = 0;
  16.         node->child = NULL;
  17.         node->prev = node;
  18.         node->next = node;
  19. }
  20. struct node *merge(struct node *t1, struct node *t2)
  21. {
  22.         if (!t1)
  23.                 return t2;
  24.         if (!t2)
  25.                 return t1;

  26.         struct node *tail = NULL;
  27.         tail = t1->prev;
  28.         t2->prev->next = t1;
  29.         t1->prev = t2->prev;

  30.         t2->prev = tail;
  31.         tail->next = t2;

  32.         if (t1->key < t2->key)
  33.                 return t1;
  34.         else
  35.                 return t2;
  36. }
  37. void insert(struct node **tree, int key)
  38. {
  39.         struct node *now = malloc(sizeof(struct node));
  40.         assert(now);
  41.         init_node(now, key);
  42.         *tree = merge(*tree, now);
  43. }
  44. struct node *join(struct node *t1, struct node *t2)
  45. {
  46.         if (!t1)
  47.                 return t2;
  48.         if (!t2)
  49.                 return t1;
  50.         if (t1->key > t2->key) {
  51.                 struct node *tmp = t1;
  52.                 t1 = t2;
  53.                 t2 = tmp;
  54.         }

  55.         t1->degree ++;
  56.         struct node *child = t1->child;
  57.         if (!child) {
  58.                 t2->prev = t2->next = t2;
  59.                 t1->child = t2;
  60.         } else {
  61.                 struct node *next = child->next;
  62.                 child->next = t2;
  63.                 t2->prev = child;
  64.                 t2->next = next;
  65.                 next->prev = t2;
  66.         }
  67.         return t1;
  68. }
  69. struct node *adjust(struct node *node)
  70. {
  71.         if (node == NULL)
  72.                 return NULL;

  73.         struct node *arr[32] = {NULL};
  74.         struct node *head = node;
  75.         do {
  76.                 struct node *tmp = head;
  77.                 head = head->next;
  78.                 int degree = tmp->degree;
  79.                 while (arr[degree] != NULL) {
  80.                         tmp = join(tmp, arr[degree]);
  81.                         arr[degree] = NULL;
  82.                         degree ++;
  83.                 }
  84.                 arr[degree] = tmp;
  85.         } while (head != node);
  86. int min = INT_MAX;
  87.         struct node *ret = NULL;

  88.         head = NULL;
  89.         for (int i = 0; i<32; i++) {
  90.                 if (arr[i]) {
  91.                         if (arr[i]->key < min) {
  92.                                 ret = arr[i];
  93.                                 min = ret->key;
  94.                         }
  95.                         if (head == NULL) {
  96.                                 head = arr[i];
  97.                                 head->next = head;
  98.                                 head->prev = head;
  99.                         } else {
  100.                                 struct node *next = head->next;

  101.                                 head->next = arr[i];
  102.                                 arr[i]->prev = head;
  103.                                 arr[i]->next = next;
  104.                                 next->prev = arr[i];
  105.                         }
  106.                 }
  107.         }

  108.         return ret;
  109. }
  110. int delmin(struct node **tree)
  111. {
  112.         if (*tree == NULL) {
  113.                 fprintf(stderr, "the tree is empty");
  114.                 exit(1);
  115.         }
  116.         struct node *tmp = *tree;
  117.         struct node *child = tmp->child;
  118.         struct node *next = NULL;
  119.         int ret = tmp->key;
  120.         if (tmp->next == tmp) {
  121.                 next = NULL;
  122.         } else {
  123.                 tmp->prev->next = tmp->next;
  124.                 tmp->next->prev = tmp->prev;
  125.                 next = tmp->next;

  126.         }
  127.         free (tmp);
  128.          //这里并不需要child和next是各个链表中是最小的
  129.         tmp = merge(child, next);
  130.         *tree = adjust(tmp);
  131.         return ret;
  132. }

阅读(211) | 评论(0) | 转发(0) |
0

上一篇:置换选择

下一篇:区间堆

给主人留下些什么吧!~~