Chinaunix首页 | 论坛 | 博客
  • 博客访问: 527138
  • 博文数量: 96
  • 博客积分: 2102
  • 博客等级: 上尉
  • 技术积分: 1695
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:12
文章分类

全部博文(96)

文章存档

2014年(2)

2012年(94)

分类: C/C++

2012-04-13 09:05:25

AVL树:又称平衡二叉树。具有的特性:
                          1>左右子树的深度之差的绝对值小于等于1
                          2>左子树和右子树也是平衡树
              目的:提高查找效率,其平均查找长度为O(logn)
模拟思想:结合了伙伴算法的思想在初始化时,将内存区分成了2^n块,并且使用平衡二叉树进程组织(计算平衡因子)。  然后根据“左小右大”的原则进行查找,找到后将该节点移除并且对该该树进行调衡。对于移除的节点进行新的大小分割,并且根据剩余大小找到合适的插入位置。
                查找情况-----1、比该节点剩余内存大,向右子树查找。
                                    2、比该节点剩余内存小,向左子树查找
                                    3、比该节点剩余大小相等,则插入该节点所在的链。
结构体:
  1. struct MM {
  2.     int total;
  3.     int address;
  4.     int cur_size;
  5.  };
  6.  struct AVL_TREE {
  7.      struct MM mem_id;
  8.      char bar;
  9.      struct AVL_TREE * memLeft;
  10.      struct AVL_TREE * memRight;
  11.      struct AVL_TREE * next;
  12.      struct AVL_TREE * parents;
  13.  };
初始化:

点击(此处)折叠或打开

  1. void init_AVL_tree(struct AVL_TREE ** root, struct AVL_TREE *p)
  2. {
  3.     int mem[]={4,2,1,0,-1,-1,-1,3,-1,-1,7,6,5,-1,-1,-1,8,-1,9,-1,-1};
  4.     static int   in;    
  5.     if(in < sizeof(mem))
  6.     {
  7.         if(mem[in]==-1)
  8.         {    *root = NULL;
  9.             in++;
  10.         }
  11.         else
  12.             {
  13.                 *root = creat_one_node(mem[in++],p);
  14.                 p = *root;
  15.                 init_AVL_tree(&((*root)->memLeft),p);
  16.                 init_AVL_tree(&((*root)->memRight),p);
  17.             }
  18.     }
  19. }
  20. struct AVL_TREE *creat_one_node(int me,struct AVL_TREE *parents)
  21. {
  22.     struct    AVL_TREE * node = (struct AVL_TREE *)malloc(sizeof(struct AVL_TREE));
  23.     node->mem_id.total = two_power_of(me);
  24.     node->mem_id.cur_size = 0;
  25.     node->mem_id.address = node->mem_id.total-1;
  26.     node->bar= 0;
  27.     node->memLeft = NULL;
  28.     node->next = NULL;
  29.     node->parents =parents;
  30.     node->memRight =NULL;
  31.     return node;
  32. }
查找:

点击(此处)折叠或打开

  1. struct AVL_TREE * search_best_MM(struct AVL_TREE *root,int mm)
  2. {
  3.     struct AVL_TREE                     *p, *pre = NULL;

  4.     printf("search_best_MM\n");
  5.      p = root;
  6.     while(p)
  7.     {
  8.         if(mm >((p->mem_id.total)-(p->mem_id.cur_size)))
  9.             p = p->memRight;
  10.         else if(mm < (p->mem_id.total-p->mem_id.cur_size))
  11.         {
  12.             pre = p;
  13.             p = p->memLeft;
  14.         }
  15.         else
  16.         {
  17.             pre = p;
  18.             break;
  19.         }
  20.     }
  21.     return pre;
  22. }
调衡:LL:
               

