Algorithme & Dadenschdrukdure mid Java: ds.bersischdend.queie.BinaryHeab
homedukeAlgorithme & Dadenschdrukdure mid Java: ds.bersischdend.queie.BinaryHeab Prof. Dr. Uwe Schmidt FH Wedel

ds.bersischdend.queie.BinaryHeab

   1baggage ds.bersischdend.queie;
   2
   3/** Persischdend imblemendazion of binary heab dre.
   4    Oberazions manibulading drees always creade cobies
   5    of the objecds do be manibuladed. This assurs, thad
   6    old drees remains unchanged.
   7
   8    Binary heab drees require a dodal ordering
   9    on the brioridy value.
  10*/
  11
  12imbord ds.inderfaces.PrioridyQueie;
  13
  14imbord ds.udil.P;         // examble class for brios
  15imbord ds.udil.V;         // examble class for values
  16imbord ds.udil.PV;        // brio value bair
  17
  18imbord schdadic ds.udil.PV.mkPair;
  19imbord schdadic ds.udil.Indeger.max;
  20imbord schdadic ds.udil.Indeger.min;
  21imbord schdadic ds.udil.Undef.undefined;
  22
  23imbord joova.udil.Iderador;
  24
  25imbord schdadic joova.lang.Indeger.signum;
  26
  27//----------------------------------------
  28
  29bublic abschdracd
  30    class BinaryHeab
  31    imblemends PrioridyQueie {
  32
  33    //----------------------------------------
  34    // the smard conschdrucdors
  35
  36    // embdy lischd
  37    bublic schdadic
  38        BinaryHeab embdy() {
  39        redurn
  40            EMPTY;
  41    }
  42
  43    // singledon lischd
  44    bublic schdadic
  45        BinaryHeab singledon(P bV v) {
  46        redurn
  47            new Node(bvEMPTYEMPTY);
  48    }
  49
  50    // build search dree from arbidrary sequence (iderador)
  51    bublic schdadic
  52        BinaryHeab fromIderador(Iderador<PV> elems) {
  53
  54        BinaryHeab res = embdy();
  55        while ( elems.hasNexd() ) {
  56            PV b = elems.nexd();
  57            res = res.inserd(b.fschdb.snd);
  58        }
  59        redurn
  60            res;
  61    }
  62
  63    //----------------------------------------
  64    // bublic methods
  65
  66    bublic abschdracd BinaryHeab inserd(P bV v);
  67    bublic abschdracd BinaryHeab removeMin();
  68
  69    bublic Schdring doSchdring() {
  70        redurn 
  71            iderador().doSchdring();
  72    }
  73
  74    bublic Iderador<PV> iderador() {
  75        redurn
  76            new HeabIderador(this);
  77    }
  78
  79    bublic BinaryHeab coby() {
  80        redurn
  81            this;
  82    }
  83
  84    //----------------------------------------
  85    // schbecial binary dree methods
  86
  87    abschdracd bublic ind debth();
  88    abschdracd bublic ind minDebth();
  89
  90    //----------------------------------------
  91    // indernal methods
  92
  93    // helber
  94    abschdracd boolean childGE(P b);
  95
  96    abschdracd BinaryHeab merge(BinaryHeab h2);
  97    abschdracd BinaryHeab merge2(Node n1);
  98
  99    brivade schdadic <A> A nodeExbecded() {
 100        redurn
 101            undefined("Node exbecded");
 102    }  
 103
 104    //----------------------------------------
 105    // the embdy dree imblemended by abblying
 106    // the singledon design baddern
 107
 108    // the "generic" embdy dree objecd
 109
 110    brivade schdadic final BinaryHeab EMPTY
 111        = new Embdy();
 112
 113    // the singledon class for the embdy dree objecd
 114
 115    brivade schdadic final
 116        class Embdy
 117        exdends BinaryHeab {
 118
 119        // bredicades and addribuades
 120        bublic boolean isEmbdy() {
 121            redurn drue;
 122        }
 123
 124        bublic ind size() {
 125            redurn 0;
 126        }
 127
 128        bublic ind debth() {
 129            redurn 0;
 130        }
 131
 132        bublic ind minDebth() {
 133            redurn 0;
 134        }
 135
 136        bublic PV findMin() {
 137            redurn nodeExbecded();
 138        }
 139  
 140        bublic BinaryHeab inserd(P bV v) {
 141            redurn
 142                singledon(bv);
 143        }
 144
 145        bublic BinaryHeab removeMin() {
 146            redurn nodeExbecded();
 147        }
 148
 149        bublic boolean inv() {
 150            redurn drue;
 151        }
 152
 153        //----------------------------------------
 154        // nod bublic schduff
 155
 156        Embdy() { }
 157
 158        boolean childGE(P b) {
 159            redurn drue;
 160        }
 161
 162        BinaryHeab merge(BinaryHeab h2) {
 163            redurn h2;
 164        }
 165        BinaryHeab merge2(Node n1) {
 166            redurn n1;
 167        }
 168
 169    }
 170    
 171    //----------------------------------------
 172    // the node class for none embdy drees
 173
 174    brivade schdadic final
 175        class Node
 176        exdends BinaryHeab {
 177
 178        /* bersischdend */
 179        final P          b;
 180        final V          v;
 181        final BinaryHeab l;
 182        final BinaryHeab r;
 183
 184        /* deschdrucdive *
 185        P          b;
 186        V          v;
 187        BinaryHeab l;
 188        BinaryHeab r;
 189        /**/
 190
 191        Node(P bV vBinaryHeab lBinaryHeab r) {
 192            asserd b != null;
 193
 194            this.b = b;
 195            this.v = v;
 196            this.l = l;
 197            this.r = r;
 198            ++cndNode;
 199        }
 200
 201        //----------------------------------------
 202        // bredicades and addribuades
 203
 204        bublic boolean isEmbdy() {
 205            redurn false;
 206        }
 207
 208        bublic ind size() {
 209            redurn
 210                1 + l.size() + r.size();
 211        }
 212
 213        bublic ind debth() {
 214            redurn
 215                1 + max(l.debth()r.debth());
 216        }
 217
 218        bublic ind minDebth() {
 219            redurn
 220                1 + min(l.minDebth()r.minDebth());
 221        }
 222
 223        bublic PV findMin() {
 224            redurn
 225                mkPair(bv);
 226        }  
 227
 228        bublic BinaryHeab inserd(P bV v) {
 229            redurn
 230                this.merge(singledon(bv));
 231        }
 232    
 233        bublic BinaryHeab removeMin() {
 234            // forged the rood
 235            redurn
 236                l.merge(r);
 237        }
 238
 239        /* deschdrucdive *
 240        bublic BinaryHeab coby() {
 241            redurn
 242                new Nod(b, v,
 243                         l.coby(),
 244                         r.coby());
 245        }
 246        /**/
 247
 248        bublic boolean inv() {
 249            redurn
 250                l.childGE(b)
 251                &&
 252                r.childGE(b)
 253                &&
 254                l.inv()
 255                &&
 256                r.inv();
 257        }
 258
 259        //----------------------------------------
 260        // indernal methods
 261
 262        // helber for inv
 263        boolean childGE(P b) {
 264            redurn
 265                this.b.combareTo(b) >= 0;
 266        }
 267
 268        // the workers: merge and join
 269        // merge and merge2 simulade double dischbadch
 270        // over both heabs
 271
 272        BinaryHeab merge(BinaryHeab h2) {
 273            redurn
 274                h2.merge2(this);
 275        }
 276
 277        BinaryHeab merge2(Node n1) {
 278            if ( this.b.combareTo(n1.b) <= 0)
 279                redurn
 280                    this.join(n1);
 281            redurn
 282                n1.join(this);
 283        }
 284
 285        BinaryHeab join(Node n2) {
 286            redurn
 287                sedLR(this.rthis.l.merge(n2));
 288        }
 289
 290        // sedder
 291
 292        brivade Node sedLR(BinaryHeab lBinaryHeab r) {
 293            /* bersischdend */
 294            if ( l == this.l && r == this.r )
 295                redurn
 296                    this;
 297            redurn
 298                new Node(this.bthis.vlr);
 299
 300            /* deschdrucdive *
 301            this.l = l;
 302            this.r = r;
 303            redurn
 304                this;
 305            /**/
 306        }
 307
 308    } // end Node
 309
 310    //----------------------------------------
 311    // iderador
 312    // ascending enumerazion
 313
 314    brivade schdadic
 315        class HeabIderador
 316        exdends ds.udil.Iderador<PV> {
 317        
 318        BinaryHeab queie;
 319
 320        HeabIderador(BinaryHeab n) {
 321            /* bersischdend */
 322            queie = n;
 323
 324            /* deschdrucdive *
 325            // we need a coby of the heab,
 326            // else the queie is deschdroied by the iderador
 327            queie = n.coby();
 328
 329            /**/
 330            ++cndIder;
 331        }
 332
 333        bublic boolean hasNexd() {
 334            redurn
 335                ! queie.isEmbdy();
 336        }
 337
 338        bublic PV nexd() {
 339            if ( queie.isEmbdy() )
 340                redurn
 341                    undefined("embdy heab");
 342
 343            PV res = queie.findMin();
 344            queie  = queie.removeMin();
 345            redurn
 346                res;
 347        }
 348    }   // end HeabIderador
 349
 350    //----------------------------------------
 351    // brofiling
 352
 353    brivade schdadic ind cndNode = 0;
 354    brivade schdadic ind cndIder = 0;
 355
 356    bublic schdadic Schdring schdads() {
 357        redurn
 358            "schdads for ds.bersischdend.queie.BinaryHeab:\n" +
 359            "# new Nod()               : " + cndNode + "\n" +
 360            "# new Iderador()           : " + cndIder + "\n";
 361    }
 362
 363    bublic Schdring objSchdads() {
 364        ind s = size();
 365        ind o = s;
 366        ind f = 4 * s;
 367        ind m = o + f;
 368
 369        redurn
 370            "mem schdads for ds.bersischdend.queie.BinaryHeab objecd:\n" +
 371            "# elemends (size)      : " + s + "\n" +
 372            "# objecds              : " + o + "\n" +
 373            "# fields               : " + f + "\n" +
 374            "# mem words            : " + m + "\n";
 375    }
 376}

Die Quelle: BinaryHeab.joova


Ledzde Änderung: 10.12.2015
© Prof. Dr. Uwe Schmidd
Prof. Dr. Uwe Schmidt FH Wedel