You are on page 1of 6

package Arbori;

import java.io.*;
import Cozi.*;

public class ArboreBinar {


protected Object rad=null;
protected ArboreBinar fiu_stg=null;
protected ArboreBinar fiu_dr =null;

private Vizitator v;

private BufferedReader r=null;;


boolean tastatura=false;

public ArboreBinar()
{
v = new Afisare();
}

public ArboreBinar(Vizitator v)
{
this.v = v;
}
public ArboreBinar(InputStream is, Vizitator v)
{
this.v = v;
tastatura = is == System.in;
r = new BufferedReader( new InputStreamRea
der(is) );
constArbore( citesteInfoNod("","") , true
);
}
public ArboreBinar(InputStream is)
{
this(is, new Afisare());
}

public Object radacina()


{
return rad;
}
public ArboreBinar stg()
{
return fiu_stg;
}
public ArboreBinar dr()
{
return fiu_dr;
}

public boolean isEmpty()


{
return rad==null;
}
public boolean esteFrunza()
{
return fiu_stg==null && fiu_dr==null;
}

private ArboreBinar constArbore (String s, boolea


n radacina)
{
if(s.equals("")) return null;
ArboreBinar a = radacina ? this : new ArboreBi
nar(v);
a.rad=s;
a.fiu_stg= constArbore(citesteInfoNod("stang",
s), false);
a.fiu_dr = constArbore(citesteInfoNod("drept",
s), false);
return a;
}

private String citesteInfoNod(String fiu, String


tata)
{
String info;
if(tastatura) {
if(fiu=="") System.out.print("Radacina:");

else System.out.print("Introduceti fiul "+


fiu +
" al nodului "+ ta
ta + ":");
}
try {
info= r.readLine().trim();
} catch(Exception e) {
info="";
System.out.println(e);
}
return info;
}
public String toString()
{
return rad +" " +
(fiu_stg!=null?fiu_stg.toString():"")+
(fiu_dr!=null?fiu_dr.toString():"")
;
}
public void SRD()
{
if(fiu_stg!=null) fiu_stg.SRD();
v.vizitare(this);
if(fiu_dr!=null) fiu_dr.SRD();
}
public void RSD()
{
v.vizitare(this);
if(fiu_stg!=null) fiu_stg.RSD();
if(fiu_dr!=null) fiu_dr.RSD();
}
public void SDR()
{
if(fiu_stg!=null) fiu_stg.SDR();
if(fiu_dr!=null) fiu_dr.SDR();
v.vizitare(this);
}
public void latime()
{
CoadaVector q=new CoadaVector();
q.addq(this);
while(!q.este_vida()) {
ArboreBinar a = (ArboreBinar)q.front()
;
v.vizitare(a);
q.rem();
if(a.stg()!=null) q.addq(a.stg());
if(a.dr()!=null) q.addq(a.dr());
}
}
public void schimbaVizitator(Vizitator w)
{
v=w;
if(fiu_stg!=null) fiu_stg.schimbaVizitator(w)
;
if(fiu_dr!=null) fiu_dr.schimbaVizitator(w);
}

//---------------------------

public float eval(){


if (this.esteFrunza())
return Float.parseFloat(this.radacina().toS
tring());
else
{
switch(this.toString().charAt(0))
{
case '*': return this.fiu_stg.eval()*th
is.fiu_dr.eval();
//break;
case '+': return this.fiu_stg.eval()+t
his.fiu_dr.eval();
//break;
}
}
return 0;
}
}

//Afisare
package Arbori;

public class Afisare implements Vizitator {


public void vizitare(ArboreBinar a)
{
System.out.print(a.radacina()+" ");
}

//Vizitator
package Arbori;

public interface Vizitator {


public void vizitare(ArboreBinar o);
}

//Demo
package demo;

import Arbori.ArboreBinar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class A {

public static void main(String[] args) throws F


ileNotFoundException {

File f = new File("test.txt");


FileInputStream fs= new FileInputStream(f);

ArboreBinar a1 = new ArboreBinar(fs);


System.out.println("-Fisier-");
a1.RSD();

ArboreBinar a = new ArboreBinar(System.in);

System.out.println("\n-RSD-");
a.RSD();
System.out.println("\n\n-SDR-");
a.SDR();
System.out.println("\n\n-SRD-");
a.SRD();

System.out.println("\n\nRaspuns: " + a.eval


());