|
| 1 | + |
| 2 | +public class BinomialHeap { |
| 3 | + public static void main(String[] args) throws Exception { |
| 4 | + BinomialHeap myHeap=new BinomialHeap(); |
| 5 | + BinomialHeap myHeap2=new BinomialHeap(); |
| 6 | + int a[] = {12, 7, 25, 15, 28, 33, 41}; |
| 7 | + int b[] = {27,11,8,17,14,38,6,29,12,18,1,25,10}; |
| 8 | + for(Integer i:a) myHeap.insert(i); |
| 9 | + for(Integer i:b) myHeap2.insert(i); |
| 10 | + myHeap.print(); |
| 11 | + myHeap2.print(); |
| 12 | + myHeap2.decreaseKey(38,5); |
| 13 | + myHeap2.print(); |
| 14 | + System.out.println(myHeap2.getMinimum().key); |
| 15 | + System.out.println(myHeap2.extractMin().key); |
| 16 | + myHeap2.print(); |
| 17 | + System.out.println(myHeap2.extractMin().key); |
| 18 | + System.out.println(myHeap2.extractMin().key); |
| 19 | + myHeap2.print(); |
| 20 | + myHeap2.delete(18); |
| 21 | + myHeap2.print(); |
| 22 | + myHeap.union(myHeap2).print(); |
| 23 | + } |
| 24 | + |
| 25 | + private class heapNode{ |
| 26 | + int key; |
| 27 | + int degree; |
| 28 | + heapNode parent,child,sibling; |
| 29 | + |
| 30 | + public heapNode(int key){ |
| 31 | + this.key=key; |
| 32 | + this.degree=0; |
| 33 | + parent=child=sibling=null; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private heapNode head; |
| 38 | + public BinomialHeap(){ |
| 39 | + this.head=null; |
| 40 | + } |
| 41 | + |
| 42 | + public BinomialHeap(heapNode node){ |
| 43 | + this.head=node; |
| 44 | + } |
| 45 | + |
| 46 | + public BinomialHeap makeHeap(heapNode node){ |
| 47 | + return new BinomialHeap(node); |
| 48 | + } |
| 49 | + |
| 50 | + public heapNode getMinimum() throws Exception { |
| 51 | + int ans=Integer.MAX_VALUE; |
| 52 | + heapNode minimum=null; |
| 53 | + heapNode iter=head; |
| 54 | + while(iter!=null){ |
| 55 | + if(ans>iter.key){ |
| 56 | + ans=iter.key; |
| 57 | + minimum=iter; |
| 58 | + } |
| 59 | + iter=iter.sibling; |
| 60 | + } |
| 61 | + if(minimum==null) throw new Exception("the binomial heap is null"); |
| 62 | + return minimum; |
| 63 | + } |
| 64 | + |
| 65 | + public BinomialHeap union(BinomialHeap heap){ |
| 66 | + head=union(head,heap.head); |
| 67 | + return makeHeap(head); |
| 68 | + } |
| 69 | + |
| 70 | + private heapNode union(heapNode node1, heapNode node2) { |
| 71 | + heapNode root= mergeTwoLists(node1,node2),iter=root; |
| 72 | + heapNode next=iter.sibling,nnext,dummy=new heapNode(-1),prev; |
| 73 | + dummy.sibling=root; |
| 74 | + prev=dummy; |
| 75 | + while(next!=null){ |
| 76 | + nnext=next.sibling; |
| 77 | + if(next.degree<iter.degree){ |
| 78 | + prev.sibling=next; |
| 79 | + next.sibling=iter; |
| 80 | + iter.sibling=nnext; |
| 81 | + prev=next; |
| 82 | + next=nnext; |
| 83 | + continue; |
| 84 | + } |
| 85 | + if(next.degree!=iter.degree){ |
| 86 | + prev=iter; |
| 87 | + iter=next; |
| 88 | + } |
| 89 | + else{ |
| 90 | + iter.sibling=null; |
| 91 | + next.sibling=null; |
| 92 | + if(iter.key>=next.key) iter=incorporate(iter,next); |
| 93 | + else iter=incorporate(next,iter); |
| 94 | + prev.sibling=iter; |
| 95 | + iter.sibling=nnext; |
| 96 | + } |
| 97 | + next=nnext; |
| 98 | + } |
| 99 | + return dummy.sibling; |
| 100 | + } |
| 101 | + //merge two heads in ascending degree |
| 102 | + private heapNode mergeTwoLists(heapNode node1, heapNode node2){ |
| 103 | + if(node1==null) return node2; |
| 104 | + if(node2==null) return node1; |
| 105 | + if(node1.degree<=node2.degree){ |
| 106 | + node1.sibling=mergeTwoLists(node1.sibling,node2); |
| 107 | + return node1; |
| 108 | + } |
| 109 | + node2.sibling=mergeTwoLists(node1,node2.sibling); |
| 110 | + return node2; |
| 111 | + } |
| 112 | + //connect two heads with same degree |
| 113 | + private heapNode incorporate(heapNode node1, heapNode node2){ |
| 114 | + node1.sibling=node2.child; |
| 115 | + node1.parent=node2; |
| 116 | + node2.child=node1; |
| 117 | + node2.degree++; |
| 118 | + return node2; |
| 119 | + } |
| 120 | + |
| 121 | + public void insert(int key) throws Exception { |
| 122 | + heapNode newNode=new heapNode(key); |
| 123 | + if(find(key)) throw new Exception("Cannot add same key into the binomial heap!"); |
| 124 | + head=union(newNode,head); |
| 125 | + } |
| 126 | + |
| 127 | + public heapNode extractMin() throws Exception{ |
| 128 | + heapNode minimum=getMinimum(); |
| 129 | + heapNode iter,dummy=new heapNode(-1); |
| 130 | + dummy.sibling=head; |
| 131 | + iter=dummy; |
| 132 | + while(iter.sibling!=minimum){ |
| 133 | + iter=iter.sibling; |
| 134 | + } |
| 135 | + iter.sibling=minimum.sibling; |
| 136 | + minimum.sibling=null; |
| 137 | + head=union(head,disassemble(minimum.child)); |
| 138 | + return minimum; |
| 139 | + } |
| 140 | + //break down the child of minimum child |
| 141 | + private heapNode disassemble(heapNode minimum){ |
| 142 | + if(minimum==null) return null; |
| 143 | + heapNode curr=minimum,next=curr.sibling,nnext; |
| 144 | + curr.sibling=null; |
| 145 | + while(next!=null){ |
| 146 | + nnext=next.sibling; |
| 147 | + curr.parent=null; |
| 148 | + next.sibling=curr; |
| 149 | + curr=next; |
| 150 | + next=nnext; |
| 151 | + } |
| 152 | + curr.parent=null; |
| 153 | + return curr; |
| 154 | + } |
| 155 | + |
| 156 | + public void delete(int key) throws Exception { |
| 157 | + decreaseKey(key,Integer.MIN_VALUE); |
| 158 | + extractMin(); |
| 159 | + } |
| 160 | + |
| 161 | + public void decreaseKey(int key, int value) throws Exception { |
| 162 | + if(!find(key)) throw new Exception("no key found!"); |
| 163 | + heapNode iter=head; |
| 164 | + while(iter!=null){ |
| 165 | + if(iter.key==key){iter.key=value;return;} |
| 166 | + if(iter.key>key){ |
| 167 | + iter=iter.sibling; |
| 168 | + continue; |
| 169 | + } |
| 170 | + //remember to float up if the min-heap property is broken |
| 171 | + heapNode node=dfs(iter.child,key),parent; |
| 172 | + if(node!=null) { |
| 173 | + node.key=value; |
| 174 | + parent=node.parent; |
| 175 | + while(parent!=null){ |
| 176 | + if(node.key>parent.key) break; |
| 177 | + int tmp=node.key; |
| 178 | + node.key=parent.key; |
| 179 | + parent.key=tmp; |
| 180 | + node=parent; |
| 181 | + parent=parent.parent; |
| 182 | + } |
| 183 | + break; |
| 184 | + } |
| 185 | + iter=iter.sibling; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private boolean find(int key){ |
| 190 | + heapNode iter=head; |
| 191 | + while(iter!=null){ |
| 192 | + if(iter.key==key) return true; |
| 193 | + if(iter.key>key){ |
| 194 | + iter=iter.sibling; |
| 195 | + continue; |
| 196 | + } |
| 197 | + if(dfs(iter.child,key)!=null) return true; |
| 198 | + iter=iter.sibling; |
| 199 | + } |
| 200 | + return false; |
| 201 | + } |
| 202 | + private heapNode dfs(heapNode node,int key){ |
| 203 | + if(node==null) return null; |
| 204 | + if(node.key==key) return node; |
| 205 | + heapNode node1=dfs(node.child,key); |
| 206 | + if(node1==null) node1=dfs(node.sibling,key); |
| 207 | + return node1; |
| 208 | + } |
| 209 | + |
| 210 | + //'print' references: https://github.com/wangkuiwu/datastructs_and_algorithm/blob/master/source/heap/binomial/java/BinomialHeap.java |
| 211 | + public void print() { |
| 212 | + if (head == null) return; |
| 213 | + heapNode p = head; |
| 214 | + System.out.printf("== Binomial Heap( "); |
| 215 | + while (p != null) { |
| 216 | + System.out.printf("B%d ", p.degree); |
| 217 | + p = p.sibling; |
| 218 | + } |
| 219 | + System.out.printf(")details:\n"); |
| 220 | + |
| 221 | + int i=0; |
| 222 | + p = head; |
| 223 | + while (p != null) { |
| 224 | + i++; |
| 225 | + System.out.printf("%d. BinaryHeapB%d: \n", i, p.degree); |
| 226 | + System.out.printf("\t%2d(%d) is root\n", p.key, p.degree); |
| 227 | + print(p.child, p, 1); |
| 228 | + p = p.sibling; |
| 229 | + } |
| 230 | + System.out.printf("\n"); |
| 231 | + } |
| 232 | + private void print(heapNode node, heapNode prev, int direction) { |
| 233 | + while(node != null) { |
| 234 | + if(direction==1) System.out.printf("\t%2d(%d) is %2d's child\n", node.key, node.degree, prev.key); |
| 235 | + else System.out.printf("\t%2d(%d) is %2d's next\n", node.key, node.degree, prev.key); |
| 236 | + if (node.child != null) print(node.child, node, 1); |
| 237 | + |
| 238 | + prev = node; |
| 239 | + node = node.sibling; |
| 240 | + direction = 2; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments