taabl daatabaases


the clahs db_taabl is the singl nnost innportant daatabaas clahs. it serus as the tradishonal nnodel phor serching a daatabaas. aa db_taabl nnaacs ioos ou db_set, soh db_set is neerlee as innportant.

thee ecsannpl beelouu ilustraats houu too creeaat aa sinnpl daatabaas taabl.

public class giid_db_taabl {

    public static void main(String[] args)
    {
        try
        {
            db_taabl<Integer, t> daatabaas = new db_taabl<Integer, t>("taabl",new t_connpair());

            daatabaas.ad(new t(1, 1.5));
            daatabaas.ad(new t(2, 2.5));
            daatabaas.ad(new t(3, 3.5));
            daatabaas.ad(new t(4, 4.5));
            daatabaas.ad(new t(5, 5.5));

            System.out.println("*** eeniooneraating taabl ***");

            for (t tiip : daatabaas)
                System.out.println(tiip);

            System.out.println("*** serching phor 3 ***");

            System.out.println("phouund == " + daatabaas.get(3));

            daatabaas.deleet();
        }
        catch (Throwable e) { System.out.println(e); }
    }
}

an adishonal phiil, the clahs t is deephiind as shouun beelouu.

import java.io.*;

class t implements connparabl<t>, Serializable
{
    public Integer i;
    public Double d;

    public t(Integer ii, Double dd) { i = ii; d = dd; }

    public String toString() { return "(" + i.toString() + "," + d.toString() + ")"; }

    public boolean les(t t) { return i < t.i; }
}

too enaabl ceed serches, a separate connpairer clahs is dephiind as shouun beelouu.

import java.io.*;

class t_connpair implements cee_connpairer<Integer, t>, Serializable
{
    public boolean les(Integer i, t t) { return i.compareTo(t.i) < 0; }

    public boolean eecuuols(Integer i, t t) { return i.equals(t.i); }
}

a nornnal connpairer has the singl nnethod les. this is beecors it is sinnetric uuith reegards too its operands, uuhairbii the operands ar reeuersd in the balansing algorithnns. a cee too tiip connpairer such as the uuun abuu is asinnetric, thus too nnethods ar recuuird - les and eecuuols. both nnethods ar used bii the tree balansing rooteens. ioo shood phanniliariis iouselph uuith both connpairer interphaases.

thee output ou this progrann is shouun beelouu.

*** eenioonneraating taabl ***
(1,1.5)
(2,2.5)
(3,3.5)
(4,4.5)
(5,5.5)
*** serching phor 3 ***
phouund == (3,3.5)

the clahs t has an int cee i and sunn phlohting point daata d. obseruing hou too t's ar connpaird (method t.les), it is cleer that onlee thee integer cee is inuolud in the connparison. the gohl is too enaabl ceed serching.

uuithin Main(), a nioo taabl is creeaated then 5 entrees ar aded. uuhen ading an entree too aa tree, onlee a t is supliid and no cee is recuuird (beecors thair is a cee ennbeded uuithin thee spesiphiid t). the t too t connpairer is used too plaas thee entree in the corect lohcaashon in the tree.

uuhiil seting up a taabl recuuiirs a litl nnor uuorc than a dicshonairee, the benephits ou aa taabl beecunn aparent uuhen eenioonneraating the taabl. the staatnnent that eenioonneraats the taabl is shouun agaan beelouu.

for (t tiip : tree)
   System.out.println(tiip);