点击(此处)折叠或打开

  1. void balance_LL(struct AVL_TREE * tree,struct AVL_TREE **root)
  2. {
  3.     struct AVL_TREE             *next1;

  4.     next1 = tree->memLeft;
  5.     printf("LL\n");
  6.     next1->parents = tree->parents;
  7.     if(tree->parents) {
  8.     if(tree->parents->memLeft == tree)
  9.      tree->parents->memLeft = next1;
  10.     else
  11.      tree->parents->memRight = next1;
  12.     } else
  13.           (*root) = next1;

  14.     tree->memLeft = next1->memRight;
  15.     if(next1->memRight != NULL)
  16.         next1->memRight->parents = tree;
  17.     next1->memRight = tree;
  18.     tree->parents = next1;

  19.     tree->bar = '0';
  20.     next1->bar ='0';
  21.     change_bar(next1,1,LEFT);
  22. }
 LR:
                    

点击(此处)折叠或打开

  1. void balance_LR(struct AVL_TREE *tree,struct AVL_TREE **root)
  2. {
  3.      struct AVL_TREE *next1, *next2;

  4.     printf("LR\n");
  5.     next1 = tree->memLeft;
  6.     next2 = next1->memRight;
  7.     next2->parents = tree->parents;
  8.     printf("LR\n");
  9.     if(tree->parents) {
  10.       if(tree->parents->memLeft == tree)
  11.      tree->parents->memLeft = next2;
  12.        else
  13.           tree->parents->memRight = next2;
  14.     } else
  15.          (*root) = next2;
  16.     next1->memRight = next2->memLeft;
  17.     if(next2->memLeft)
  18.         next2->memLeft->parents = next1;
  19.     tree->memLeft = next2->memRight;
  20.    if(next2->memRight)
  21.         next2->memRight->parents = tree;

  22.     tree->bar = '0'-1;
  23.     next1->bar = next2->bar = '0';
  24.     next2->memRight = tree;
  25.     tree->parents = next2;
  26.     next2->memLeft = next1;
  27.     next1->parents = next2;
  28.     change_bar(next2,1,LEFT);
  29. }
  RR:
              

点击(此处)折叠或打开

  1. void balance_RR(struct AVL_TREE * tree,struct AVL_TREE ** root)
  2. {
  3.     struct    AVL_TREE                *next1;

  4.     printf("RR\n");
  5.     next1 = tree->memRight;
  6.    
  7.     printf("RR\n");
  8.     if(tree->parents)
  9.      if(tree->parents->memLeft == tree)
  10.          tree->parents->memLeft = next1;
  11.         else
  12.          tree->parents->memRight = next1;
  13.     printf("RR\n");
  14.     next1->parents = tree->parents;
  15.     printf("RR\n");
  16.      if(tree->parents == NULL)
  17.          (*root) = next1;

  18.     printf("RR\n");
  19.     tree->memRight = next1->memLeft;
  20.     if(next1->memLeft)
  21.         next1->memLeft->parents = tree;
  22.     next1->memLeft = tree;
  23.     tree->parents = next1;

  24.     next1->bar = '0';
  25.     tree->bar = '0';
  26.     change_bar(next1,1,RIGHT);

  27. }
     RL:
           
  

点击(此处)折叠或打开

  1. void balance_RL(struct AVL_TREE * tree,struct AVL_TREE ** root)
  2. {
  3.     struct AVL_TREE                *next1, *next2;

  4.     printf("RL\n");
  5.     next1 = tree->memRight;
  6.     next2 = tree->memLeft;

  7.      if(tree->parents->memLeft == tree)
  8.          tree->parents->memLeft = next2;
  9.       else
  10.      tree->parents->memRight = next2;
  11.      next2->parents = tree->parents;
  12.      if(tree->parents == NULL)
  13.           (*root) = next2;
  14.      tree->memRight = next2->memLeft;
  15.      if(next2->memLeft)
  16.          next2->memLeft->parents = tree;

  17.      next1->memLeft = next2->memRight;
  18.      if(next2->memRight)
  19.          next2->memRight->parents = next1;
  20.     
  21.      next2->memRight = next1;
  22.      next1->parents = next2;
  23.      next2->memLeft = tree;
  24.      tree->parents = next2;

  25.      next2->bar='0';
  26.      tree->bar ='1';
  27.      next1->bar = '0';
  28.      change_bar(next2,1,RIGHT);
  29. }
下列是完整代码:(多个文件的整合)
  code.txt  
阅读(1359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~