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 / StabloProgram.java
1 import java.util.ArrayList;
2 import java.util.List;
4 class Stablo {
6 public final int id;
7 public String vrednost;
8 public Stablo levi;
9 public Stablo desni;
11 public Stablo(int id) {
12 this(id, null);
13 }
15 public Stablo(int id, String vrednost) {
16 this(id, vrednost, null, null);
17 }
19 public Stablo(int id, String vrednost, Stablo levi, Stablo desni) {
20 this.id = id;
21 this.vrednost = vrednost;
22 this.levi = levi;
23 this.desni = desni;
24 }
26 @Override
27 public String toString() {
28 return "(" + id + " \"" + vrednost + "\"" + (levi == null ? "" : " " + levi) + (desni == null ? "" : " " + desni) + ")";
29 }
30 }
32 public class StabloProgram {
34 public static void main(String[] args) {
36 // Ucitavanje stabla
37 StabloIO inIO = new StabloIORandom(12345);
38 Stablo stablo = inIO.readStablo(null);
40 // Ispisivanje stabla na ekran
41 StabloIO outIO = new StabloIOPretty();
42 outIO.printStablo(Svetovid.out, stablo);
43 Svetovid.out.println();
45 // Broj nivoa
46 int brojNivoa = brojNivoa(stablo);
47 Svetovid.out.println("Broj nivoa:", brojNivoa);
48 Svetovid.out.println();
50 // Broj nivoa
51 int brojElementata = brojElementata(stablo);
52 Svetovid.out.println("Broj elementata:", brojElementata);
53 Svetovid.out.println();
55 // Broj nivoa
56 String najduzaVrednost = najduzaVrednost(stablo);
57 Svetovid.out.println("Najduza vrednost:", najduzaVrednost);
58 Svetovid.out.println();
60 // Svi Putevi od korena do listova
61 sviPutevi(stablo, new ArrayList<>(brojNivoa));
62 Svetovid.out.println();
64 // Obrnuto stablo
65 Stablo obrnuto = obrni(stablo);
66 outIO.printStablo(Svetovid.out, obrnuto);
67 Svetovid.out.println();
69 }
71 private static int brojElementata(Stablo stablo) {
72 if (stablo == null) {
73 return 0;
74 }
75 return 1 + brojElementata(stablo.levi) + brojElementata(stablo.desni);
76 }
78 private static int brojNivoa(Stablo stablo) {
79 if (stablo == null) {
80 return 0;
81 }
82 return 1 + Math.max(brojNivoa(stablo.levi), brojNivoa(stablo.desni));
83 }
85 private static String najduzaVrednost(Stablo stablo) {
86 if (stablo == null) {
87 return null;
88 }
89 String rezultat = stablo.vrednost;
90 String levaVrednost = najduzaVrednost(stablo.levi);
91 String desnaVrednost = najduzaVrednost(stablo.desni);
92 if (rezultat == null || (levaVrednost != null && rezultat.length() < levaVrednost.length())) {
93 rezultat = levaVrednost;
94 }
95 if (rezultat == null || (desnaVrednost != null && rezultat.length() < desnaVrednost.length())) {
96 rezultat = desnaVrednost;
97 }
98 return rezultat;
99 }
101 private static void sviPutevi(Stablo stablo, List<String> put) {
102 if (stablo == null) {
103 return;
105 put.add(stablo.vrednost);
106 if ((stablo.levi == null) && (stablo.desni == null)) {
107 Svetovid.out.println("Put: " + put);
109 sviPutevi(stablo.levi, put);
110 sviPutevi(stablo.desni, put);
111 put.remove(put.size() - 1);
114 private static Stablo obrni(Stablo stablo) {
115 if (stablo == null) {
116 return null;
118 Stablo levi = obrni(stablo.levi);
119 Stablo desni = obrni(stablo.desni);
120 Stablo obrnuto = new Stablo(stablo.id, stablo.vrednost, desni, levi);
121 return obrnuto;
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner