-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoManager.java
85 lines (69 loc) · 2.1 KB
/
PhotoManager.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
public class PhotoManager {
private BST<LinkedList<Photo>> bst;
private LinkedList<Photo> photos;
// Constructor
public PhotoManager() {
bst = new BST<>();
photos = new LinkedList<>();
}
// Add a photo
public void addPhoto(Photo p) {
LinkedList<String> tags = p.getTags();
tags.findFirst();
LinkedList<Photo> photosList;
while (tags.isThereNext()) {
String tag = tags.retrieve();
if (bst.findKey(tag)) {
photosList = bst.retrieve();
photosList.insert(p);
bst.update(tag, photosList);
} else {
photosList = new LinkedList<>();
photosList.insert(p);
bst.insert(tag, photosList);
}
tags.findNext();
}
//all photos
photos.insert(p);
}
// Delete a photo
public void deletePhoto(String path) {
BSTNode<LinkedList<Photo>> root = bst.getRoot();
updateNodeData(root, path);
}
public void updateNodeData(BSTNode<LinkedList<Photo>> node, String path) {
LinkedList<Photo> photosList = node.data;
photosList.findFirst();
while (photosList.isThereNext()) {
Photo photo = photosList.retrieve();
if (photo.path.equals(path)) {
photosList.remove();
}
if (photosList.isThereNext())
photosList.findNext();
}
if (photosList.empty()) {
bst.removeKey(node.key);
}
if (node.left != null) {
updateNodeData(node.left, path);
}
if (node.right != null) {
updateNodeData(node.right, path);
}
if (node.left == null && node.right == null) return;
}
// Return the inverted index of all managed photos
public BST<LinkedList<Photo>> getPhotos() {
return bst;
}
public LinkedList<Photo> getAllPhotos() {
return photos;
}
//add to PhotoManager
@Override
public String toString() {
return bst.toString();
}
}