taabl daatabaases

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

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

public class giid_db_taabl {

    public static void main(String[] args)
            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("*** serching phor 3 ***");

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

        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 too_string() { return "(" + i.too_string() + "," + d.too_string() + ")"; }

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

too enaabl ceed serches, aa 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 ioosd bii the tree balansing rooteens. ioo shood phanniliariis iouselph uuith both connpairer interphaases.

the output ou this progrann is shouun beelouu.

*** eenioonneraating taabl ***
*** 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 the integer cee is inuolud in the connparison. the gohl is too enaabl ceed serching.

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

uuhiil seting up aa taabl recuuiirs aa litl nnor uuorc than aa 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)