-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-notes.txt
308 lines (242 loc) · 5.44 KB
/
app-notes.txt
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// API that is very general and abstract and allows extension
// Tree is a sample data structure, and an optimal greedy algorithm will be a sample algorithm
// User constructs a graph and keeps track of vertices and edges and weights on the edges and orientation of the edges
// User can compute connected components and return minimum spanning tree
// User can use either Prim's or Kruskal's to find minimum spanning tree
// Disjoint-set data structure also kept track of along with the graph for use in Kruskal's
// object-oriented principles and design patterns to incorporate:
// prefer composition to inheritance
// nested class
//
interface Set<T> {
boolean insert(T element);
T search(T element);
boolean delete(T element);
}
public abstract class DataSet<T> implements Set<T> {
private T elements[];
private int size;
public abstract boolean insert(T element);
public abstract T search(T element);
public abstract boolean delete(T element);
}
public class TreeElement<T implements Comparable<T>> {
public T element;
public TreeElement<T> left;
public TreeElement<T> right;
public int compare(TreeElement t) {
return this.element.compareTo(t.element);
}
}
public class Tree<T extends TreeElement> extends DataSet<T extends TreeElement> {
public T root;
public boolean compare(T t1, T t2) {
return t1.compareTo(t2);
}
public static void createTree<T>() {
elements = new T[1];
root = elements[0];
root.left = null;
root.right = null;
}
boolean insert(T element) {
if (root == null) {
root = element;
}
else {
T temp = root;
boolean done = false;
while (!done) {
if (compare(element, temp) <= 0) {
if (temp.left == null) {
temp.left = element;
done = true;
}
else {
temp = temp.left;
}
}
else {
if (temp.right == null) {
temp.right = element;
done = true;
}
else {
temp = temp.right;
}
}
}
}
return true;
}
T search(T element) {
if (root == null) {
return null;
}
else {
T temp = root;
while (temp != null) {
if (compare(element, temp) == 0) {
return temp;
}
else if (compare(element, temp) < 0) {
temp = temp.left;
}
else {
temp = temp.left;
}
return false;
}
}
}
boolean delete(T element) {
if (root == null) {
return false;
}
else {
T temp1 = search(element);
if (temp1.left != null && temp1.right != null) {
T temp2 = temp1;
temp2 = temp2.left;
T temp3 = temp2;
while (temp2.right != null) {
temp3 = temp2;
temp2 = temp2.right;
}
temp3.right = null;
temp1 = temp2;
return true;
}
else if (temp1.left != null) {
temp1 = temp1.left;
return true;
}
else if (temp1.right != null) {
temp1 = temp1.right;
return true;
}
else {
return false;
}
}
}
}
public class disjointSetElement<T> {
public disjointSetElement<T> parent;
public int rank;
}
class Ordered_Pair<T> {
T first;
T second;
T getFirst() {
return first;
}
T getSecond() {
return second;
}
}
class Graph<T> {
Set<Ordered_Pair<T>> edges;
Set<T> vertices;
Set<double> edgeWeights;
void addEdge();
void addVertex();
}
class disjointSetDataStructure<T extends disjointSetElement<T>> implements Set<T extends disjointSetElement<T>> {
void makeSet(T t) {
t.parent = t;
t.rank = 0;
}
T findSet<T extends disjointSetElement<T>>(T t) {
if (t != t.parent) {
t.parent = findSet(t.parent);
}
return t;
}
T link(T t1, T t2) {
if (t1.rank > t2.rank) {
t2.parent = t1;
}
else {
t1.parent = t2;
if (t1.rank == t2.rank) {
++t2.rank;
}
}
}
void union(T t1, T t2) {
link(findSet(t1),findSet(t2));
}
}
class Algorithms {
// all trees just use inorder traversal instead of sort
// need to ensure edges ordered by their weight though
// all hash tables maybe return an array for use or something
// idea - have a getnext method for traversing
void kruskalsAlgo<Tree<T>>(Graph g) {
//
T t = g.getTree().getRoot();
while () {
}
Set<T> minSpanTreeSubset;
}
void kruskalsAlgo<Tree>() {
//preorder traversal - maybe override this method
}
void primsAlgo() {
}
}
// after implementing the above, write a program that constructs graphs and runs the algorithms, then tweak to make it work well
// and to see what else might work well with it
// start by putting it all in one package, then consider how some pieces like the set interface may be put in another package
class Outputter {
public void displayOptions() {
System.out.println("Choose a set:");
System.out.println("Choose an object:");
System.out.println("Choose an operation:");
}
public void displayAllOptions() {
}
public void displaySomeOptions(ArrayList<Option> options) {
}
}
Set s;
s.insert(o1);
s.insert(o2);
s.insert(o3);
s.search(o2);
s.delete(o1);
s.search(o1);
s.insert(o3);
interface Event {
void callOperation(Set<T> s);
}
interface Schedule<T extends Event, V extends Algorithm> {
Schedule createSchedule<T, V>();
}
interface Runner {
runSequenceOfEvents;
}
interface Output {
void displayInputOptions();
void getOutputFromController();
void displayOutput();
}
interface Input {
void getInput();
void giveInputToController();
}
interface controller {
giveDataToObjectCreator;
askRunnerObjectToRun;
}
interface creator { // this could be generic
createObject;
}
interface Algorithm {
useInputToPerformOperations;
}
class event {
}
class schedule {
}