-
Notifications
You must be signed in to change notification settings - Fork 0
/
WAVLTreeMeasurements.java
66 lines (53 loc) · 2.1 KB
/
WAVLTreeMeasurements.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package wavltree;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
public class WAVLTreeMeasurements {
public static int N = 10;
public static int NUM_OF_OPERATIONS = 10000 * N;
public static void main(String[] args) {
ArrayList<Integer> unsorted = new ArrayList<>(NUM_OF_OPERATIONS);
for (int i = 0; i < NUM_OF_OPERATIONS; i++) {
unsorted.add(i);
}
ArrayList<Integer> sorted = new ArrayList<Integer>(unsorted);
Collections.shuffle(unsorted);
WAVLTree t = new WAVLTree();
int sumOperationsOfInsertions = 0;
int maxOperationsOfInsertions = 0;
for (int i = 0; i < NUM_OF_OPERATIONS; i++) {
int n = insert(t, unsorted.get(i));
sumOperationsOfInsertions += n;
if (n > maxOperationsOfInsertions) {
maxOperationsOfInsertions = n;
}
}
System.out.println("Max num of operations of insertions: " + maxOperationsOfInsertions);
System.out.println(
"Avg num up operations of insertions: " + ((double) sumOperationsOfInsertions / NUM_OF_OPERATIONS));
int sumOperationsOfDeletions = 0;
int maxOperationsOfDeletions = 0;
for (int i = 0; i < NUM_OF_OPERATIONS; i++) {
int n = delete(t, sorted.get(i));
sumOperationsOfDeletions += n;
if (n > maxOperationsOfDeletions) {
maxOperationsOfDeletions = n;
}
}
System.out.println("Max num of operations of deletions: " + maxOperationsOfDeletions);
System.out.println(
"Avg num up operations of deletions: " + ((double) sumOperationsOfDeletions / NUM_OF_OPERATIONS));
}
public static int getRandInt(int min, int max) {
Random rand = new Random();
return rand.nextInt(max - min) + min;
}
public static int insert(WAVLTree t, int k) {
int res = t.insert(k, Integer.toString(k));
return res;
}
public static int delete(WAVLTree t, int k) {
int res = t.delete(k);
return res;
}
}