public int extractMin() throws Exception {
        if (this.lastFreePos == 0) {
            throw new Exception("Empty heap");
        }
        int returnValue = this.values[0], aux;
        this.values[0] = this.values[this.lastFreePos - 1];
        this.values[this.lastFreePos - 1] = 0;
        this.lastFreePos--;
        
        /* bubble down */
        int parentPos = 0, leftChildPos = (parentPos + 1) * 2 - 1, rightChildPos = leftChildPos + 1, minChildPos;
        while ((leftChildPos < this.lastFreePos && this.values[parentPos] > this.values[leftChildPos])
                || (rightChildPos < this.lastFreePos && this.values[parentPos] > this.values[rightChildPos])) {
            minChildPos = leftChildPos;
            if (rightChildPos < this.lastFreePos && this.values[leftChildPos] > this.values[rightChildPos]) {
                minChildPos = rightChildPos;
            }
            aux = this.values[parentPos];
            this.values[parentPos] = this.values[minChildPos];
            this.values[minChildPos] = aux;
            parentPos = minChildPos;
            leftChildPos = (parentPos + 1) * 2 - 1;
            rightChildPos = leftChildPos + 1;
        }
        return returnValue;
    }