算法之二项堆
来源:互联网 发布:儿童读物在线阅读软件 编辑:程序博客网 时间:2024/05/17 08:10
- package com.eshore.sweetop.exdataframe;
- public class BinomialHeap {
- private BinomialNode head;
- public BinomialNode getHead() {
- return head;
- }
- private void setHead(BinomialNode head) {
- this.head = head;
- }
- public BinomialNode min() {
- BinomialNode nodey = null;
- BinomialNode nodex = head;
- int min = Integer.MAX_VALUE;
- while (nodex != null) {
- if (nodex.getKey() < min) {
- min = nodex.getKey();
- nodey = nodex;
- }
- nodex = nodex.getSibling();
- }
- return nodey;
- }
- public void link(BinomialNode nodey, BinomialNode nodez) {
- nodey.setParent(nodez);
- nodey.setSibling(nodez.getChild());
- nodez.setChild(nodey);
- nodez.setDegree(nodez.getDegree() + 1);
- }
- public void union(BinomialHeap h2) {
- this.setHead(merge(this, h2));
- BinomialNode px = null;
- BinomialNode x = this.getHead();
- BinomialNode nx = x.getSibling();
- while (nx != null) {
- if (x.getDegree() != nx.getDegree()
- || (nx.getSibling() != null && nx.getSibling().getDegree() == x
- .getDegree())) {
- px = x;
- x = nx;
- } else if (x.getKey() <= nx.getKey()) {
- x.setSibling(nx.getSibling());
- link(nx, x);
- } else {
- if (px == null) {
- this.setHead(nx);
- } else {
- px.setSibling(nx);
- }
- link(x, nx);
- x = nx;
- }
- nx = x.getSibling();
- }
- }
- public void insert(BinomialNode x) {
- BinomialHeap h = new BinomialHeap();
- x.setParent(null);
- x.setChild(null);
- x.setSibling(null);
- x.setDegree(0);
- h.setHead(x);
- union(h);
- }
- public BinomialNode merge(BinomialHeap h1, BinomialHeap h2) {
- BinomialNode head1 = h1.getHead();
- BinomialNode head2 = h2.getHead();
- BinomialNode nd = new BinomialNode();
- BinomialNode node = nd;
- while (!(head1 == null && head2 == null)) {
- if (head1 == null) {
- node.setSibling(head2);
- head2 = head2.getSibling();
- } else if (head2 == null) {
- node.setSibling(head1);
- head1 = head1.getSibling();
- } else {
- if (head1.getDegree() < head2.getDegree()) {
- node.setSibling(head1);
- head1 = head1.getSibling();
- } else {
- node.setSibling(head2);
- head2 = head2.getSibling();
- }
- }
- node = node.getSibling();
- }
- return nd.getSibling();
- }
- public BinomialNode extractMin() {
- BinomialNode nodep = null;
- BinomialNode nodey = null;
- BinomialNode nodex = head;
- BinomialNode nodeyp = null;
- int min = Integer.MAX_VALUE;
- while (nodex != null) {
- if (nodex.getKey() < min) {
- min = nodex.getKey();
- nodey = nodex;
- nodeyp = nodep;
- }
- nodep = nodex;
- nodex = nodex.getSibling();
- }
- if (nodey != null) {
- if (nodeyp == null) {
- this.setHead(nodey.getSibling());
- } else {
- nodeyp.setSibling(nodey.getSibling());
- }
- BinomialHeap h1 = new BinomialHeap();
- BinomialNode nd=nodey.getChild();
- nodey.setChild(null);
- BinomialNode[] nds=new BinomialNode[nodey.getDegree()];
- int i=0;
- while(nd!=null){
- nd.setParent(null);
- nds[i]=nd;
- nd=nd.getSibling();
- i++;
- }
-
- for (int j = 0; j < nds.length-1; j++) {
- nd=nds[nds.length-1-j];
- nd.setSibling(nds[nds.length-2-j]);
- }
- nds[0].setSibling(null);
- h1.setHead(nds[nds.length-1]);
- this.union(h1);
- }
- return nodey;
- }
- public void decreaseKey(BinomialNode nodex,int k){
- if(k>nodex.getKey()){
- throw new RuntimeException();
- }
- nodex.setKey(k);
- BinomialNode nodey=nodex;
- BinomialNode nodez=nodey.getParent();
- while(nodez!=null && nodey.getKey()<nodez.getKey()){
- int temp=nodey.getKey();
- nodey.setKey(nodez.getKey());
- nodez.setKey(temp);
- nodey=nodez;
- nodez=nodey.getParent();
- }
- }
-
- public void delete(BinomialNode nodex){
- decreaseKey(nodex,Integer.MIN_VALUE);
- extractMin();
- }
-
- public static void main(String[] args) {
- BinomialHeap bh = new BinomialHeap();
- int[] keys = { 9, 10, 5, 14, 13, 8 ,15,12,11,7,22,6};
- BinomialNode node=null;
- for (int i = 0; i < keys.length; i++) {
- BinomialNode bn = new BinomialNode();
- bn.setKey(keys[i]);
- bh.insert(bn);
- node=bn;
- }
- bh.delete(node);
- System.out.println(bh.extractMin().getKey());
- System.out.println(bh.extractMin().getKey());
- }
- }