二项树:
高度为0的二项树是一棵单结点树,高度为k的二项树B(k)是由两棵二项树B(k-1)组成的,
其中一棵树是另一棵树根的最左孩子。
二项队列:
二项队列是优先队列的一种实现。
二项队列是由高度不重复的若干个二项树构成的森林。
高度不重复意味着相同高度的二项树在二项队列中最多存在一个,
二项队列中的二项树的高度可以不连续。
组成二项队列的所有二项树都具有一致的堆序性,每一个二项树不仅仅满足堆序性,
而且每个二项树的堆序性必须保持一致,要么都是大根堆,要么就是小根堆。
合并: 将高度相同的两棵二项树B(k)合并为一棵二项树B(k+1),
更新到二项队列的森林中(vector)
删除最小根: 首先找到一棵具有最小根的二项树B(k)来完成, 从森林中除去二项树B(k),
形成新的二项队列H1; 再除去B(k)的根,得到一些二项树B(0),B(1),....,B(k-1),
它们共同形成二项队列H2; 合并H1, H2.
#include <iostream>
#include <vector>
#include <queue>
struct BinNode
{
int val;
BinNode* leftChild;
BinNode* nextSibling;
BinNode(int v)
: val(v), leftChild(nullptr), nextSibling(nullptr) {}
};
class BinQueue
{
public:
BinQueue() : curSize(0) {}
~BinQueue();
void insert(int k);
bool deleteMin();
void layer();
private:
int capacity();
void merge(BinQueue &bq);
BinNode* combineTrees(BinNode* t1, BinNode* t2);
void destroy(BinNode* t);
int findMinIndex();
int curSize; // 森林中树的结点的个数
std::vector<BinNode*> trees; // 存放森林中树的根节点
};
BinQueue::~BinQueue()
{
curSize = 0;
int size = trees.size();
for (int i = 0; i < size; ++i)
if (trees[i] != nullptr)
destroy(trees[i]);
}
void BinQueue::destroy(BinNode* t)
{
if (t != nullptr)
{
destroy(t->leftChild);
destroy(t->nextSibling);
delete t;
t = nullptr;
}
}
int BinQueue::capacity()
{
int sum = 0;
for (unsigned int i = 0; i < trees.size(); ++i)
sum += (1 << i);
return sum;
}
BinNode* BinQueue::combineTrees(BinNode* t1, BinNode* t2)
{
if (t1->val > t2->val)
return combineTrees(t2, t1);
t2->nextSibling = t1->leftChild;
t1->leftChild = t2;
return t1;
}
void BinQueue::merge(BinQueue &bq)
{
if (this == &bq)
return;
this->curSize += bq.curSize;
if (this->curSize > this->capacity())
{
int oldSize = this->trees.size();
int newSize = (oldSize > bq.curSize ? oldSize : bq.curSize) + 1;
this->trees.resize(newSize);
for (int i = oldSize; i < newSize; ++i)
{
this->trees[i] = nullptr;
}
}
BinNode* t1 = nullptr;
BinNode* t2 = nullptr;
BinNode* carry = nullptr;
for (int i = 0, j = 1; j <= this->curSize; ++i, j *= 2)
{
t1 = this->trees[i];
t2 = bq.trees.size() > i ? bq.trees[i] : nullptr;
switch (!!t1 + 2 * !!t2 + 4 * !!carry)
{
case 0: /* No trees */
case 1: /* Only this */
break;
case 2: /* Only bq */
this->trees[i] = t2;
bq.trees[i] = nullptr;
break;
case 4: /* Only carry */
this->trees[i] = carry;
carry = nullptr;
break;
case 3: /* this and bq */
carry = combineTrees(t1, t2);
this->trees[i] = bq.trees[i] = nullptr;
break;
case 5: /* this and carry */
carry = combineTrees(t1, carry);
this->trees[i] = nullptr;
break;
case 6: /* bq and carry */
carry = combineTrees(t2, carry);
bq.trees[i] = nullptr;
break;
case 7: /* All three */
this->trees[i] = carry;
carry = combineTrees(t1, t2);
bq.trees[i] = nullptr;
break;
}
}
bq.curSize = 0;
}
void BinQueue::insert(int k)
{
BinNode* pNode = new BinNode(k);
BinQueue bq;
bq.curSize = 1;
bq.trees.push_back(pNode);
merge(bq);
}
int BinQueue::findMinIndex()
{
int i = 0, index = -1;
for (i = 0; i < trees.size(); ++i)
{
if (trees[i] != nullptr)
{
index = i;
break;
}
}
if (index == -1)
return index;
for (i = index + 1; i < trees.size(); ++i)
if (trees[i] != nullptr && trees[index]->val > trees[i]->val)
{
index = i;
}
return index;
}
bool BinQueue::deleteMin()
{
int i = findMinIndex();
if (i < 0)
return false;
BinNode* pDelNode = trees[i];
BinNode* pMinNode = pDelNode;
pDelNode = pDelNode->leftChild;
delete pMinNode;
pMinNode = nullptr;
// 存放所删的二项树中剩余的二项队列
BinQueue delQue;
delQue.trees.resize(i);
delQue.curSize = (1 << i) - 1;
for (int j = i - 1; j >= 0; --j)
{
delQue.trees[j] = pDelNode;
pDelNode = pDelNode->nextSibling;
delQue.trees[j]->nextSibling = nullptr;
}
trees[i] = nullptr;
curSize -= (delQue.curSize + 1);
merge(delQue);
return true;
}
void BinQueue::layer()
{
for (int i = 0; i < trees.size(); ++i)
{
if (trees[i] == nullptr)
continue;
std::cout << "print trees[" << i << "]" << std::endl;
BinNode* t = trees[i];
std::queue<BinNode*> qu;
qu.push(t);
while (!qu.empty())
{
int size = qu.size();
for (int i = 0; i < size; ++i)
{
t = qu.front();
qu.pop();
std::cout << t->val << " ";
BinNode* lc = t->leftChild;
if (lc != nullptr)
{
qu.push(lc);
t = lc->nextSibling;
while (t != nullptr)
{
qu.push(t);
t = t->nextSibling;
}
}
}
std::cout << std::endl;
}
}
}
int main()
{
int arr[] = {12, 21, 24, 65, 14, 26, 16, 18, 1, 2, 3, 4, 5, 6, 7, 8};
BinQueue bq;
for (int i = 0; i < sizeof(arr) / sizeof(int); ++i)
bq.insert(arr[i]);
bq.layer();
bq.deleteMin();
bq.layer();
return 0;
}
print trees[4]
1
12 5 3 2
14 24 21 7 6 4
16 26 65 8
18
print trees[0]
2
print trees[1]
3
4
print trees[2]
5
7 6
8
print trees[3]
12
14 24 21
16 26 65
18