For example, 60 is represented as 11200 in skew binary (31 + 15 + 7 + 7), and adding 1 produces 12000 (31 + 15 + 15). Since the next higher digit is guaranteed not to be 2, a carry is performed at most once.1
typedefstructBinNode*Position;typedefstructCollection*BinQueue;tyepdefstructBinNode*BenTree;structBinNode{ElementTypeElement;PositionLeftChild;PositionNextSibling;};structCollection{intCurrentSize;// total number of nodesBinTreeTheTrees[MaxTrees];}
BinTreeCombineTrees(BinTreeT1,BinTreeT2){if(T1->Element>T2->Element)returnCombineTrees(T2,T1);// attach the larger one to the smaller one// insert T2 to be the leftmost child of T1T2->NextSibling=T1->LeftChild;T1->LeftChild=T2;returnT1;}
BinQueueMerge(BinQueueH1,BinQueueH2){BinTreeT1,T2,Carry=NULL;inti,j;if(H1->CurrentSize+H2->CurrentSize>Capacity)ErrorMessage();H1->CurrentSize+=H2->CurrentSize;for(i=0,j=1;j<=H1->CurrentSize;i++,j*=2){T1=H1->TheTrees[i];T2=H2->TheTrees[i];if(Carry==NULL){if(T2==NULL)// do nothingelse{if(T1==NULL){H1->TheTrees[i]=T2;H2->TheTrees[i]=NULL;}else{Carry=CombineTrees(T1,T2);H1->TheTrees[i]=H2->TheTrees[i]=NULL;}}}else{if(T2==NULL){if(T1==NULL){H1->TheTrees[i]=Carry;Carry=NULL;}else{Carry=CombineTrees(T1,Carry);H1->TheTrees[i]=NULL}}else{if(T1==NULL){Carry=CombineTrees(T2,Carry);H2->TheTrees[i]=NULL;}else{H1->TheTrees[i]=Carry;Carry=CombineTrees(T1,T2);H2->TheTrees[i]=NULL;}}}}returnH1;}
voidDeleteMin(BinQueueH){BinQueueDeletedQueue;PositionDeletedTree,OldRoot;ElementTypeMinItem=Infinity;// the min item to be returnedinti,j,MinTree;if(IsEmpty(H)){PrintErrorMessage();return-Infinity;}/* Step 1: find the minimum item */for(i=0;i<MaxTrees;i++){if(H->TheTrees[i]&&H->TheTrees[i]->Element<MinItem){MinItem=H->TheTrees[i]->Element;MinTree=i;}}// this can be optimized by maintaining a ptr to minterm in queueDeletedTree=H->TheTrees[MinTree];/* Step 2: remove MinTree from H, creating H' */H->TheTrees[MinTree]=NULL;/* Step 3.1: remove the root */OldRoot=DeletedTree;DeletedTree=DeletedTree->LeftChild;free(OldRoot);/* Step 3.2: create H'' */DeletedQueue=IntializeBinQueue();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->CurrentSize-=DeletedQueue->CurrentSize+1;/* Step 4: merge H' and H'' */H=merge(H,DeletedQueue);returnMinItem;}
#ifndef FIBONACCI_HEAP_H#define FIBONACCI_HEAP_H#include"Heap.hpp"#include<cmath>#include<list>#include<unordered_map>#include<vector>usingnamespacestd;template<typenameValueType>classFibonacciHeap:publicHeap<ValueType>{private:// Node structure for the Fibonacci heapstructNode{ValueTypevalue;intkey;intdegree;boolmark;Node*parent;list<Node*>children;// Constructor for the Node structureNode(ValueTypevalue,intkey):value(value),key(key),degree(0),mark(false),parent(nullptr){}};// Pointer to the minimum node in the heapNode*min_node;// List of root nodes in the heaplist<Node*>root_list;unordered_map<ValueType,Node*>value_to_node;intnode_count;// Consolidate the trees of the same degreevoidconsolidate(){intmax_degree=static_cast<int>(log2(node_count))+1;vector<Node*>degree_table(max_degree,nullptr);// iterate root list to consolidate trees of the same degreefor(autoit=root_list.begin();it!=root_list.end();it++){Node*x=*it;intdegree=x->degree;while(degree_table[degree]!=nullptr){Node*y=degree_table[degree];if(y->key<x->key)swap(x,y);attach(y,x);// attach y to xdegree_table[degree]=nullptr;degree++;}degree_table[degree]=x;}// rebuild root listmin_node=nullptr;root_list.clear();for(Node*node:degree_table){if(node){root_list.push_back(node);if(!min_node||node->key<min_node->key)min_node=node;}}}// Attach a child node to a parent nodevoidattach(Node*target_child,Node*target_parent){root_list.remove(target_child);target_parent->children.push_back(target_child);target_child->parent=target_parent;target_parent->mark=false;target_parent->degree++;}// Cut a child node from its parent nodevoidcut(Node*child,Node*parent){parent->children.remove(child);parent->degree--;root_list.push_back(child);child->parent=nullptr;child->mark=false;}// Perform cascading cut on a nodevoidcascadingCut(Node*node){Node*parent=node->parent;if(parent){if(!parent->mark)parent->mark=true;else{cut(node,parent);cascadingCut(parent);}}}public:// Constructor for the Fibonacci heapFibonacciHeap():min_node(nullptr),node_count(0){}// Insert a new node into the heapvoidinsert(ValueTypevalue,intkey)override{Node*new_node=newNode(value,key);root_list.push_back(new_node);// lazy insertionif(!min_node||key<min_node->key)min_node=new_node;value_to_node[value]=new_node;++node_count;}// Extract the minimum node from the heappair<ValueType,int>extractMin()override{if(!min_node)throw"Heap is empty";Node*old_min_node=min_node;pair<ValueType,int>result={old_min_node->value,old_min_node->key};// add children of the minimun node to root listfor(autochild:old_min_node->children){child->parent=nullptr;root_list.push_back(child);}// remove the min noderoot_list.remove(old_min_node);value_to_node.erase(old_min_node->value);deleteold_min_node;if(root_list.empty())min_node=nullptr;else{min_node=*root_list.begin();consolidate();}--node_count;returnresult;}// Decrease the key of a nodevoiddecreaseKey(ValueTypevalue,intnew_key)override{if(value_to_node.find(value)==value_to_node.end())throw"Value not found in heap";Node*node=value_to_node[value];if(new_key>node->key)throw"New key is greater than current key";node->key=new_key;Node*parent=node->parent;if(parent&&node->key<parent->key){cut(node,parent);cascadingCut(parent);}if(node->key<min_node->key)min_node=node;}// Check if the heap is emptyboolisEmpty()override{returnroot_list.empty();}};#endif // FIBONACCI_HEAP_H
In a binomial queue, the total number of the nodes at even depth is always ___ than that of the nodes at odd depth (the root is defined to be at the depth 0).