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

2014年(16)

我的朋友
最近访客

分类: C/C++

2014-05-22 10:32:00


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. /***factor可以和parent用一个元素存放***/
  5. struct avl_node {
  6.         int key;
  7.         int factor;
  8.         struct avl_node *parent;
  9.         struct avl_node *lchild;
  10.         struct avl_node *rchild;
  11. };
  12. struct avl_head {
  13.         struct avl_node *head;
  14. };
  15. #define LH 2
  16. #define RH 1
  17. #define EH 0
  18. #define MASK 3
  19. void init_head(struct avl_head *head)
  20. {
  21.         head->head = NULL;
  22. }
  23. void init_node(struct avl_node *node, int key)
  24. {
  25.         node->key = key;
  26.         node->parent = node->rchild = node->lchild = NULL;
  27. }

  28. struct avl_node *get_parent(struct avl_node *node)
  29. {
  30.         if (node)
  31.                 return node->parent;
  32.         else
  33.                 return NULL;
  34. }
  35. int get_factor(struct avl_node *node)
  36. {
  37.         int factor = 0;
  38.         if (node)
  39.                 factor = node->factor;
  40.         return factor;
  41. }
  42. void set_parent_factor(struct avl_node *now, struct avl_node *parent, int factor)
  43. {
  44.         if (now) {
  45.                 now->parent = parent;
  46.                 if (factor != -1)
  47.                         now->factor = factor;
  48.         }

  49. }
  50. struct avl_node *ll_rotate(struct avl_node *p)
  51. {
  52.         struct avl_node *tmp, *parent;
  53.         int factor;

  54.         parent = get_parent(p);

  55.         tmp = p->lchild;
  56.         p->lchild = tmp->rchild;
  57.         tmp->rchild = p;

  58.         factor = get_factor(tmp);
  59.         set_parent_factor(p->lchild, p, -1);

  60.         if (LH == factor) {
  61.                 set_parent_factor(p, tmp, EH);
  62.                 set_parent_factor(tmp, parent, EH);
  63.         } else {
  64.                 set_parent_factor(p, tmp, LH);
  65.                 set_parent_factor(tmp, parent, RH);
  66.         }

  67.         return tmp;
  68. }
  69. struct avl_node *rr_rotate(struct avl_node *p)
  70. {
  71.         struct avl_node *tmp, *parent;
  72.         int factor;

  73.         parent = get_parent(p);

  74.         tmp = p->rchild;
  75.         p->rchild = tmp->lchild;
  76.         tmp->lchild = p;

  77.         factor = get_factor(tmp);
  78.         set_parent_factor(p->rchild, p, -1);

  79.         if (RH == factor) {
  80.                 set_parent_factor(p, tmp, EH);
  81.                 set_parent_factor(tmp, parent, EH);
  82.         } else {
  83.                 set_parent_factor(p, tmp, RH);
  84.                 set_parent_factor(tmp, parent, LH);
  85.         }
  86.         return tmp;
  87. }

  88. struct avl_node *lr_rotate(struct avl_node *p)
  89. {
  90.         struct avl_node *tmp, *parent, *rc;
  91.         int factor;

  92.         parent = get_parent(p);

  93.         tmp = p->lchild;
  94.         rc = tmp->rchild;
  95.         tmp->rchild = rc->lchild;
  96.         p->lchild = rc->rchild;
  97.         rc->lchild = tmp;
  98.         rc->rchild = p;

  99.         factor = get_factor(rc);
  100.         set_parent_factor(rc, parent, EH);
  101.         set_parent_factor(tmp->rchild, tmp, -1);
  102.         set_parent_factor(p->lchild, p, -1);

  103.         if (EH == factor) {
  104.                 set_parent_factor(tmp, rc, EH);
  105.                 set_parent_factor(p, rc, EH);
  106.         } else if(LH == factor) {
  107.                 set_parent_factor(tmp, rc, EH);
  108.                 set_parent_factor(p, rc, RH);
  109.         } else {
  110.                 set_parent_factor(tmp, rc, LH);
  111.                 set_parent_factor(p, rc, EH);
  112.         }

  113.         return rc;
  114. }
  115. struct avl_node *rl_rotate(struct avl_node *p)
  116. {
  117.         struct avl_node *tmp, *parent, *rc;
  118.         int factor;

  119.         parent = get_parent(p);

  120.         tmp = p->rchild;
  121.         rc = tmp->lchild;
  122.         p->rchild = rc->lchild;
  123.         tmp->lchild = rc->rchild;
  124.         rc->lchild = p;
  125.         rc->rchild = tmp;

  126.         factor = get_factor(rc);
  127.         set_parent_factor(rc, parent, EH);
  128.         set_parent_factor(p->rchild, p, -1);
  129.         set_parent_factor(tmp->lchild, tmp, -1);

  130.         if (EH == factor) {
  131.                 set_parent_factor(tmp, rc, EH);
  132.                 set_parent_factor(p, rc, EH);
  133.         } else if (LH == factor) {
  134.                 set_parent_factor(p, rc, EH);
  135.                 set_parent_factor(tmp, rc, RH);
  136.         } else {
  137.                 set_parent_factor(p, rc, LH);
  138.                 set_parent_factor(tmp, rc, EH);
  139.         }

  140.         return rc;
  141. }
  142. void avl_new_link(struct avl_node *new, struct avl_node *parent, struct avl_node **which)
  143. {
  144.         new->parent = parent;
  145.         new->lchild = new->rchild = NULL;
  146.         new->factor = EH;
  147.         *which = new;
  148. }
  149. void avl_change_link(struct avl_node *new, struct avl_node *old, \
  150.                 struct avl_node *parent, struct avl_head *root)
  151. {
  152.         if (parent) {
  153.                 if (old == parent->lchild)
  154.                         parent->lchild = new;
  155.                 else
  156.                         parent->rchild = new;
  157.         } else
  158.                 root->head = new;
  159. }

  160. void avl_insert(struct avl_head *root, struct avl_node *node)
  161. {
  162.         struct avl_node *parent, *gp;

  163.         parent = get_parent(node);

  164.         while (true) {
  165.                 int factor;
  166.                 struct avl_node *tmp;

  167.                 if (!parent)
  168.                         break;
  169.                 gp = get_parent(parent);
  170.                 factor = get_factor(parent);

  171.                 if (node == parent->lchild) {
  172.                         if (EH == factor) {
  173.                                 set_parent_factor(parent, gp, LH);
  174.                                 node = parent;
  175.                                 parent = gp;
  176.                                 continue;
  177.                         } else if (LH == factor) {
  178.                                 int f;
  179.                                 f = get_factor(node);
  180.                                 if (LH == f)
  181.                                         node = ll_rotate(parent);
  182.                                 else
  183.                                         node = lr_rotate(parent);

  184.                                 avl_change_link(node, parent, gp, root);
  185.                                 break;
  186.                         } else {
  187.                                 set_parent_factor(parent, gp, EH);
  188.                                 break;
  189.                         }
  190.   } else {
  191.                         if (EH == factor) {
  192.                                 set_parent_factor(parent, gp, RH);
  193.                                 node = parent;
  194.                                 parent = gp;
  195.                                 continue;
  196.                         } else if (LH == factor) {
  197.                                 set_parent_factor(parent, gp, EH);
  198.                                 break;
  199.                         } else {
  200.                                 int f;
  201.                                 f = get_factor(node);
  202.                                 if (LH == f)
  203.                                         node = rl_rotate(parent);
  204.                                 else
  205.                                         node = rr_rotate(parent);

  206.                                 avl_change_link(node, parent, gp, root);
  207.                                 break;
  208.                         }
  209.                 }
  210.         }
  211. }
  212. void do_del(struct avl_head *root, struct avl_node *node)
  213. {
  214.         struct avl_node *parent, *gp;
  215.         int factor;

  216.         parent = get_parent(node);
  217.         while(true) {
  218.                 if (!parent)
  219.                         break;
  220.                 gp = get_parent(parent);
  221.                 factor = get_factor(parent);

  222.                 if (node == parent->lchild) {
  223.                         if (EH == factor) {
  224.                                 set_parent_factor(parent, gp, RH);
  225.                                 break;
  226.                         } else if (LH == factor) {
  227.                                 set_parent_factor(parent, gp, EH);
  228.                                 node = parent;
  229.                                 parent = gp;
  230.                                 continue;
  231.                         } else {
  232.                                 int f;
  233.                                 f = get_factor(parent->rchild);
  234.                                 if (RH == f) {
  235.                                         node = rr_rotate(parent);
  236.                                         avl_change_link(node, parent, gp, root);
  237.                                         parent = gp;
  238.                                         continue;
  239.                                 } else if (EH == f) {
  240.                                         node = rr_rotate(parent);
  241.                                         avl_change_link(node, parent, gp, root);
  242.                                         break;
  243.                                 } else {
  244.                                         node = rl_rotate(parent);
  245.                                         avl_change_link(node, parent, gp, root);
  246.                                         parent = gp;
  247.                                         continue;
  248.                                 }
  249.                         }
  250.   } else {
  251.                         if (EH == factor) {
  252.                                 set_parent_factor(parent, gp, LH);
  253.                                 break;
  254.                         } else if (RH == factor) {
  255.                                 set_parent_factor(parent, gp, EH);
  256.                                 node = parent;
  257.                                 parent = gp;
  258.                                 continue;
  259.                         } else {
  260.                                 int f;
  261.                                 f = get_factor(parent->lchild);
  262.                                 if (LH == f) {
  263.                                         node = ll_rotate(parent);
  264.                                         avl_change_link(node, parent, gp, root);
  265.                                         parent = gp;
  266.                                         continue;
  267.                                 } else if (EH == f) {
  268.                                         node == ll_rotate(parent);
  269.                                         avl_change_link(node, parent, gp, root);
  270.                                         break;
  271.                                 } else {
  272.                                         node = lr_rotate(parent);
  273.                                         avl_change_link(node, parent, gp, root);
  274.                                         parent = gp;
  275.                                         continue;
  276.                                 }
  277.                         }
  278.                 }
  279.         }
  280. }
  281. void avl_del(struct avl_head *root, struct avl_node *node)
  282. {
  283.         struct avl_node *victim, *parent, *child;
  284.         struct avl_node tmp;

  285.         victim = node;
  286.         if (node->lchild && node->rchild) {
  287.                 victim = node->lchild;
  288.                 while (victim->rchild)
  289.                         victim = victim->rchild;
  290.         }

  291.         if (victim->lchild)
  292.                 child = victim->lchild;
  293.         else
  294.                 child = victim->rchild;

  295.         do_del(root, victim);

  296.         parent = get_parent(victim);
  297.         set_parent_factor(child, parent, -1);
  298.         avl_change_link(child, victim, parent, root);

  299.         if (victim != node) {
  300.                 *victim = *node;
  301.                 parent = get_parent(victim);
  302.                 set_parent_factor(victim->lchild, victim, -1);
  303.                 set_parent_factor(victim->rchild, victim, -1);
  304.                 avl_change_link(victim, node, parent, root);
  305.         }
  306. }
  307. void insert(struct avl_head *root, int key)
  308. {
  309.         struct avl_node *parent, **which, *node;


  310.         which = &root->head;
  311.         parent = *which;
  312.         while(*which) {
  313.                 parent = *which;
  314.                 if (key < (*which)->key)
  315.                         which = &(*which)->lchild;
  316.                 else if (key > (*which)->key)
  317.                         which = &(*which)->rchild;
  318.                 else
  319.                         break;
  320.         }
  321.         if (*which)
  322.                 fprintf(stderr, "insert: key been bound\n");
  323.         else {
  324.                 node = malloc(sizeof(struct avl_node));
  325.                 init_node(node, key);
  326.                 avl_new_link(node, parent, which);
  327.                 avl_insert(root, node);
  328.         }
  329. }
  330. void del(struct avl_head *root, int key)
  331. {
  332.         struct avl_node *parent, **which, *node;

  333.         node = root->head;

  334.         while (node) {
  335.                 if (key < node->key)
  336.                         node = node->lchild;
  337.                 else if (key > node->key)
  338.                         node = node->rchild;
  339.                 else
  340.                         break;
  341.         }

  342.         if (node == NULL) {
  343.                 fprintf(stderr, "this key is not bound\n");
  344.         } else {
  345.                 avl_del(root, node);
  346.                 free(node);
  347.         }
  348. }

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

上一篇:treap

下一篇:置换选择

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