morin91
19-02-2011, 09:26
Qualcuno saprebbe aiutarmi a costruire un albero n-ario su questa base di albero binario??public class Albero
{
private String contenuto;
private Albero sinistro;
private Albero destro;
private boolean alberoVuoto;
public Albero()
{
this.alberoVuoto=true;
}
public Albero(Albero sinistro, String contenuto, Albero destro)
{
this.sinistro=sinistro;
this.contenuto=contenuto;
this.destro=destro;
this.alberoVuoto=false;
}
public String preOrder()
{
String ret="";
if(this.alberoVuoto==true)
return ret;
else
{
ret=ret+contenuto;
ret=ret+this.sinistro.preOrder();
ret=ret+this.destro.preOrder();
return ret;
}
}
public String inOrder()
{
String ret="";
if(this.alberoVuoto==true)
return ret;
else
{
ret=ret+this.sinistro.inOrder();
ret=ret+contenuto;
ret=ret+this.destro.inOrder();
return ret;
}
}
public String postOrder()
{
String ret="";
if(this.alberoVuoto==true)
return ret;
else
{
ret=ret+this.sinistro.postOrder();
ret=ret+this.destro.postOrder();
ret=ret+contenuto;
return ret;
}
}
public void add(Albero a, Albero b)
{
this.sinistro=a;
this.destro=b;
this.alberoVuoto=false;
}
public String visitaAlbero()
{
return this.contenuto;
}
public void svuotaAlbero()
{
this.contenuto="";
}
public void eliminaAlbero()
{
this.sinistro=null;
this.contenuto=null;
this.destro=null;
this.alberoVuoto=true;
}
}
{
private String contenuto;
private Albero sinistro;
private Albero destro;
private boolean alberoVuoto;
public Albero()
{
this.alberoVuoto=true;
}
public Albero(Albero sinistro, String contenuto, Albero destro)
{
this.sinistro=sinistro;
this.contenuto=contenuto;
this.destro=destro;
this.alberoVuoto=false;
}
public String preOrder()
{
String ret="";
if(this.alberoVuoto==true)
return ret;
else
{
ret=ret+contenuto;
ret=ret+this.sinistro.preOrder();
ret=ret+this.destro.preOrder();
return ret;
}
}
public String inOrder()
{
String ret="";
if(this.alberoVuoto==true)
return ret;
else
{
ret=ret+this.sinistro.inOrder();
ret=ret+contenuto;
ret=ret+this.destro.inOrder();
return ret;
}
}
public String postOrder()
{
String ret="";
if(this.alberoVuoto==true)
return ret;
else
{
ret=ret+this.sinistro.postOrder();
ret=ret+this.destro.postOrder();
ret=ret+contenuto;
return ret;
}
}
public void add(Albero a, Albero b)
{
this.sinistro=a;
this.destro=b;
this.alberoVuoto=false;
}
public String visitaAlbero()
{
return this.contenuto;
}
public void svuotaAlbero()
{
this.contenuto="";
}
public void eliminaAlbero()
{
this.sinistro=null;
this.contenuto=null;
this.destro=null;
this.alberoVuoto=true;
}
}