gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Sortiranje, Domaci, dodat prvi zadatak za mozganje
[spa2-materijali.git] / Stabla / Primeri za test / StabloIORandom.java
1 import java.math.BigInteger;
2 import java.util.Random;
3 import java.util.concurrent.atomic.AtomicInteger;
5 import org.svetovid.io.SvetovidReader;
6 import org.svetovid.io.SvetovidWriter;
8 public class StabloIORandom implements StabloIO {
10 protected long seed;
11 protected int length;
12 protected int depth;
14 public StabloIORandom() {
15 this(System.currentTimeMillis());
16 }
18 public StabloIORandom(long seed) {
19 this(seed, 5, 10);
20 }
22 public StabloIORandom(long seed, int length, int depth) {
23 this.seed = seed;
24 this.length = length;
25 this.depth = depth;
26 }
28 public long getSeed() {
29 return seed;
30 }
32 public void setSeed(long seed) {
33 this.seed = seed;
34 }
36 public int getLength() {
37 return length;
38 }
40 public void setLength(int length) {
41 this.length = length;
42 }
44 public int getDepth() {
45 return depth;
46 }
48 public void setDepth(int depth) {
49 this.depth = depth;
50 }
52 @Override
53 public Stablo readStablo(SvetovidReader in) {
54 Random random = new Random(seed);
55 AtomicInteger sequence = new AtomicInteger();
56 Stablo stablo = readStablo(random, sequence, length, depth);
57 return stablo;
58 }
60 private Stablo readStablo(Random random, AtomicInteger sequence, int length, int depth) {
61 if (depth <= 0) {
62 return null;
63 }
64 int id = sequence.addAndGet(1 + random.nextInt(9));
65 String vrednost = new BigInteger(length, random).toString(36);
66 Stablo desni = readStablo(random, sequence, length + random.nextInt(5), depth - random.nextInt(5));
67 Stablo levi = readStablo(random, sequence, length + random.nextInt(5), depth - random.nextInt(5));
68 return new Stablo(id, vrednost, levi, desni);
69 }
71 @Override
72 public void printStablo(SvetovidWriter out, Stablo stablo) {
73 throw new UnsupportedOperationException();
74 }
75 }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner