private void yhdistaKeot(Binomikeko b) { BinomiMerge(b); if (this.head == null) { return; } BinomiNode prevX = null; BinomiNode x = this.head; BinomiNode nextX = x.getSisar(); while (nextX != null) { if (x.getAste() != nextX.getAste() || (nextX.getSisar() != null && nextX.getSisar().getAste() == x.getAste())) { prevX = x; x = nextX; } else if (x.getArvo() <= nextX.getArvo()) { // x:n ja nextx:n asteet samat x.setSisar(nextX.getSisar()); nextX.Yhdista(x); } else { if (prevX == null) { this.head = nextX; } else { prevX.setSisar(nextX); } x.Yhdista(nextX); x = nextX; } nextX = x.getSisar(); } }
private void BinomiMerge(Binomikeko hb) { BinomiNode a = this.head; BinomiNode b = hb.head; this.head = minAste(a, b); if (this.head == null) { return; } if (this.head == b) { b = a; } a = this.head; while (b != null) { if (a.getSisar() == null) { a.setSisar(b); return; } else if (a.getSisar().getAste() < b.getAste()) { a = a.getSisar(); } else { BinomiNode c = b.getSisar(); b.setSisar(a.getSisar()); a.setSisar(b); a = a.getSisar(); b = c; } } }
private int extractMin() { // extract-Min BinomiNode minEd = null; int min = Integer.MAX_VALUE; Binomikeko tmp = new Binomikeko(); BinomiNode edellinen; BinomiNode i = this.head; if (i == null) { throw new EmptyStackException(); } edellinen = null; do { if (i.getArvo() <= min) { min = i.getArvo(); minEd = edellinen; } edellinen = i; i = i.getSisar(); } while (i != null); BinomiNode poistettava; if (minEd == null) { // head on ainoa jolla ei isosiskoa poistettava = this.head; this.head = poistettava.getSisar(); } else { poistettava = minEd.getSisar(); minEd.setSisar(poistettava.getSisar()); } BinomiNode l = poistettava.getlapsi(); if (l == null) { return min; } // on lapsia, yhdistetään ne while (l.getSisar() != null) { BinomiNode next = l.getSisar(); l.setSisar(tmp.head); tmp.head = l; l = next; } l.setSisar(tmp.head); tmp.head = l; this.yhdistaKeot(tmp); return min; }