Article View: borland.public.jbuilder.java.api
Article #3041Re: Problems implementing an AVL Tree ...
From: Clint Brewer
Date: Wed, 03 Jun 1998 00:00
Date: Wed, 03 Jun 1998 00:00
136 lines
4346 bytes
4346 bytes
Gustavo wrote:
>
> I am writing a class that functions like an AVL Tree, I wrote it in Delphi
> and it functions well, the problem is :
>
> when I want to insert a value I pass through the nodes(left or right
> child), when I pass to the function the null node, then it is the time to
> create a node, the problem is that the new object that I create does not
> reference the one I pass to the function.
> here is the code :
>
> This is the node component
>
> public class TTermo
> {
> //---------------------------------------------------------
> // vari�veis
> protected String FTexto;
> protected int FBalanceamento;
> public TTermo NoDireito;
> public TTermo NoEsquerdo;
> boolean FPreenchido;
>
> //---------------------------------------------------------
> // m�todos publicos
> public void setaTexto(String valor)
> {
> FTexto = valor;
> FPreenchido = true;
> }
>
> //---------------------------------------------------------
> public String pegaTexto()
> {
> return FTexto;
> }
>
> //---------------------------------------------------------
> public void setaBalanceamento(int valor)
> {
> FBalanceamento = valor;
> }
>
> //---------------------------------------------------------
> public int pegaBalanceamento()
> {
> return FBalanceamento;
> }
>
> //---------------------------------------------------------
> public void setaPreenchido(boolean valor)
> {
> FPreenchido = valor;
> }
>
> //---------------------------------------------------------
> public boolean pegaPreenchido()
> {
> return FPreenchido;
> }
>
> //---------------------------------------------------------
> // construtores
>
> public TTermo()
> {
>
> NoDireito = null;
> NoEsquerdo = null;
>
> FTexto = "";
> FBalanceamento = 0;
> }
>
> }
>
> This is the function to insert:
>
> protected void insert(String valor ,TTermo no ,Boolean h)
> {
>
> String auxtexto;
>
> if (no == null )
>
> FTermoIns = new TTermo();
> FTermoIns.setaTexto(valor);
> h = Boolean.TRUE;
> FNumeroTermos ++;
> no = FTermoIns;
> }
>
> else
> {
> // para facilitar o entendimento
> auxtexto = no.pegaTexto().toLowerCase();
> if (valor.toLowerCase().compareTo(auxtexto) < 0)
> {
> retorno = insert(valor,no.NoEsquerdo, h);
> if (h == Boolean.TRUE )
> balanceEsquerdaIns(no, h);
> }
> else
> if (valor.toLowerCase().compareTo(auxtexto) > 0)
> {
> insert(valor,no.NoDireito, h);
> if (h == Boolean.TRUE)
> balanceDireitaIns(no , h);
> }
> }
>
> }
>
> ----------------------------------------------------------------------------
> ----------------
> Gustavo Dutra Martins Interbrain Sistemas
> 327-5296 205-0143
> dutra@trip.com.br gustavo@interbrain.com.br
> ----------------------------------------------------------------------------
> ---------------
Hello Gustavo,
Well, just looking at the recursive insert function you posted,
it looks like it should work fine. In the calls
to "protected void insert(String valor ,TTermo no ,Boolean h)"
'TTermo no' should be a member of another node that is actually
set to null currently, so when you create the new node, and set
'TTermo no' to refer to it, you have in fact plugged the node into the
tree. Therefore 'TTermo no' is the reference back to the
tree, and you shouldn't need to pass anything else.
Hope that helps,
Clint Brewer
Technical Details
Message-ID:
<3575BAB0.5242@borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<6kcfmt$6gm6@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1089d0,aa7241afe30c6361 X-Google-Attributes: gid1089d0,public From: Clint Brewer <supportinfo@borland.com> Subject: Re: Problems implementing an AVL Tree ... Date: 1998/06/03 Message-ID: <3575BAB0.5242@borland.com> X-Deja-AN: 359266091 Content-Transfer-Encoding: 8bit References: <6kcfmt$6gm6@forums.borland.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Organization: Borland International Newsgroups: borland.public.jbuilder.java.api