-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTippKatsed.java
36 lines (29 loc) · 1.01 KB
/
NTippKatsed.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import ee.ut.dendroloj.Dendrologist;
import java.util.Arrays;
import java.util.Random;
public class NTippKatsed {
static NTipp juhuslikPuu(int h, Random juhus) {
if (h == 0) return null;
int alluvaid = 2 + juhus.nextInt(4);
NTipp[] alluvad = new NTipp[alluvaid];
for (int i = 0; i < alluvaid; i++) {
if (juhus.nextBoolean())
alluvad[i] = juhuslikPuu(h - 1, juhus);
}
return new NTipp("", alluvad);
}
static int nummerdaEesjärjestuses(NTipp tipp, int i) {
if (tipp == null) return i;
tipp.info = Integer.toString(i);
i += 1;
for (NTipp alluv : tipp.alluvad) {
i = nummerdaEesjärjestuses(alluv, i);
}
return i;
}
public static void main(String[] args) {
NTipp tipp = juhuslikPuu(5, new Random(42));
nummerdaEesjärjestuses(tipp, 1);
Dendrologist.drawTree(tipp, t -> t.info, t -> Arrays.asList(t.alluvad));
}
}