public class SPole { Polozka[] polozky; int pocet; public SPole() { this.pocet = 0; //this.polozky = nul; } private void promen(int j, int k) { if (this.jePrazdna()) return; Polozka pom = this.polozky[j]; this.polozky[j] = this.polozky[k]; this.polozky[k] = pom; } private void dolu(int k, int pocet) { if (this.jePrazdna()) return; while (2*k <= pocet) { int j = 2*k; if (j < pocet && this.polozky[j].cena > this.polozky[j+1].cena) j++; if (this.polozky[k].cena <= this.polozky[j].cena) break; this.promen(j,k); k = j; } } private void nahoru(int k) { if (this.jePrazdna()) return; while (k > 1 && this.polozky[k/2].cena > this.polozky[k].cena) { System.out.println(k+" "+(k/2)); this.promen(k,k/2); k = k/2; } } public boolean jePrazdna() { return (pocet == 0); } public void vloz(int cena, int dodavatel) { // Zvetseni pole Polozka[] novePole = new Polozka[++this.pocet]; if (this.pocet != 1) System.arraycopy(this.polozky,0,novePole,0,this.pocet-1); novePole[pocet-1] = new Polozka(cena, dodavatel); this.polozky = novePole; nahoru(this.pocet-1); } public Polozka vratNejmensi() { if (!jePrazdna()) return this.polozky[0]; return null; } public Polozka vymazNejmensi() { if (this.jePrazdna()) return null; Polozka nejmensiPolozka = this.polozky[0]; this.promen(0,this.pocet-1); //dolu(0,this.pocet-1); // Zmenseni pole Polozka[] novePole = new Polozka[--this.pocet]; System.arraycopy(this.polozky,0,novePole,0,this.pocet); this.polozky = novePole; this.nahoru(this.pocet-1); return nejmensiPolozka; } public void println() { for(int i=1; i<=this.pocet; i++) { this.polozky[i].println(); } } }