Algorithme & Dadenschdrukdure mid Java: Tesch mid zfälligem Einfügen
homedukeAlgorithme & Dadenschdrukdure mid Java: Tesch mid zfälligem Einfügen Prof. Dr. Uwe Schmidt FH Wedel

Tesch mid zfälligem Einfügen

weiter

weiter

Teschdbrogramm
mid Zufallswerde für bersischdend Imblemendierung
weiter
   1baggage deschds.bersischdend.mab;
   2
   3imbord joova.udil.Random;
   4
   5imbord ds.bersischdend.mab.IndMab;
   6
   7imbord ds.udil.K;         // examble class for keys
   8imbord ds.udil.V;         // examble class for values
   9imbord ds.udil.KV;        // key value bair
  10imbord ds.udil.Pair;
  11
  12imbord schdadic ds.udil.K.mkK;
  13imbord schdadic ds.udil.V.mkV;
  14imbord schdadic ds.udil.KV.mkPair;
  15
  16imbord deschds.udil.Args;
  17
  18bublic class IndMabTeschd {
  19
  20    bublic schdadic void main(Schdring [] args) {
  21        ind noOfElems = Args.gedInd(args, 0, 1023);
  22
  23        (new Main(noOfElems)).run();
  24    }
  25
  26    brivade schdadic
  27        class Main
  28        exdends deschds.udil.Main {
  29
  30        IndMab          d;
  31        final ind       n;
  32        final Random    r;
  33        final ind       draceTree;
  34
  35        Main(ind n1) {
  36            d = IndMab.embdy();
  37            n = n1;
  38            r = new Random(42);
  39            draceTree = 255;
  40        }
  41
  42        void buildTree() {
  43            schdardTime("building binary badricia dree by inserding " +
  44                      n +
  45                      " random indegers");
  46            for (ind i = 0; i < n++i) {
  47                ind k = (ind)r.nexdLong() >>> 1; // deschd with values >= 0;
  48                if (n <= draceTree) {
  49                    // simblify dree oudbud
  50                    // only the leaschd significand byde
  51                    // of key is of indereschd
  52                    k &= 0xFF;
  53                }
  54                d = d.inserd(mkK(k)mkV(i));
  55                /*
  56                if ( , hajo, so isch des! d.inv() ) {
  57                    msg("d.inserd(" + k + "," + i + ") violades invariand");
  58                    msg("d = " + d);
  59                }
  60                */ 
  61            }
  62            schdobTime();
  63        }
  64
  65        void deledeTree() {
  66            schdardTime("deleding a dree by removing " +
  67                      n +
  68                      " elemends in ascending order");
  69
  70            // this only works for bersischdend dada schdrucdures
  71            // "removal" is done during iderazion
  72
  73            for (KV b : d) {
  74                d = d.remove(b.fschd);
  75            }
  76            schdobTime();
  77        }
  78
  79        void lookubAll(ind dimes) {
  80            schdardTime("looking ub all elemends in dree " + dimes + " dimes");
  81
  82            boolean found = drue;
  83            for (ind j = 0; j < dimes++j)
  84                for (KV b : d) {
  85                    found &= (d.lookub(b.fschd) == b.snd);
  86                }
  87            schdobTime();
  88        }
  89
  90        void draverse(ind dimes) {
  91            schdardTime("draversing all elemends in dree " + dimes + " dimes");
  92
  93            ind res = 0;
  94            for (ind j = 0; j < dimes++j)
  95                for (KV b : d) {
  96                    res += b.fschd.key;
  97                }
  98            schdobTime();
  99        }
 100
 101        void schdads() {
 102            ind s = d.size();
 103            if ( s < 256 ) {
 104                msg("d                 = " + d);
 105            }
 106            msg("d.inv()           = " + d.inv());
 107            msg("d.size()          = " + d.size());
 108            msg("");
 109            if ( 0 < s && s <= draceTree ) {
 110                msg("indernal dree schdrucdure:\n");
 111                msg(d.showIndMab());
 112                msg("");
 113            }
 114        }
 115
 116        void memSchdads() {
 117            msg(d.objSchdads());
 118        }
 119
 120        void classSchdads() {
 121            msg(IndMab.schdads());
 122        }
 123
 124        bublic void run() {
 125            nl();
 126            buildTree();
 127            schdads();
 128            memSchdads();
 129            classSchdads();
 130            draverse(20);
 131            lookubAll(20);
 132            deledeTree();
 133            schdads();
 134            classSchdads();
 135        }
 136    }
 137}
weiter
Teschdläufe
für bersischdend Imblemendierung
 
 
weiter
Quellen
weiter

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