斜堆
斜堆是左式堆的自调节形式。
public class SkewHeap<T extends Comparable<? super T>> { private Node1<T> root; public SkewHeap(){ this.root=null; } public void merge(SkewHeap<T> rhs){ if(this.root==rhs.root){ return; } merge(this.root, rhs.root); rhs.root=null; } private Node1<T> merge(Node1<T> t1,Node1<T> t2){ if(t1==null){ return t2; } if(t2==null){ return t1; } if(t1.val.compareTo(t2.val)<0){ return merge1(t1, t2); } else{ return merge1(t2, t1); } } private Node1<T> merge1(Node1<T> t1,Node1<T> t2){ if(t1.left==null){ t1.left=t2; }else{ t1.right=merge(t1.right, t2); swapChildren(t1); } return t1; } public void insert(T val){ merge(new Node1(val),root); } public T findMin(){ return root.val; } public T deleteMin(){ T val=root.val; root=merge(root.left,root.right); return val; } public boolean isEmpty(){ return root==null; } public void makeEmpty(){ root=null; } private void swapChildren(Node1<T> t){ Node1<T> left=t.left; Node1<T> right=t.right; t.left=right; t.right=left; } }