-
Notifications
You must be signed in to change notification settings - Fork 0
/
animalTree.java
146 lines (130 loc) · 4.61 KB
/
animalTree.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/***
* Class to define to Animal Tree
* @author Gabrielle Dias
* @version 0.1
* Date of creation: April 15, 2022
* Last Date Modified: April 15, 2022
*/
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/***
* check if input Animal Tree is a valid input
* @author gabrielledias
*
*/
public class animalTree {
public static void main(String[] args) {
BST<String> animalBST = new BST<>();
Heap<String> animalHeap = new Heap<>();
ArrayList<String> animalAL = new ArrayList<>();
readFile(animalAL, "animals.txt");
testAdd(animalBST, animalHeap, animalAL);
testContains(animalBST, animalHeap, animalAL);
testRemove(animalBST, animalHeap, animalAL);
System.out.println("\n\nBefore sorting animalAL");
System.out.println("BST height: " + animalBST.height());
System.out.println("heap height: " + animalHeap.height());
System.out.println("BST isBalanced? " + animalBST.isBalanced());
System.out.println("Heap isBalanced? " + animalHeap.isBalanced());
animalBST.clear();
animalHeap.clear();
java.util.Collections.sort(animalAL);
for(int i=0; i<animalAL.size(); i++) {
String name = animalAL.get(i);
animalBST.add(name);
animalHeap.add(name);
}
System.out.println("\n\nAfter sorting animalAL");
System.out.println("BST height: " + animalBST.height());
System.out.println("heap height: " + animalHeap.height());
System.out.println("BST isBalanced? " + animalBST.isBalanced());
System.out.println("Heap isBalanced? " + animalHeap.isBalanced());
}
/***
* check if input Read File is a valid input
* @param al for the al you are searching for
* @param filename for the filename you are searching for
*/
public static void readFile(ArrayList<String> al, String filename) {
File file = new File(filename);
try {
Scanner read = new Scanner(file);
while(read.hasNextLine()) {
al.add(read.nextLine());
}
read.close();
}
catch(FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
}
/***
* check if input Test Add is a valid input
* @param bst for the bst you are searching for
* @param heap for the heap you are searching for
* @param al for the al you are searching for
*/
public static void testAdd(BST<String> bst, Heap<String> heap, ArrayList<String> al) {
int totalBST = 0, totalHeap = 0;
int count = 0;
System.out.println("Testing Add()");
System.out.printf("%-30s\t%-10s\t%-10s\n", "Animal name", "BST Iterations", "Heap Iterations");
for(int i=0; i<al.size(); i++) {
String name = al.get(i);
int bstIter = bst.add(name);
int heapIter = heap.add(name);
if(i % 24 == 0) {
count++;
System.out.printf("%-30s\t%-10d\t%-10d\n", name, bstIter, heapIter);
totalBST += bstIter;
totalHeap += heapIter;
}
}
System.out.printf("%-30s\t%-10d\t%-10d\n", "average", totalBST/count, totalHeap/count);
}
/***
* check if input Test Contains is a valid input
* @param bst for the bst you are searching for
* @param heap for the heap you are searching for
* @param al for the al you are searching for
*/
public static void testContains(BST<String> bst, Heap<String> heap, ArrayList<String> al) {
int totalBST = 0, totalHeap = 0;
System.out.println("Testing Contains()");
System.out.printf("%-30s\t%-10s\t%-10s\n", "Animal name", "BST Iterations", "Heap Iterations");
for(int i=0; i<20; i++) {
int index = (int) (Math.random() * al.size());
String name = al.get(index);
int bstIter = bst.contains(name);
int heapIter = heap.contains(name);
System.out.printf("%-30s\t%-10d\t%-10d\n", name, bstIter, heapIter);
totalBST += bstIter;
totalHeap += heapIter;
}
System.out.printf("%-30s\t%-10d\t%-10d\n", "average", totalBST/20, totalHeap/20);
}
/***
* check if input Test Remove is a valid input
* @param bst for the bst you are searching for
* @param heap for the heap you are searching for
* @param al for the al you are searching for
*/
public static void testRemove(BST<String> bst, Heap<String> heap, ArrayList<String> al) {
int totalBST = 0, totalHeap = 0;
System.out.println("Testing Remove()");
System.out.printf("%-30s\t%-10s\t%-10s\n", "Animal name", "BST Iterations", "Heap Iterations");
for(int i=0; i<20; i++) {
int index = (int) (Math.random() * al.size());
String name = al.get(index);
int bstIter = bst.remove(name);
int heapIter = heap.remove();
System.out.printf("%-30s\t%-10d\t%-10d\n", name, bstIter, heapIter);
totalBST += bstIter;
totalHeap += heapIter;
}
System.out.printf("%-30s\t%-10d\t%-10d\n", "average", totalBST/20, totalHeap/20);
}
}