Skew heap

Last updated

A skew heap (or self-adjusting heap) is a heap data structure implemented as a binary tree. Skew heaps are advantageous because of their ability to merge more quickly than binary heaps. In contrast with binary heaps, there are no structural constraints, so there is no guarantee that the height of the tree is logarithmic. Only two conditions must be satisfied:

Contents

A skew heap is a self-adjusting form of a leftist heap which attempts to maintain balance by unconditionally swapping all nodes in the merge path when merging two heaps. (The merge operation is also used when adding and removing values.) With no structural constraints, it may seem that a skew heap would be horribly inefficient. However, amortized complexity analysis can be used to demonstrate that all operations on a skew heap can be done in O(log n). [1] In fact, with denoting the golden ratio, the exact amortized complexity is known to be logφn (approximately 1.44 log2n). [2] [3]

Definition

Skew heaps may be described with the following recursive definition:[ citation needed ][ clarification needed ]

Operations

Merging two heaps

When two skew heaps are to be merged, we can use a similar process as the merge of two leftist heaps:


template<classT,classCompareFunction>SkewNode<T>*CSkewHeap<T,CompareFunction>::Merge(SkewNode<T>*root_1,SkewNode<T>*root_2){SkewNode<T>*firstRoot=root_1;SkewNode<T>*secondRoot=root_2;if(firstRoot==NULL)returnsecondRoot;elseif(secondRoot==NULL)returnfirstRoot;if(sh_compare->Less(firstRoot->key,secondRoot->key)){SkewNode<T>*tempHeap=firstRoot->rightNode;firstRoot->rightNode=firstRoot->leftNode;firstRoot->leftNode=Merge(secondRoot,tempHeap);returnfirstRoot;}elsereturnMerge(secondRoot,firstRoot);}

Before: SkewHeapMerge1.svg


after SkewHeapMerge7.svg

Non-recursive merging

Alternatively, there is a non-recursive approach which is more wordy, and does require some sorting at the outset.

SkewHeapMerge1.svg

SkewHeapMerge2.svg

SkewHeapMerge3.svg

SkewHeapMerge4.svg

SkewHeapMerge5.svg

SkewHeapMerge6.svg

SkewHeapMerge7.svg

Adding values

Adding a value to a skew heap is like merging a tree with one node together with the original tree.

Removing values

Removing the first value in a heap can be accomplished by removing the root and merging its child subtrees.

Implementation

In many functional languages, skew heaps become extremely simple to implement. Here is a complete sample implementation in Haskell.

dataSkewHeapa=Empty|Nodea(SkewHeapa)(SkewHeapa)singleton::Orda=>a->SkewHeapasingletonx=NodexEmptyEmptyunion::Orda=>SkewHeapa->SkewHeapa->SkewHeapaEmpty`union`t2=t2t1`union`Empty=t1t1@(Nodex1l1r1)`union`t2@(Nodex2l2r2)|x1<=x2=Nodex1(t2`union`r1)l1|otherwise=Nodex2(t1`union`r2)l2insert::Orda=>a->SkewHeapa->SkewHeapainsertxheap=singletonx`union`heapextractMin::Orda=>SkewHeapa->Maybe(a,SkewHeapa)extractMinEmpty=NothingextractMin(Nodexlr)=Just(x,l`union`r)

References

  1. Sleator, Daniel Dominic; Tarjan, Robert Endre (February 1986). "Self-Adjusting Heaps". SIAM Journal on Computing . 15 (1): 52–69. CiteSeerX   10.1.1.93.6678 . doi:10.1137/0215004. ISSN   0097-5397.
  2. Kaldewaij, Anne; Schoenmakers, Berry (1991). "The Derivation of a Tighter Bound for Top-Down Skew Heaps" (PDF). Information Processing Letters . 37 (5): 265–271. CiteSeerX   10.1.1.56.8717 . doi:10.1016/0020-0190(91)90218-7.
  3. Schoenmakers, Berry (1997). "A Tight Lower Bound for Top-Down Skew Heaps" (PDF). Information Processing Letters . 61 (5): 279–284. CiteSeerX   10.1.1.47.447 . doi:10.1016/S0020-0190(97)00028-8. S2CID   11288837.