二项队列的简单实现

二项队列

  二项队列也是优先队列的一种实现方式,之前有用左式堆和二叉堆来实现优先队列,不过二项队列与左式堆和二叉堆的不同在于二项队列能有效的支持合并/插入/DeleteMin操作,每次操作的最坏运行时间是O(log n),对于插入操作,平均是花费常数时间

什么是二项队列

  与左式堆和二叉堆不同的是,二项队列不是树,而是树的集合,也就是森林.
  这个森林中,每个高度最多只有一棵树.

这里不再详细说明二项队列的思路,如有需要,可以自行百度(因为百度上已经有很多比较详细的了)

代码实现


#include <stddef.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct BinNode *Positon, *BinTree;
typedef struct Collection *BinQueue;
typedef int ElementType;

int MaxTrees = 100000;
int Capacity = 200000;
struct BinNode {
   ElementType Element;
   Positon LeftChild;
   Positon NextSibling;
};

struct Collection {
   int CurrentSize;
   BinTree TheTrees[1000000];
};

void Error(char *message) {
   printf("%s\n", message);
}

int IsEmpty(BinQueue H) {
   return H->CurrentSize == 0;
}

BinQueue Initialize() {
   BinQueue H = malloc(sizeof(struct Collection));
   H->CurrentSize = 0;

   int i;
   for (i = 0; i <= MaxTrees; i++) {
       H->TheTrees[i] = NULL;
   }
   return H;
}

BinTree CombineTrees(BinTree T1, BinTree T2) {
   if (T1->Element > T2->Element)
       return CombineTrees(T2, T1);
   T2->NextSibling = T1->LeftChild;
   T1->LeftChild = T2;
   return T1;
}

/* Merge two binomial queues */
/* Not optimized for early termination */
/* H1 contains merged result */
BinQueue Merge(BinQueue H1, BinQueue H2) {
   BinTree T1, T2, Carry = NULL;
   int i, j;

   if (H1->CurrentSize + H2->CurrentSize > Capacity)
       Error("Merged would exceed capacity");

   H1->CurrentSize += H2->CurrentSize;
   for (i = 0, j = 1; j <= H1->CurrentSize; i++, j *= 2) {
       T1 = H1->TheTrees[i];
       T2 = H2->TheTrees[i];

       switch (!!T1 + 2 * !!T2 + 4 * !!Carry) {
           case 0:/* No trees */
           case 1:/* Only H1 */
               break;
           case 2:/* Only H2 */
               H1->TheTrees[i] = T2;
               H2->TheTrees[i] = NULL;
               break;
           case 4:/* Only Carry */
               H1->TheTrees[i] = Carry;
               Carry = NULL;
               break;
           case 3:
               Carry = CombineTrees(T1, T2);
               H1->TheTrees[i] = H2->TheTrees[i] = NULL;
               break;
           case 5:
               Carry = CombineTrees(T1, Carry);
               H1->TheTrees[i] = NULL;
               break;
           case 6:
               Carry = CombineTrees(T2, Carry);
               H2->TheTrees[i] = NULL;
               break;
           case 7:
               H1->TheTrees[i] = Carry;
               Carry = CombineTrees(T1, T2);
               H2->TheTrees[i] = NULL;
               break;
       }

   }
   return H1;
}


ElementType DeleteMin(BinQueue H) {
   int i, j;
   int MinTree;
   BinQueue DeletedQueue;
   Positon DeletedTree, OldRoot;
   ElementType MinItem;

   if (IsEmpty(H)) {
       Error("Empty binomial queue");
       return -INFINITY;
   }

   MinItem = INFINITY;
   for (i = 0; i < MaxTrees; i++) {
       if (H->TheTrees[i] &&
           H->TheTrees[i]->Element < MinItem) {
           MinItem = H->TheTrees[i]->Element;
           printf("%d\n", MinItem);
           MinTree = i;
       }
   }

   DeletedTree = H->TheTrees[MinTree];
   OldRoot = DeletedTree;
   DeletedTree = DeletedTree->LeftChild;
   free(OldRoot);

   DeletedQueue = Initialize();
   DeletedQueue->CurrentSize = (1 << MinTree) - 1;
   for (j = MinTree - 1; j >= 0; j--) {
       DeletedQueue->TheTrees[j] = DeletedTree;
       DeletedTree = DeletedTree->NextSibling;
       DeletedQueue->TheTrees[j]->NextSibling = NULL;
   }

   H->TheTrees[MinTree] = NULL;
   H->CurrentSize -= DeletedQueue->CurrentSize + 1;

   Merge(H, DeletedQueue);
   return MinItem;
}

int main() {
   int i;
   BinQueue Q[1000];
   for (i = 0; i < 1000; i++) {
       Positon P = malloc(sizeof(struct BinNode));
       P->Element = i;
       P->NextSibling = P->LeftChild = NULL;
       Q[i] = Initialize();
       Q[i]->CurrentSize = 1;
       Q[i]->TheTrees[0] = P;
   }
   BinQueue result = Merge(Q[0], Q[1]);
   for (i = 2; i < 1000; i++) {
       result = Merge(result, Q[i]);
   }
   int j = DeleteMin(result);
   printf("%d\n", j);
   return 0;
}

Merge

  Merge操作是按高度从小到大依次合并

Deletemin

  遍历一遍二项队列中的树根,找到最小的那个,然后把该二项树打散成若干哥二项树,构造成一个新的二项队列,然后删掉这个二项树构造出另一个新的二项队列,最后合并这两个新的二项队列

  • 代码摘自 数据结构与算法分析第二版 ,基于书上的代码增添了书中未完善的部分,提供了一个main函数用于测试
点赞