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

2014年(16)

我的朋友
最近访客

分类: C/C++

2014-05-21 10:32:18


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. /****dagendui****/
  5. struct treap {
  6.         int key;
  7.         int perior;
  8.         struct treap *lchild;
  9.         struct treap *rchild;
  10. };


  11. void init_node(struct treap *node, int key)
  12. {
  13.         node->lchild = node->rchild = NULL;
  14.         node->key = key;
  15.         do {
  16.                 node->perior = rand()%100;
  17.         } while (node->perior == INT_MIN || node->perior == INT_MAX);
  18. }
  19. inline struct treap *r_rotate(struct treap *node)
  20. {
  21.         struct treap *tmp = node;
  22.         if (node->lchild && node->lchild->perior > node->perior) {
  23.                 tmp = node->lchild;
  24.                 node->lchild = tmp->rchild;
  25.                 tmp->rchild = node;
  26.         }
  27.         return tmp;
  28. }
  29. inline struct treap *l_rotate(struct treap *node)
  30. {
  31.         struct treap *tmp = node;
  32.         if (node->rchild && node->rchild->perior > node->perior) {
  33.                 tmp = node->rchild;
  34.                 node->rchild = tmp->lchild;
  35.                 tmp->lchild = node;
  36.         }
  37.         return tmp;
  38. }
  39. struct treap *do_insert(struct treap *root, struct treap *node)
  40. {
  41.         if (!root) {
  42.                 return node;
  43.         } else {
  44.                 if (node->key < root->key) {
  45.                         root->lchild = do_insert(root->lchild, node);
  46.                         return r_rotate(root);
  47.                 } else if (node->key > root->key) {
  48.                         root->rchild = do_insert(root->rchild, node);
  49.                         return l_rotate(root);
  50.                 } else {
  51.                         fprintf(stderr, "this key has been bound\n");
  52.                         free (node);
  53.                         return root;
  54.                 }
  55.         }

  56. }

  57. struct treap * insert(struct treap *root, int key)
  58. {
  59.         struct treap *node;
  60.         node = malloc(sizeof(*node));
  61.         init_node(node, key);
  62.         return do_insert(root, node);
  63. }
  64. struct treap *do_del(struct treap *node)
  65. {
  66.         struct treap *tmp, *ret;

  67.         node->perior = INT_MIN;
  68.         ret = NULL;
  69.         if (!node->lchild && !node->rchild) {
  70.                 free(node);
  71.                 return NULL;
  72.         } else if (!node->lchild) {
  73.                 ret = l_rotate(node);
  74.                 ret->lchild = do_del(node);
  75.         } else if (!node->rchild) {
  76.                 ret = r_rotate(node);
  77.                 ret->rchild = do_del(node);
  78.         } else if (node->lchild->key < node->rchild->key) {
  79.                 ret = l_rotate(node);
  80.                 ret->lchild = do_del(node);
  81.         } else {
  82.                 ret = r_rotate(node);
  83.                 ret->rchild = do_del(node);
  84.         }
  85.         return ret;
  86. }
  87. struct treap *del(struct treap *root, int key)
  88. {
  89.         struct treap *parent, *tmp;
  90.         struct treap *ret;

  91.         ret = root;
  92.         parent = NULL;
  93.         while (root) {
  94.                 if (key < root->key) {
  95.                         parent = root;
  96.                         root = root->lchild;
  97.                 } else if (key > root->key) {
  98.                         parent = root;
  99.                         root = root->rchild;
  100.                 } else
  101.                         break;
  102.         }

  103.         if (!root) {
  104.                 fprintf(stderr, "del :this key is not bound\n");
  105.                 return ret;
  106.         }

  107.         tmp = do_del(root);
  108.         if (parent) {
  109.                 if (parent->lchild == root)
  110.                         parent->lchild = tmp;
  111.                 else
  112.                         parent->rchild = tmp;
  113.         } else
  114.                 ret = tmp;

  115.         return ret;
  116. }

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

上一篇:rbtree

下一篇:avl2

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