gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Stabla, dodati primeri algoritama nad stablima
[spa2-materijali.git] / Stabla / Primeri za test / StabloIOIndent.java
1 import org.svetovid.SvetovidFormatException;
2 import org.svetovid.io.SvetovidReader;
3 import org.svetovid.io.SvetovidWriter;
5 /**
6 * Format:
7 *
8 * id vrednost
9 * levi
10 * desni
11 */
12 public class StabloIOIndent implements StabloIO {
14 protected String nullSymbol;
15 protected String indent;
17 public StabloIOIndent() {
18 this("-", " ");
19 }
21 public StabloIOIndent(String nullSymbol, String indent) {
22 this.nullSymbol = nullSymbol;
23 this.indent = indent;
24 }
26 public String getNullSymbol() {
27 return nullSymbol;
28 }
30 public void setNullSymbol(String nullSymbol) {
31 this.nullSymbol = nullSymbol;
32 }
34 public String getIndent() {
35 return indent;
36 }
38 public void setIndent(String indent) {
39 this.indent = indent;
40 }
42 @Override
43 public Stablo readStablo(SvetovidReader in) {
44 try {
45 int id = in.readInt();
46 String vrednost = in.readLine();
47 Stablo levi = readStablo(in);
48 Stablo desni = readStablo(in);
49 Stablo stablo = new Stablo(id, vrednost, levi, desni);
50 return stablo;
51 } catch (SvetovidFormatException e) {
52 return null;
53 }
54 }
56 @Override
57 public void printStablo(SvetovidWriter out, Stablo stablo) {
58 write(out, stablo, nullSymbol, indent, "");
59 }
61 protected void write(SvetovidWriter out, Stablo stablo, String nullSymbol, String indent, String prefix) {
62 if (stablo == null) {
63 out.println(prefix + nullSymbol);
64 return;
65 }
66 int id = stablo.id;
67 String vrednost = stablo.vrednost;
68 out.print(prefix);
69 out.println(id, vrednost);
70 write(out, stablo.levi, nullSymbol, indent, prefix + indent);
71 write(out, stablo.desni, nullSymbol, indent, prefix + indent);
72 }
73 }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner