算法之二项堆

  1. packagecom.eshore.sweetop.exdataframe;
  2. publicclassBinomialHeap{
  3. privateBinomialNodehead;
  4. publicBinomialNodegetHead(){
  5. returnhead;
  6. }
  7. privatevoidsetHead(BinomialNodehead){
  8. this.head=head;
  9. }
  10. publicBinomialNodemin(){
  11. BinomialNodenodey=null;
  12. BinomialNodenodex=head;
  13. intmin=Integer.MAX_VALUE;
  14. while(nodex!=null){
  15. if(nodex.getKey()<min){
  16. min=nodex.getKey();
  17. nodey=nodex;
  18. }
  19. nodex=nodex.getSibling();
  20. }
  21. returnnodey;
  22. }
  23. publicvoidlink(BinomialNodenodey,BinomialNodenodez){
  24. nodey.setParent(nodez);
  25. nodey.setSibling(nodez.getChild());
  26. nodez.setChild(nodey);
  27. nodez.setDegree(nodez.getDegree()+1);
  28. }
  29. publicvoidunion(BinomialHeaph2){
  30. this.setHead(merge(this,h2));
  31. BinomialNodepx=null;
  32. BinomialNodex=this.getHead();
  33. BinomialNodenx=x.getSibling();
  34. while(nx!=null){
  35. if(x.getDegree()!=nx.getDegree()
  36. ||(nx.getSibling()!=null&&nx.getSibling().getDegree()==x
  37. .getDegree())){
  38. px=x;
  39. x=nx;
  40. }elseif(x.getKey()<=nx.getKey()){
  41. x.setSibling(nx.getSibling());
  42. link(nx,x);
  43. }else{
  44. if(px==null){
  45. this.setHead(nx);
  46. }else{
  47. px.setSibling(nx);
  48. }
  49. link(x,nx);
  50. x=nx;
  51. }
  52. nx=x.getSibling();
  53. }
  54. }
  55. publicvoidinsert(BinomialNodex){
  56. BinomialHeaph=newBinomialHeap();
  57. x.setParent(null);
  58. x.setChild(null);
  59. x.setSibling(null);
  60. x.setDegree(0);
  61. h.setHead(x);
  62. union(h);
  63. }
  64. publicBinomialNodemerge(BinomialHeaph1,BinomialHeaph2){
  65. BinomialNodehead1=h1.getHead();
  66. BinomialNodehead2=h2.getHead();
  67. BinomialNodend=newBinomialNode();
  68. BinomialNodenode=nd;
  69. while(!(head1==null&&head2==null)){
  70. if(head1==null){
  71. node.setSibling(head2);
  72. head2=head2.getSibling();
  73. }elseif(head2==null){
  74. node.setSibling(head1);
  75. head1=head1.getSibling();
  76. }else{
  77. if(head1.getDegree()<head2.getDegree()){
  78. node.setSibling(head1);
  79. head1=head1.getSibling();
  80. }else{
  81. node.setSibling(head2);
  82. head2=head2.getSibling();
  83. }
  84. }
  85. node=node.getSibling();
  86. }
  87. returnnd.getSibling();
  88. }
  89. publicBinomialNodeextractMin(){
  90. BinomialNodenodep=null;
  91. BinomialNodenodey=null;
  92. BinomialNodenodex=head;
  93. BinomialNodenodeyp=null;
  94. intmin=Integer.MAX_VALUE;
  95. while(nodex!=null){
  96. if(nodex.getKey()<min){
  97. min=nodex.getKey();
  98. nodey=nodex;
  99. nodeyp=nodep;
  100. }
  101. nodep=nodex;
  102. nodex=nodex.getSibling();
  103. }
  104. if(nodey!=null){
  105. if(nodeyp==null){
  106. this.setHead(nodey.getSibling());
  107. }else{
  108. nodeyp.setSibling(nodey.getSibling());
  109. }
  110. BinomialHeaph1=newBinomialHeap();
  111. BinomialNodend=nodey.getChild();
  112. nodey.setChild(null);
  113. BinomialNode[]nds=newBinomialNode[nodey.getDegree()];
  114. inti=0;
  115. while(nd!=null){
  116. nd.setParent(null);
  117. nds[i]=nd;
  118. nd=nd.getSibling();
  119. i++;
  120. }
  121. for(intj=0;j<nds.length-1;j++){
  122. nd=nds[nds.length-1-j];
  123. nd.setSibling(nds[nds.length-2-j]);
  124. }
  125. nds[0].setSibling(null);
  126. h1.setHead(nds[nds.length-1]);
  127. this.union(h1);
  128. }
  129. returnnodey;
  130. }
  131. publicvoiddecreaseKey(BinomialNodenodex,intk){
  132. if(k>nodex.getKey()){
  133. thrownewRuntimeException();
  134. }
  135. nodex.setKey(k);
  136. BinomialNodenodey=nodex;
  137. BinomialNodenodez=nodey.getParent();
  138. while(nodez!=null&&nodey.getKey()<nodez.getKey()){
  139. inttemp=nodey.getKey();
  140. nodey.setKey(nodez.getKey());
  141. nodez.setKey(temp);
  142. nodey=nodez;
  143. nodez=nodey.getParent();
  144. }
  145. }
  146. publicvoiddelete(BinomialNodenodex){
  147. decreaseKey(nodex,Integer.MIN_VALUE);
  148. extractMin();
  149. }
  150. publicstaticvoidmain(String[]args){
  151. BinomialHeapbh=newBinomialHeap();
  152. int[]keys={9,10,5,14,13,8,15,12,11,7,22,6};
  153. BinomialNodenode=null;
  154. for(inti=0;i<keys.length;i++){
  155. BinomialNodebn=newBinomialNode();
  156. bn.setKey(keys[i]);
  157. bh.insert(bn);
  158. node=bn;
  159. }
  160. bh.delete(node);
  161. System.out.println(bh.extractMin().getKey());
  162. System.out.println(bh.extractMin().getKey());
  163. }
  164. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值