实现:
import java.util.LinkedList;
import java.util.Stack;
/**
*
* Skew Heap
*
* Copyright (c) 2011 ljs (http://blog.csdn.net/ljsspace/)
* Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
*
* @author ljs
* 2011-08-25
*
*/
public class SkewHeap {
static class Node{
int key;
Node left;
Node right;
public Node(int key){
this.key = key;
}
public String toString(){
return String.valueOf(this.key);
}
public Node recursiveMerge(Node rhsRoot){
if(rhsRoot==this || rhsRoot == null) {
//Note: no need to swap the left and right children of this tree
//Node tmp = this.left;
//this.left = this.right;
//this.right = tmp;
return this;
}
Node root1 = null; //the root of the merged tree
Node root2 = null;
if(rhsRoot.key stack = new Stack();
Node r1 = root1;
Node r2 = root2;
while(r1 != null && r2 != null){
if(r1.key queue = new LinkedList();
int n = A.length;
//init: queue all elements as a single-node tree
for(int i=0;i1){
Node root1 = queue.remove(); //dequeue
Node root2 = queue.remove();
Node rootNode = iterativeMerge(root1,root2);
queue.add(rootNode);
}
Node rootNode = queue.remove();
return new SkewHeap(rootNode);
}
public void insert(int x){
this.root = SkewHeap.iterativeMerge(new Node(x), this.root);
}
public Integer extractMin(){
if(this.root == null) return null;
int min = this.root.key;
this.root = SkewHeap.iterativeMerge(this.root.left, this.root.right);
return min;
}
public static void main(String[] args) {
int[] A = new int[]{4,8,10,9,1,3,5,6,11};
SkewHeap heap = SkewHeap.buildHeap(A);
heap.insert(7);
Integer min = null;
while((min = heap.extractMin()) != null){
System.out.format(" %d", min);
}
System.out.println();
System.out.println("********************");
A = new int[]{3,10,8,21,14,17,23,26};
Node a0 = new Node(A[0]);
Node a1 = new Node(A[1]);
Node a2 = new Node(A[2]);
Node a3 = new Node(A[3]);
Node a4 = new Node(A[4]);
Node a5 = new Node(A[5]);
Node a6 = new Node(A[6]);
Node a7 = new Node(A[7]);
a0.left = a1;
a0.right = a2;
a1.left = a3;
a1.right = a4;
a4.left = a6;
a2.left = a5;
a5.left = a7;
SkewHeap h1 = new SkewHeap(a0);
int[] B = new int[]{6,12,7,18,24,37,18,33};
Node b0 = new Node(B[0]);
Node b1 = new Node(B[1]);
Node b2 = new Node(B[2]);
Node b3 = new Node(B[3]);
Node b4 = new Node(B[4]);
Node b5 = new Node(B[5]);
Node b6 = new Node(B[6]);
Node b7 = new Node(B[7]);
b0.left = b1;
b0.right = b2;
b1.left = b3;
b1.right = b4;
b4.left = b7;
b2.left = b5;
b2.right = b6;
SkewHeap h2 = new SkewHeap(b0);
heap = SkewHeap.merge(h1,h2);
while((min = heap.extractMin()) != null){
System.out.format(" %d", min);
}
System.out.println();
System.out.println("********************");
A = new int[]{1,4,23,63,24,44,28};
a0 = new Node(A[0]);
a1 = new Node(A[1]);
a2 = new Node(A[2]);
a3 = new Node(A[3]);
a4 = new Node(A[4]);
a5 = new Node(A[5]);
a6 = new Node(A[6]);
a0.left = a1;
a0.right = a2;
a1.left = a3;
a1.right = a4;
a2.left = a5;
a2.right = a6;
h1 = new SkewHeap(a0);
B = new int[]{13,17,99,57,49,105,201};
b0 = new Node(B[0]);
b1 = new Node(B[1]);
b2 = new Node(B[2]);
b3 = new Node(B[3]);
b4 = new Node(B[4]);
b5 = new Node(B[5]);
b6 = new Node(B[6]);
b0.left = b1;
b0.right = b2;
b1.left = b3;
b1.right = b4;
b2.left = b5;
b2.right = b6;
h2 = new SkewHeap(b0);
heap = SkewHeap.merge(h1,h2);
while((min = heap.extractMin()) != null){
System.out.format(" %d", min);
}
System.out.println();
}
}
参考资料:
Sleator & Tarjan (1986). "Self-Adjusting Heaps"